Professional Documents
Culture Documents
Cooperating processes
Execution of most of the processes relies on
Process Synchronization each other. These processes are called
cooperating processes.
Naveen Aggarwal Cooperating processes communicate with each
other either by
Shared logical address space (threads or LWP) or
Shared files (asynchronously running processes at a
same time in memory)
1
06-09-2015
2
06-09-2015
3
06-09-2015
Naveen Aggarwal
4
06-09-2015
Naveen Aggarwal
5
06-09-2015
6
06-09-2015
Naveen Aggarwal
7
06-09-2015
P / Wait Operation for Binary Semaphore V / Signal Operation for Binary Semaphore
8
06-09-2015
printer.
Naveen Aggarwal Naveen Aggarwal
9
06-09-2015
To solve Problems
# of processes.
# of resources =# of semaphore variables
semaphores should be initialized to number of
PROBLEM SOLVING USING available instances.
SEMAPHORES Define entry and exit criteria for every critical
section i.e. where shared variables are
modified.
For every P operation (wait), there should be
corresponding V operation (signal).
Naveen Aggarwal
10
06-09-2015
11
06-09-2015
12
06-09-2015
Table eat;
5 3
signal (left);
P4 P3 signal(right);
4
If there are N philosophers and each philosopher picks up the its left and right
think;
chopsticks then ith philosopher will pick up chopsticks with numbers
Right as (i)%N and }
Left as (i+1) % N
We represent each chopstick with a semaphore as it should have mutual
exclusive access
13
06-09-2015
14
06-09-2015
problem. else
count++; //increase count
Semaphore ‘customer’ is used to wait or signal signal (mutex); //release count variable
customers. It is initialized to 0. signal(customer); // to wakeup barber
wait(barber); // if barber is not yet ready
Semaphore ‘barber’ is used to check the readiness Get Haircut;
of barber. It is also initialized to 0
wait(mutex); //to decrease count variable
Semaphore mutex to provide exclusive access to count--;
signal(mutex); //release count variable
count variable. }
15
06-09-2015
16
06-09-2015
17
06-09-2015
THANKS
18