semaphores ref: william stallings g.anuradha. principle of a semaphore two or more processes can...
TRANSCRIPT
![Page 1: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/1.jpg)
Semaphores
Ref: William StallingsG.Anuradha
![Page 2: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/2.jpg)
Principle of a SemaphoreTwo or more processes can cooperate by means of simple signals, such that a process can be forced to stop at a specified place until it has received a specific signal
• For signaling semaphores are used– For sending a signal semSignal (s) is used– For receiving a signal semWait(s) is used
• First defined by Dijkstra• Semaphore is a variable
– A semaphore may be initialized to a nonnegative integer value.– The semWait operation decrements the semaphore value. – The semSignal operation increments the semaphore value.
![Page 3: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/3.jpg)
Definition of semaphore
![Page 4: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/4.jpg)
Binary Semaphore
Can only take on values 0, 1
![Page 5: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/5.jpg)
Operations on Binary Semaphore
![Page 6: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/6.jpg)
Types of Semaphores
• Binary semaphore:-takes value 0 and 1• Mutex:-similar to binary semaphore but the
key difference is that the mutex is locked and unlocked by the same process
• Strong semaphore:- Process who is blocked the longest is released from queue first
• Weak semaphore:- Semaphore whose order is not specified.
![Page 7: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/7.jpg)
Animation
![Page 8: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/8.jpg)
Classical Synchronization problems
• Producer consumer problem• Reader-writer problem• Barber shop• Dining philosopher
![Page 9: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/9.jpg)
9
I. Producer-ConsumerImagine a chef cooking items and putting them onto a conveyor belt
![Page 10: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/10.jpg)
10
Producer-ConsumerImagine a chef cooking items and putting them onto a conveyor belt
Now imagine many such chefs!
![Page 11: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/11.jpg)
11
Producer-ConsumerImagine a chef cooking items and putting them onto a conveyor belt
Now imagine many such chefs!
![Page 12: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/12.jpg)
12
Producer-ConsumerImagine a chef cooking items and putting them onto a conveyor belt
Now imagine many such chefs!
Now imagine a customer picking items off the conveyor belt
![Page 13: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/13.jpg)
13
Producer-ConsumerImagine a chef cooking items and putting them onto a conveyor belt
Now imagine many such chefs!
Now imagine many such customers picking items off the conveyor belt!
![Page 14: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/14.jpg)
14
Producer-Consumer
Imagine a chef cooking items and putting them onto a conveyor belt
Now imagine many such chefs!
Now imagine many such customers picking items off the conveyor belt!
![Page 15: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/15.jpg)
15
Producer-Consumer
insertPtr
removePtr
Chef = ProducerCustomer = Consumer
![Page 16: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/16.jpg)
16
Producer-Consumer
insertPtr
removePtr
Chef = ProducerCustomer = Consumer
![Page 17: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/17.jpg)
17
Producer-Consumer
insertPtr
removePtr
Chef = ProducerCustomer = Consumer
![Page 18: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/18.jpg)
18
Producer-Consumer
insertPtr
removePtr
Chef = ProducerCustomer = Consumer
![Page 19: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/19.jpg)
19
Producer-Consumer
insertPtr
removePtr
Chef = ProducerCustomer = Consumer
![Page 20: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/20.jpg)
20
Producer-Consumer
insertPtr
removePtr
Chef = ProducerCustomer = Consumer
![Page 21: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/21.jpg)
21
Producer-Consumer
insertPtr
removePtr
Chef = ProducerCustomer = Consumer
![Page 22: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/22.jpg)
22
Producer-Consumer
insertPtr
removePtr
BUFFER FULL: Producer must be blocked!
Chef = ProducerCustomer = Consumer
![Page 23: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/23.jpg)
23
Producer-Consumer
insertPtrremovePtr
Chef = ProducerCustomer = Consumer
![Page 24: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/24.jpg)
24
Producer-Consumer
insertPtr
removePtr
Chef = ProducerCustomer = Consumer
![Page 25: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/25.jpg)
25
Producer-Consumer
insertPtr
removePtr
Chef = ProducerCustomer = Consumer
![Page 26: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/26.jpg)
26
Producer-Consumer
insertPtr
removePtr
Chef = ProducerCustomer = Consumer
![Page 27: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/27.jpg)
27
Producer-Consumer
insertPtr
removePtr
Chef = ProducerCustomer = Consumer
![Page 28: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/28.jpg)
28
Producer-Consumer
insertPtr
removePtr
Chef = ProducerCustomer = Consumer
![Page 29: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/29.jpg)
29
Producer-Consumer
insertPtr
removePtr
Chef = ProducerCustomer = Consumer
![Page 30: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/30.jpg)
30
Producer-Consumer
insertPtr
removePtr
BUFFER EMPTY: Consumer must be blocked!
Chef = ProducerCustomer = Consumer
![Page 31: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/31.jpg)
Producer Consumer problem
Infinite buffer
![Page 32: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/32.jpg)
![Page 33: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/33.jpg)
Possible scenario
![Page 34: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/34.jpg)
Correct solution
![Page 35: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/35.jpg)
Finite circular buffer for the Producer/consumer problem
![Page 36: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/36.jpg)
Summing up• One process produces some type of data• The other process consumes that data
– Data stored in a shared buffer (infinite size)– Require mutual exclusion to access buffer
• Producer do forever
produce itemwait(s)append to queuen++if n = 1 then signal(delay)signal(s)
• Consumerwait(delay)do forever
wait(s)remove from queuen--m = nsignal(s)if m = 0 then wait(delay)
![Page 37: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/37.jpg)
Barbershop Problem• 3 barbers, each with a barber chair
– Haircuts may take varying amounts of time
• Sofa can hold 4 customers, max of 20 in shop– Customers wait outside if necessary
• When a chair is empty:– Customer sitting longest on sofa is served– Customer standing the longest sits down
• After haircut, go to cashier for payment– Only one cash register– Algorithm has a separate cashier, but often barbers
also take payment• This is also a critical section
![Page 38: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/38.jpg)
Fair Barbershop program barbershop2;var max_capacity: semaphore (:=20);
sofa: semaphore (:=4);barber_chair, coord: semaphore (:=3);mutex1, mutex2: semaphore (:=1);cust_ready, leave_b_chair, payment, receipt: semaphore (:=0)finished: array [1..50] of semaphore (:=0);count: integer;
procedure customer; procedure barber; procedure cashier;var custnr: integer; var b_cust: integer beginbegin begin repeat wait (max_capacity ); repeat wait( payment ); enter shop; wait( cust_ready ); wait( coord ); wait( mutex1 ); wait( mutex2 ); accept payment; count := count + 1; dequeue1( b_cust ); signal( coord ); custnr := count; signal( mutex2 ); signal( receipt ); signal( mutex1 ); wait( coord ); forever wait( sofa ); cut hair; end; sit on sofa; signal( coord ); wait( barber_chair ); signal( finsihed[b_cust] ); get up from sofa; wait( leave_b_chair ); signal( sofa ); signal( barber_chair ); sit in barber chair; forever wait( mutex2 ); end; enqueue1( custnr ); signal( cust_ready ); signal( mutex2 ); wait( finished[custnr] ); leave barber chair; signal( leave_b_chair ); pay; signal( payment ); wait( receipt ); exit shop; signal( max_capacity );end;
![Page 39: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/39.jpg)
39
II. Reader-Writer Problem• A reader: read data• A writer: write data• Rules:
– Multiple readers may read the data simultaneously– Only one writer can write the data at any time– A reader and a writer cannot access data simultaneously
• Locking table: whether any two can be in the critical section simultaneously
Reader Writer
Reader OK No
Writer No No
![Page 40: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/40.jpg)
40
III. Dining Philosophers: an intellectual game
1
![Page 41: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/41.jpg)
Problem Statement
• Five philosophers eat then think forever– They never sleep nor relieve themselves!– They do not behave altruistically
• They eat at a communal table– It has a single bowl of tangled spaghetti– Five plates each with a single fork to the left of their plate– To eat a philosopher must have two forks, their own and
that of their neighbour’s to the right– If a philosopher is unable to eat they resume thinking
![Page 42: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/42.jpg)
Ramifications• Deadlock
– All philosophers decide to eat at same time– They all pick up one fork– None of them can eat hence the system comes to
a halt
• Starvation– Some philosophers think for such a short time and
contrive to put their forks down in such a way that no other philosophers have the opportunity to pick up the two forks they require to eat
![Page 43: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/43.jpg)
Deadlock - Pictorially
![Page 44: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/44.jpg)
Starvation - Pictorially
![Page 45: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/45.jpg)
Naïve Solution
• Unconstrained picking up and putting down of forks causes problems– So put down in the reverse order of picking up
• Manage the entry and exit of philosophers to the table by means of a Butler process that entry and exit is orderly
• Each fork is a process– Accessed either from left or right hand
![Page 46: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/46.jpg)
Deadlocks
• Each philosopher has their left fork• BUT• Cannot get a right fork
• Butler did nothing– Needs to control access to table so that there is
always one empty seat– Ensures one philosopher can eat
![Page 47: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/47.jpg)
Butler Controls
• No more than 4 philosopher can sit at the table (enter)
• Once 4 philosophers seated then Butler only lets people exit (get down) from the table
![Page 48: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/48.jpg)
Problems with Semaphores
• Incorrect usage of semaphores leads to timing errors
• Timing errors occur only if some particular execution sequence takes place.(rare)
• InCorrect use of semaphore operations– signal (mutex) …. wait (mutex) (multiple CS)– wait (mutex) … wait (mutex) (deadlock)– Omitting of wait (mutex) or signal (mutex) (or
both)
![Page 49: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/49.jpg)
Monitors• A high-level abstraction (ADT) that provides a convenient and effective
mechanism for process synchronization• Only one process may be active within the monitor at a time• Has one or more procedure, initialization sequence, local data• Local data variables are accessible only by the monitor’s procedures and not
by any external procedure
monitor monitor-name{
// shared variable declarationsprocedure P1 (…) { …. }
…
procedure Pn (…) {……}
Initialization code ( ….) { … }…
}}
![Page 50: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/50.jpg)
Schematic view of a Monitor
![Page 51: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/51.jpg)
Condition Variables• Synchronization is supported by the use of condition variables that
are contained and accessible only within the monitor• condition x, y;
• Two operations on a condition variable:– x.wait () – Suspends execution of the calling process on
condition x. monitor is available for use by another process– x.signal () – resumes execution of some process suspended after
a wait on the same condion. one of processes (if any) that invoked
![Page 52: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/52.jpg)
Monitor with Condition Variables
![Page 53: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/53.jpg)
Monitors for Producer consumer problem
• Monitor module used to control the buffer used to store and retrieve characters.
• Condition variables– Not full :- TRUE when there is room to add atleast
one character to the buffer– Not empty :- TRUE when there is atleast one
character in the buffer
![Page 54: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/54.jpg)
Monitors for Producer consumer Bounded Buffer problem
![Page 55: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/55.jpg)
![Page 56: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/56.jpg)
![Page 57: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/57.jpg)
Solution to Dining Philosophers
monitor DP {
enum { THINKING; HUNGRY, EATING) state [5] ;condition self [5];’/* hungry but is unable to obtain forks*/
/* A philosopher can set state[i]=eating only if state (i+4)%5 and (i+1)%5 are not eating */void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self [i].wait;}
void putdown (int i) { state[i] = THINKING;
// test left and right neighbors test((i + 4) % 5); test((i + 1) % 5);
}
![Page 58: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/58.jpg)
Solution to Dining Philosophers (cont)
void test (int i) { if ( (state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5] != EATING) ) { state[i] = EATING ;
self[i].signal () ; } }
initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING;}
}
![Page 59: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/59.jpg)
Solution to Dining Philosophers (cont)
• Each philosopher I invokes the operations pickup() and putdown() in the following sequence:
dp.pickup (i)
EAT
dp.putdown (i)
No two neighbours eat simultaneously and no deadlocks will occur. But a philosopher can starve to death…………………………
![Page 60: Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process](https://reader036.vdocuments.us/reader036/viewer/2022062519/5697c0271a28abf838cd6046/html5/thumbnails/60.jpg)