Lecture 25 Syed Mansoor Sarwar Operating Systems Lecture 25 Syed Mansoor Sarwar
© Copyright Virtual University of Pakistan Agenda for Today Review of previous lecture Dining philosophers problem High-level synchronization constructs Critical region Monitor Recap of lecture 30 November 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Review of Lecture 24 Counting semaphores Classical synchronization problems Bounded buffer problem Readers and writers problem Dining philosophers problem 30 November 2018 © Copyright Virtual University of Pakistan
Dining Philosophers Problem Five philosophers who spend their lives just thinking and eating. Only five chopsticks are available to the philosophers 30 November 2018 © Copyright Virtual University of Pakistan
Dining Philosophers Problem Each philosopher thinks. When he becomes hungry, he sits down and picks up the two chopsticks that are closest to him and eats. After a philosopher finishes eating, he puts down the chopsticks and starts to think. 30 November 2018 © Copyright Virtual University of Pakistan
Dining-Philosophers Problem 30 November 2018 © Copyright Virtual University of Pakistan
Dining-Philosophers Problem Shared data : semaphore chopstick[5]; // Initialized to 1 30 November 2018 © Copyright Virtual University of Pakistan
Dining-Philosophers Problem Philosopher i do { wait(chopstick[i]) wait(chopstick[(i+1)% 5]) eat signal(chopstick[i]); signal(chopstick[(i+1)% 5]) think } while (1); 30 November 2018 © Copyright Virtual University of Pakistan
Possibility of Deadlock If all philosophers become hungry at the same time and pick up their left chopstick, a deadlock occurs. 30 November 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Possible Solutions Allow at most four philosophers to be sitting simultaneously at the table. Allow a philosopher to pick up his/her chopsticks only if both chopsticks are available (to do this she must pick them up in a critical section) 30 November 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Possible Solutions Use an asymmetric solution; that is, an odd philosopher picks up first her left chopstick, whereas an even philosopher picks up her right chopstick and then her left chopstick. 30 November 2018 © Copyright Virtual University of Pakistan
Possibility of Starvation Two philosophers who are fast eaters and fast thinkers, and lock the chopsticks before others every time. 30 November 2018 © Copyright Virtual University of Pakistan
High-level Synchronization Constructs Shift the responsibility of enforcing mutual exclusion from the programmer (where it resides when semaphores are used) to the compiler. 30 November 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Critical Regions A critical region is a section of code that is always executed under mutual exclusion. 30 November 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Critical Regions They consist of two parts: Variables that must be accessed under mutual exclusion. A new language statement that identifies a critical region in which the variables are accessed. 30 November 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan The region Statement A shared variable v of type T, is declared as: v: shared T; region v when B do S; Variable v is accessed only inside S when B is true and when S is being executed, no other process can access variable v. 30 November 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan The region Statement When a process tries to execute the region statement, the Boolean expression B is evaluated. If B is true, statement S is executed. If it is false, the process is delayed until B becomes true and no other process is in the region associated with v. 30 November 2018 © Copyright Virtual University of Pakistan
Bounded Buffer Example Shared data struct buffer { int pool[n]; int count, in, out; } 30 November 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Producer Process Producer process inserts nextp into the shared buffer region buffer when (count < n) { pool[in] = nextp; in:= (in+1) % n; count++; } 30 November 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Consumer Process Consumer process removes an item from the buffer and puts it in nextc region buffer when (count > 0) { nextc = pool[out]; out = (out+1) % n; count--; } 30 November 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan The region Statement What happens when two region statements are executed simultaneously? region v when B do S1; region v when B do S2; 30 November 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Monitors High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent cooperating processes. Only one process at a time is active within a monitor. 30 November 2018 © Copyright Virtual University of Pakistan
Schematic View of a Monitor 30 November 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Monitors monitor monitor-name { shared variable declarations procedure P1 (…) { . . . } procedure P2 (…) { . . . } … initialization code } 30 November 2018 © Copyright Virtual University of Pakistan
Monitors with Condition Variables Additional synchronization constructs are needed to model some synchronization problems. They can be modeled with condition variables. condition x, y; 30 November 2018 © Copyright Virtual University of Pakistan
Monitors with Condition Variables Only two operations can be performed on condition variables, wait and signal. x.wait: Process invoking this operation is suspended until another process invokes x.signal. 30 November 2018 © Copyright Virtual University of Pakistan
Monitors with Condition Variables x.signal: Resumes exactly one suspended process. If no process is suspended, this is a null operation, i.e., the state of x is unaffected. 30 November 2018 © Copyright Virtual University of Pakistan
Monitor with Condition Variables 30 November 2018 © Copyright Virtual University of Pakistan
Dining Philosophers Example monitor dp { enum {thinking, hungry, eating} state[5]; condition self[5]; void pickup(int i) // Following slides void putdown(int i) // Following slides void test(int i) // Following slides void init() { for (int i = 0; i < 5; i++) state[i] = thinking; } 30 November 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Dining Philosophers void pickup(int i) { state[i] = hungry; test(i); if (state[i] != eating) self[i].wait(); } 30 November 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Dining Philosophers void putdown(int i) { state[i] = thinking; // test left and right // neighbors test((i+4) % 5); test((i+1) % 5); } 30 November 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Dining Philosophers void test(int i) { if ((state[(i+4)%5]!= eating) && (state[i] == hungry) && (state[(i+1)%5]!= eating)) { state[i] = eating; self[i].signal(); } 30 November 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Recap of Lecture Dining philosophers problem High-level language constructs Critical region Monitor 30 November 2018 © Copyright Virtual University of Pakistan
Lecture 25 Syed Mansoor Sarwar Operating Systems Lecture 25 Syed Mansoor Sarwar