Chapter 2 3 Interprocess Communication Process concept n





























![Correct Solution Program diningphilosophers; Var fork : array[0. . 4] of semaphore; i : Correct Solution Program diningphilosophers; Var fork : array[0. . 4] of semaphore; i :](https://slidetodoc.com/presentation_image/74994c3f5ed68c56760e9644605d9580/image-30.jpg)



- Slides: 33
Chapter 2. 3 : Interprocess Communication Process concept n Process scheduling n Interprocess communication n Deadlocks n Threads n 1
Producer - Consumer Problem Producer Process Consumer Process Produce Get from buffer Put in buffer Consume BUFFER n Buffer is shared (ie. , it is a shared variable) 2
Progress in time…. . Producer p 1 1 p 2 2 p 3 3 Buffer p 4 4 3 instead of 2! Consumer 1 n c 1 2 c 2 Both processes are started at the same time and consumer uses some old value initially t 3
A Race Condition Because of the timing and which process starts first n There is a chance that different executions may end up with different results n 4
Critical Sections n Critical Section n n A section of code in which the process accesses and modifies shared variables Mutual Exclusion n A method of preventing for ensuring that one (or a specified number) of processes are in a critical section 5
Why Processes Need to Communicate? n To synchronize their executions n To exchange data and information 6
Rules to Form Critical Sections 7
Mutual Exclusion Problem : Starvation n Also known as Indefinite Postponement n Definition n n Cause n n Indefinitely delaying the scheduling of a process in favour of other processes Usually a bias in a systems scheduling policies (a bad scheduling algorithm) Solution n Implement some form of aging 8
Another Problem : Deadlocks n Two (or more) processes are blocked waiting for an event that will never occur n Generally, A waits for B to do something and B is waiting for A n Both are not doing anything so both events never occur 9
How to Implement Mutual Exclusion n Three possibilities Application: programmer builds some method into the program n Hardware: special h/w instructions provided to implement ME n OS: provides some services that can be used by the programmer n n All schemes rely on some code for n n n enter_critical_section, and exit_critical_section These "functions" enclose the critical section 10
Application Mutual Exclusion n n Application Mutual Exclusion is n implemented by the programmer n hard to get correct, and n very inefficient All rely on some form of busy waiting (process tests a condition, say a flag, and loops while the condition remains the same) 11
Example n n n Producer produce If lock = 1 loop until lock = 0 lock=1 put in buffer lock=0 Consumer If lock = 1 loop until lock = 0 lock=1 get from buffer lock=0 Consume Note: initially lock = 0 12
Dekker’s Algorithm Program Dekker; Var turn : integer; wantp, wantq : boolean; Procedure p; Begin repeat wantp : = true; while wantq do if turn = 2 then begin wantp: = false; repeat until turn = 1; wantp: =true end; CS; turn : = 2; wantp: = false; NCS; until false; End; Procedure q; Begin repeat wantq 2 : = true; while wantp do if turn = 1 then begin wantq: = false; repeat until turn = 2; wantq: =false end; CS; turn : = 1; wantq : = false; NCS; until false; End; Begin (* main program *) turn: = 1; wantp: = false; wantq: = false; cobegin p; q coend End. 13
Explanation of the Algorithm Procedure p; Begin repeat wantp : = true; (* 1 *) while wantq do if turn = 2 then begin wantp: = false; repeat until turn = 1; wantp: =true end; (* 2 *) CS; turn : = 2; (* 3 *) wantp : = false; (* 4 *) NCS; until false; End; 1. 2. 3. 4. Process makes a request to enter CS If the other process had made a request to enter CS before and if it is its turn then n Take back the request to enter CS by setting the “want” variable to false” n Wait for the other process to exit CS and change the turn variable n Make a new request to enter CS and then enter CS Flip the turn variable so that now the other process can enter CS Set “want” variable to false to indicate that the process is now out of CS 14
Comments Explicit control of transfer by a turn variable. Hence; turn = 1 means P’s turn, turn = 2 Q’s turn n If a process is blocked, the other process can still go n Dekker’s algorithm is correct (prove it!) n It satisfies the mutual exclusion property n It is free from deadlock and starvation n 15
Hardware ME : Test and Set Instruction n n Perform an indivisible x: =r and r: =1 x is a local variable r is a global register set to 0 initially repeat (test&set(x)) until x = 0; < critical section > r: = 0; 16
Hardware ME : Exchange Instruction n n Exchange: swap the values of x and r x is a local variable r is a global register set to 1 initially x: = 0; repeat exchange(r, x) until x = 1; < critical section > exchange(r, x); Note: r: = 0 and x: = 1 when the process is in CS 17
Another Hardware ME : Disabling Interrupts On a single CPU only one process is executed n Concurrency is achieved by interleaving execution (usually done using interrupts) n If you disable interrupts then you can be sure only one process will ever execute n One process can lock a system or degrade performance greatly n 18
Hardware ME Characteristics n Advantages can be used by a single or multiple processes (with shared memory) n simple and therefore easy to verify n can support multiple critical sections n n Disadvantages busy waiting is used (very important) n starvation is possible n deadlock is possible (especially with priorities) n 19
Mutual Exclusion Through OS Semaphores n Message passing n 20
Semaphores n Major advance incorporated into many modern operating systems (Unix, OS/2) n A semaphore is n a non-negative integer n that has two indivisible, valid operations 21
Semaphore Operations n Wait(s) If s > 0 then s: = s - 1 else block this process n Signal(s) If there is a blocked process on this semaphore then wake it up 22
More on Semaphores The other valid operation is initialisation n Two types of semaphores n binary semaphores can only be 0 or 1 n counting semaphores can be any nonnegative integer n n Semaphores are an OS service implemented using one of the methods shown already n usually by disabling interrupts for a very short time 23
Producer - Consumer Problem: Solution by Semaphores Produce CS Wait(mutex) Put in buffer Signal(mutex) n Wait(mutex) Get from buffer Signal(mutex) Consume Initially semaphore mutex is 1 24
Another Example n Three processes all share a resource on which n n one draws an A one draws a B one draws a C Implement a form of synchronization so that the output appears ABC Process A Process B Process C think(); draw_A(); draw_B(); draw_C(); 25
n Semaphore b = 0, c = 0; Process A Process B Process C wait(b); think(); wait(c); think(); draw_A(); think(); draw_B(); signal(b); draw_C(); signal(c); 26
Dining Philosophers Figure is from Modern OS by Tanenbaum n n 5 seating places, 5 plates of spagetti and 5 forks A philosophers life cycle Repeat think; eat forever Eating can only be done with 2 forks Devise a ritual (protocol) that will allow the philosophers to eat. The protocol should satisfy mutual exclusion (no two philosophers try to use the same fork simultaneously) , free from deadlock and absense of starvation 27
Dining Philosophers Solution – First Attempt Program diningphilosophers; Var i : integer; fork : array[0. . 4] of semaphore; Procedure philosopher (i : integer); Begin repeat think; wait(fork[i]); (* get left fork *) wait(fork[(i+1) mod 5]; (* get right fork *) eat; signal(fork[i]); (* return left fork *) signal(fork[(i+1) mod 5]; (* return right fork *) until false; End; Begin (* main *) for i: = 0 to 4 do fork[i]: = 1; (* initially all forks are available *) cobegin philosopher(0); philosopher(1); philosopher(2); philosopher(3); philosopher(4); coend; End. 28
Comments on First Attempt n n n Mutual exclusion is implemented by a binary semaphore fork Deadlock is possible if all 5 philosophers take the left forks simultaneously then all would wait for the right fork How to handle the deadlock and ensure liveliness? n n n Let at the most 4 philosophers to sit and eat. Two of them can eat, one holds a fork and the other just sits One philosopher can eat and the other 3 can hold their left forks 29
Correct Solution Program diningphilosophers; Var fork : array[0. . 4] of semaphore; i : integer; table : semaphore; (* seating limit *) Procedure philosopher (i : integer); Begin repeat think; wait(table); wait(fork[i]); (* get left fork *) wait(fork[(i+1) mod 5]; (* get right fork *) eat; signal(fork[i]); (* return left fork *) signal(fork[(i+1) mod 5]; (* return right fork *) signal(table); until false; End; Begin (* main *) for i: = 0 to 4 do fork[i]: = 1; table: = 4; cobegin philosopher(0); philosopher(1); philosopher(2); philosopher(3); philosopher(4); coend; End. 30
Message Passing n Provides synchronization and information exchange n Message Operations: n send(destination, n receive &message) (source, &message) 31
Producer - Consumer Problem Using Messages #define N 100 /*number of message slots*/ producer( ) {int item; message m; while (TRUE) { produce_item(&item); receive(consumer, &m); build_message(&m, item); send(consumer, &m); }} 32
Consumer( ) {int item; message m; for (i=0; i<N; i++) send(producer, &m); while (TRUE) { receive(producer, &m); extract_item(&m, &item); send(producer, &m); consume_item(item); }} 33