Semas
Semas
03/07/24
Principle of a Semaphore
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.
– wait (S) {
while S <= 0
; // no-op
S--;
}
– signal (S) {
S++;
}
03/07/24
Definition of semaphore
Types of Semaphores
• Binary semaphore:-takes value 0 and 1-used for
resource having a single instance
• Counting Semphore:- when there are more than one
process.
• Mutex:-similar to binary semaphore but the key
difference is that the mutex is locked and unlocked
by the same process
Classical Synchronization problems
• Producer consumer problem
• Reader-writer problem
• Barber shop
• Dining philosopher
I. Producer-Consumer
Imagine a chef cooking items
and putting them onto a conveyor belt
43 43
Producer-Consumer
Imagine a chef cooking items
and putting them onto a conveyor belt
44
Producer-Consumer
Imagine a chef cooking items
and putting them onto a conveyor belt
45
Producer-Consumer
Imagine a chef cooking items
and putting them onto a conveyor belt
46
Producer-Consumer
Imagine a chef cooking items
and putting them onto a conveyor belt
Now imagine many such customers picking items off the conveyor belt!
47
Producer-Consumer
Imagine a chef cooking items
and putting them onto a conveyor belt
Now imagine many such customers picking items off the conveyor belt!
48
Producer-Consumer
Chef = Producer
Customer = Consumer
insertPtr
removePtr
49
Producer-Consumer
Chef = Producer
Customer = Consumer
insertPtr
removePtr
50
Producer-Consumer
Chef = Producer
Customer = Consumer
insertPtr
removePtr
51
Producer-Consumer
Chef = Producer
Customer = Consumer
insertPtr
removePtr
52
Producer-Consumer
Chef = Producer
Customer = Consumer
insertPtr
removePtr
53
Producer-Consumer
Chef = Producer
Customer = Consumer
insertPtr
removePtr
54
Producer-Consumer
Chef = Producer
Customer = Consumer
insertPtr
removePtr
55
Producer-Consumer
Chef = Producer
Customer = Consumer
insertPtr
removePtr
56
Producer-Consumer
Chef = Producer
Customer = Consumer
insertPtr
removePtr
57
Producer-Consumer
Chef = Producer
Customer = Consumer
removePtr
insertPtr
58
Producer-Consumer
Chef = Producer
Customer = Consumer
removePtr
insertPtr
59
Producer-Consumer
Chef = Producer
Customer = Consumer
removePtr
insertPtr
60
Producer-Consumer
Chef = Producer
Customer = Consumer
removePtr
insertPtr
61
Producer-Consumer
Chef = Producer
Customer = Consumer
removePtr
insertPtr
62
Producer-Consumer
Chef = Producer
Customer = Consumer
removePtr
insertPtr
63
Producer-Consumer
Chef = Producer
Customer = Consumer
removePtr
insertPtr
64
Producer Consumer problem
Infinite buffer
Solution of Producer consumer
using Semaphores
• Examples
– Compiler is a producer producing object code
– Assembler is a consumer that takes object code
– Printing a file is a producer process
– Printing file on the printer is a consumer process
03/07/24
Requirements to solve producer
consumer problem
• Producer should not produce an item when buffer is full
• Consumer should not consume when buffer is empty
• Producer and consumer should not try to access and update the buffer at
the same time
• Producer is full and item is produced then producer should be blocked
• If consumer is ready and buffer is empty then consumer should be
blocked
• When a consumer process consumes an item, that is, a slot in the buffer is
created the blocked producer must be signaled about it.
• When a producer process produces an item in the empty buffer, blocked
consumer process must be signaled about it.
03/07/24
• Producer Consumer problem can be solved by
placing a semaphore on the buffer.
• Two more semaphores are taken which will
count the number of empty slots and full slots
in the buffer.
• 3 semaphores are used in the solution
– empty semaphore initialized to n where n is the
number of empty slots in the buffer
– full semaphore initialized to zero
– buffer-access semaphore initialized to one
Algorithms for producer-consumer problem’s
solution with Semaphore
• The producer after producing the item waits
on the empty semaphore to check for an
empty slot
• Then it waits for the Buffer-access semaphore
to check whether any other process is
accessing it
• After getting permission it stores
• After storing it signals the buffer_access so
that any other process in wait can access the
buffer.
What is a reader writer problem?
Read Write
Read Y N
Write N N
One writer process is inside the critical section and multiple reader
processes arrive, then they must wait on the semaphore.
One reader process is inside the critical section and multiple reader
processes arrive, then they will not wait on the semaphore.
One writer process is inside the critical section and multiple writer
processes arrive, then they must wait on the semaphore.
Reader-Writer problem using Semaphore
ReadCount : integer variable as counter for reader processes;
initialized as 0.
81
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
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
Deadlock - Pictorially
Starvation - Pictorially
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
Deadlocks
• Each philosopher has their left fork
• BUT
• Cannot get a right fork
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
…
91
Condition Variables
• Synchronization is supported by the use of condition variables that are contained
and accessible only within the monitor
• condition x, y;
92
Monitor with Condition Variables
93
Monitors for Producer consumer
problem
• Monitor module used to control the buffer to
store and retrieve characters.
• Condition variables
– Not full :- TRUE then there is room to add atleast
one character to the buffer
– Not empty :- TRUE then there is atleast one
character in the buffer
94
Monitors for Producer consumer
Bounded Buffer problem
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;
}
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
98
Solution to Dining Philosophers (cont)
dp.pickup (i)
EAT
dp.putdown (i)
99
Synchronization Hardware
• One simple solution to the critical section
problem is a lock
Hardware solutions
• In a uniprocessor environment, the interrupts
could be prevented from occurring while a
shared variable was modified(nonpreemptive)
• TestAndSet() and Swap() instructions are used
in multiprocessor environment
Definition of the TestAndSet()