0% found this document useful (0 votes)
2 views53 pages

Process Synch

The document discusses process coordination, distinguishing between independent and cooperating processes, and highlights the importance of inter-process communication (IPC) mechanisms such as shared memory and message passing. It addresses critical issues like race conditions, the critical-section problem, and various synchronization solutions including semaphores and algorithms for mutual exclusion. Additionally, it covers classical synchronization problems like the Producer-Consumer, Dining Philosophers, and Readers-Writers problems, providing insights into their solutions and challenges.

Uploaded by

ATR King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views53 pages

Process Synch

The document discusses process coordination, distinguishing between independent and cooperating processes, and highlights the importance of inter-process communication (IPC) mechanisms such as shared memory and message passing. It addresses critical issues like race conditions, the critical-section problem, and various synchronization solutions including semaphores and algorithms for mutual exclusion. Additionally, it covers classical synchronization problems like the Producer-Consumer, Dining Philosophers, and Readers-Writers problems, providing insights into their solutions and challenges.

Uploaded by

ATR King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

PROCESS COORDINATION

Dr. Indrajeet Kumar


Department of CSE
Graphic Era Hill University
Dehradun
Independent Process
• A process is independent if it can’t affect
or affected by the other process executing
in the system.

• Any process that does not share data with


any other process is independent process.
Cooperating Process
• A process is said to be cooperating if it
can affect or to be affected by the other
processes executing in the system.

• Any process that share data with the other


process is called cooperating process.
• There are several reasons for providing an
environment that allows process
cooperation:
– Information sharing

– 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;

Critical section: It is the part of the


program where shared resources are
accessed by process.
• A solution to the critical section problem
must satisfy the following requirements:
– Mutual exclusion
– Progress
– Bounded waiting
• Mutual exclusion: if process p is
executing in its critical section, then no
other process can be executing in their
critical sections.
• Progress: only those process that are not
executing their remainder section can
participate in deciding which will enter its
critical section next, and this selection
can’t be postponed indefinitely.
• Bounded waiting: There exist a bound on
the number of times that other process are
allowed to enter their CS after a process
has made a request to enter their CS.
Program structure
P1 {
NCS

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;
} }

ME: achieved; Progress: Not; Bounded waiting: achieved.


Algorithm 2
Flag[F,F]
Process 1 Process 2
While(1) While(1)
{ {
flag[0]=T; flag[1]=T;
while(flag[1]); while(flag[0]);
CS CS
flag[0]=F; flag[1]=F;
remainder section; remainder section;
} }

ME: achieved; Progress: Not; Bounded waiting: achieved.


Two process solution
• Algorithm 1:
– Mutual exclusion achieved
– Progress not achieved.
• Algorithm 2:
– Mutual exclusion achieved
– Progress is achieved up to some extend but
finally process goes into deadlock, so finally
progress is again not achieved.
Two process solution
• Peterson algorithm
– Combination of algorithm 1 and algorithm 2.
– Uses turn and flag variables.
– Achieved mutual exclusion, progress and
bounded waiting.
Peterson algorithm
Turn=0 Flag[F,F]
Process 0 Process 1
While(1) While(1)
{ {
flag[0]=T; flag[1]=T;
turn=1; turn=0;
while((turn==1)&&(flag[1]==T)); while((turn==0)&&(flag[0]==T));
CS CS
flag[0]=F; flag[1]=F;
remainder section; remainder section;
} }

ME: achieved; Progress: achieved; Bounded waiting: achieved.


Consider the method used by the process p1, p2
for accessing CS is given below. Initial value of
shared Boolean variable s1, s2 is randomly
assigned. (G-2010)
p1 p2
While(s1==s2); While(s1!=s2);
CS CS
s1=s2; s1=not(s2);

a. ME and progress b. ME and no progress


c. No ME & Progress d. No ME & No Progress
Process x Process y
While(T) While(T)
{ {
Var P=T; Var Q=T;
While(var Q==T); While(var P==T);
CS CS
Var P=F; Var Q=F;
} }

a. No ME and no deadlock b. ME and no deadlock


c. No ME & deadlock d. ME & deadlock
Consider the segment of code (G-2001)

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

S/W H/W OS Prog. Lang.


Solutions Solutions Solutions 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;

NO Mutual exclusion is achieved.


Test & Set Lock (TSL)
• TSL: copies the While(1)
Entry section:
current value of While(test_and_set(&lock));
CS
flag into the Exit section
register and store lock=0;
values of ‘1’ into
the flag in single
atomic cycle
without any pre- Mutual Exclusion: Achieved
Progress: Achieved
emption.
Summary Table
Solutions Mutual Exclusion Progress Bounded waiting

Lock Variables NO YES NO

Strict alteration YES NO YES

Peterson Algo. YES YES YES

TSL YES YES NO


Semaphore
• Semaphore is an integer variable which is
used by various process in a mutual
exclusive manner to achieve
synchronization.
• Two atomic operations are used:
– Wait()/down()/P(): decrease the value by 1.
– Signal()/up()/V(): Increase the value by 1.
Implementation
Wait(semaphore s) Signal(semaphore s)
{ {
s.val=s.val -1; s.val=s.val +1;
if(s.val<0) if(s.val<=0)
{ {
block the process and select a process from list
place its in suspended list and wakeup().
and sleep(). }
} }
}
• After performing wait() operation, if the
process is getting suspended, then it is
called unsuccessful wait operation.

• If wait() is unsuccessful, then process will


not continue the execution.

• if(s.val<0) then wait is unsuccessful.

• EX: s= -7: already 7 process suspended.


• After performing wait() operation, if the
process is not getting suspended then it is
called successful wait() operation.

• If successful wait() operation then it will


continue the execution.

• If(s.val>=0) then wait is successful.

• EX: s= 7: we can perform 7 successful


wait() operation.
Semaphore
• Semaphore is an integer variable which is
used by various process in a mutual
exclusive manner to achieve
synchronization.
• Two atomic operations are used:
– Wait()/down()/P(): decrease the value by 1.
– Signal()/up()/V(): Increase the value by 1.
Type of semaphore
• Counting semaphore

• Binary semaphore
Counting semaphore
• A counting semaphore comprises of:

– An integer variable , initialized to a value k


(k>=0).

– A pointer to a process queue which holds the


list of suspended process. This queue is
implemented on FCFS order.
Implementation
Struct semaphore
{ Int val;
Queue type L;
}

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.

S=It indicates the Service


time that is Burst Time.
Classical Problems of Synchronization

1. Bounded Buffer (Producer-Consumer) Problem

2. Dining Philosophers Problem

3. The Readers Writers Problem

4. Sleeping Barber Problem


Bounded Buffer Problem
• This problem is generalized in terms of the Producer
Consumer problem, where a finite buffer pool is used
to exchange messages between producer and
consumer processes.

• Because the buffer pool has a maximum size, this


problem is often called the Bounded buffer problem.

• Solution to this problem is, creating two counting


semaphores "full" and "empty" to keep track of the
current number of full and empty buffers respectively.
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
} }
Initialization of semaphores –
mutex = 1
Full = 0 // Initially, all slots are empty. Thus, full slots are 0
Empty = n // All slots are empty initially

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.

• If a process is writing, no other process can read it.

• If at least one reader is reading, no other process can write.

• Readers may not write and only read.


Sleeping Barber Problem
• Barber shop with one barber, one barber chair and N chairs to
wait in.

• When no customers the barber goes to sleep in barber chair


and must be woken when a customer comes in.

• When barber is cutting hair new customers take empty seats


to wait, or leave if no vacancy.
Thank you

You might also like