Process Synch
Process Synch
– Computation speedup
– Modularity (Threads)
– Convenience
Inter-process communication
• Cooperating process require an inter-
process communication (IPC) mechanism
that will allow them to exchange data and
information.
• Also maintain protection and isolation of
processes.
• There are two fundamental model of IPC:
– Shared memory
– Message passing
Message passing
• Useful for exchanging smaller amount of
data.
• Easier to implement.
• Kernel is involved for every messages.
• More overheads.
Shared memory
• It allows maximum speed and
convenience of communication.
• Faster than message passing.
• Less overhead.
• Once the connection established then the
involvement of kernel is over.
Examples of cooperating process
• Producer-Consumer problem
– Producer process: produces items that is
consumed by consumer process.
– Consumer process: Consume the produced
item.
• Client-server paradigm
Producer-Consumer problem
• One solution to the problem is shared memory.
• The producer and consumer must be
synchronized, so that the consumer does not
try to consume an item that has not yet been
produced.
Shared buffer
P1: P2:
Producer Consumer
process process
Solution
Producer process Consumer process
Int count=0; Void consumer(void)
Void producer(void) {
n=8
{ Int item_c;
out=0
int itemp; While(true)
while(true) {
{produce_item(item); While(count==0); //buffer empty
While(count==n);//buffer full Itemc=buffer(out);
Buffer[in]=item_p; Out=(out+1)mod n;
In=(in+1)mod n; Count=count-1;
Count=count+1; Count= Process_item(itemc);
0
} }
in=0
} }
• Race condition: a situation where
multiple process access and manipulate
the same data concurrently and the
outcome of the execution depends on the
particular order in which they access is
called race condition.
• Such problem can be solved by process
synchronization.
The Critical-Section Problem
• n processes all competing to use some shared
data.
• Each process has a code segment, called
critical section, in which the shared data is
accessed.
• Problem – ensure that when one process is
executing in its critical section, no other process
is allowed to execute in its critical section.
Structure of process P
repeat
entry section
critical section
exit section
reminder section
until false;
Entry section
CS
Exit section
NCS
Entry section
Mutual exclusion
Progress
CS
Bounded waiting
Exit section
}
Two process solution
(Algorithm 1)
Turn=0
Process 1 Process 2
While(1) While(1)
{ {
while(turn!=0); while(turn!=1);
CS CS
turn=1; turn=0;
remainder section; remainder section;
} }
Repeat
{
Flag[i]=T;
Turn=j; a. flag[j]=T && turn=i
While(?); b. Flag[j]=T && turn=j
CS c. Flag[i]=T && turn=j
Flag(i)=F; d. Flag[i]=T && turn=i
Remainder section.
}
Until false
Critical section Solutions
CS Solutions
▪ Lock Variables
▪ Test & set Inst. ▪ Counting Semaphore
▪ Strict alteration ▪ Monitors
(TSL) ▪ Binary semaphore
▪ Peterson algorithm
Lock Variables
While(1)
Entry section: Lock
While(lock == 1); 0 1
Lock=1;
CS CS CS
available Busy
Exit section
lock=0;
• Binary semaphore
Counting semaphore
• A counting semaphore comprises of:
Wait(semaphore s) Signal(semaphore s)
{ {
s.val=s.val -1; s.val=s.val +1;
if(s.val<0) if(s.val<=0)
{ {
Put the process in list L; select a process from L;
sleep(); wakeup();
} }
Else }
return ;
}
Binary semaphore
• Also called mutex.
• It is an integer variable which can be
accessed by a cooperating process,
through two atomic operation ‘wait’ and
‘signal’.
• It is initialized by OS to 1 and it can
assume only one of the process two value
(either 1 or 0).
Implementation
Struct semaphore
{ enum val(0,1);
Queue type L;
}
Wait(semaphore s)
Signal(semaphore s)
{
{
if(s.val==1)
if(S.L is empty)
{
{
s.val=0;
s.val=1;
}
}
else
else
{
{
Put the process in list L; sleep();
select a process from L;
}
wakeup();
}
}
}
Analysis
• Binary semaphore strictly follow mutual
exclusive.
• Achieved progress.
• Achieved bounded waiting.
• A counting semaphore S is initialized to 10.
Then, 6 P operations and 4 V operations are
performed on S. What is the final value of S?
[8].
• A counting semaphore S is initialized to 7.
Then, 20 P operations and 15 V operations are
performed on S. What is the final value of S?
[2]
•
Highest response ratio next (HRRN)
RR = (W+S)/S
Where,
W=It indicates the Waiting
Time.
do{
//produce an item
wait(empty);
wait(mutex);
//place in buffer
signal(mutex);
signal(full);
}while(true)
Dining-Philosphers Problem
• The Dining Philosopher Problem states that K
philosophers seated around a circular table with
one chopstick between each pair of philosophers.
There is one chopstick between each philosopher.
A philosopher may eat if he can pickup the two
chopsticks adjacent to him. One chopstick may be
picked up by any one of its adjacent followers but
not both. This problem involves the allocation of
limited resources to a group of processes in a
deadlock-free and starvation-free manner.
Dining Philosphers
Thinking Eating
When a
When a philosopher
philosopher thinks,
gets hungry, He
does not require
tries to pick up two
any forks and does
fork that are closet
not interact with
(Left & right)
colleagues.
Each philosopher is represented by the following
pseudocode
process P[i]
while true do
{ THINK;
PICKUP(CHOPSTICK[i],CHOPSTICK[i+1 mod 5]);
EAT;
PUTDOWN(CHOPSTICK[i], CHOPSTICK[i+1 mod 5])
}
Solution
semaphore chopstick [5];
do {
wait( chopstick[i] );
wait( chopstick[ (i+1) % 5] );
..
. EATING .
signal( chopstick[i] );
signal( chopstick[ (i+1) % 5] );
.
. THINKING
.
} while(1);
Readers and Writers Problem
Suppose that a database is to be shared among several concurrent processes. Some of
these processes may want only to read the database, whereas others may want to
update (that is, to read and write) the database. We distinguish between these two
types of processes by referring to the former as readers and to the latter as writers.
Precisely in OS we call this situation as the readers-writers problem. Problem
parameters:
• One set of data is shared among a number of processes.
• Once a writer is ready, it performs its write. Only one writer may write at a time.