Process synchronization
• What is inter process communication (IPC)? : Can be done through files or
by function through global variables
1. # Problems due to lack of synchronization
- Inconsistency
- Data loss
-Deadlock
P1 P2
2. # Type of synchronization +1 -1
-Competitive Prob: (Inconsistency and Data loss) 3
- co-operative Prob: (Deadlock) C
Note: An application in an IPC environment may involve either or both type
of synchronization.
Two or more processes are said to be in competition iff they compete for the accessibility of shared resource.
Here Processes 1 and 2 tries to update the value of a shared variable ‘C’ at the same time.
Two or more processes are said to be in cooperation iff they get affected by each other i.e. the execution of
one process affect the execution of other process. Exp: Producer-Consumer Problem.
Lack of cooperative synchronization among processes may lead to the problem of DEADLOCK.
• Illustration of the problem (Producer and Consumer):
Suppose that we wanted to provide a solution to the producer-consumer problem that fills
all the buffers.
We can do so by having an integer counter that keeps track of the number of full
buffers. Initially, counter is set to 0.
It is incremented by the producer after it produces a new buffer and is decremented by
the consumer after it consumes a buffer.
• Processes can execute concurrently.
• May be interrupted at any time, partially completing execution.
• Concurrent access to shared data may result in data inconsistency.
• Maintaining data consistency requires mechanisms to ensure the
orderly execution of cooperating processes
Producer and Consumer +1 -1 C
P
Problem: 0
counter
Both processes access this memory. slots
In operating System Producer is a
process which is able to produce data/item.
#define N 100 /Global Declarations
Int Buffer [N]; /*shared by P&C in Fixed size Buffer out
Int count =0; /*shared by P&C
In operating System consumer is a process
Void producer (void) which is able to consume data/item.
{ Void consumer (void)
int temp, in=0; {
While (True) int temp, out=0;
{ While (True)
/* produce an item in temp */ {
produce item (item p); while (count == 0) ;
while (count = = N); Co-operation item c = Buffer [out];
buffer[in] = temp; out = (out + 1)%N
in = (in + 1)%N count= count-1;
count= count+1; Process item [item c]
} }
} }
IN indicates the location where the item suppose to
be placed in NEXT. “IN” is locating already next Competition
position. So, this sequence is right to place items.
SETUP:
I. Load RP, M[count] I. Load RC, M[count] Ti RP : I, II, Preempt
II. Inc, RP II. Dcr, RC Tj RC : I, II, III
III. SM[Count], RP III. SM[Count], RC Tk RP : III
Schedule /Reschedule
CPU
+1
RQ RP RC 6 5 RP
Inconsistency
-1 4 5 RC
Preempt
Producer Consumer Problem
• Producer-Consumer problem is a classical synchronization problem in the
operating system. With the presence of more than one process and
limited resources in the system the synchronization problem arises.
• If one resource is shared between more than one process at the
same time then it can lead to data inconsistency.
• In the producer-consumer problem, the producer produces an item
and the consumer consumes the item produced by the producer.
• Both Producer and Consumer share a common memory buffer. This buffer
is a space of a certain size in the memory of the system which is used for
storage. The producer produces the data into the buffer and the consumer
consumes the data from the buffer.
• Accessing memory buffer should not be allowed to producer and
consumer at the same time.
• first, understand the few terms used in the code:
• "in" used in a producer code represent the next empty buffer.
• "out" used in consumer code represent first filled buffer.
• count keeps the count number of elements in the buffer.
3. # Causes responsible for synchronization problems (Necessary conditions)
critical part of program where shared resources are accessed.
section (CS): (NonCS: which does not consist shared resources)
A program in IPC may involve or may have a series of CS and NonCS
Race Process must be racing either competitively or cooperatively to access
condition: CS,
exp: P&C problem, tries to update the count at the same time.
Preemption
P1 P2 P3
CS
Principal of Mutual Exclusion (POME):
• If a process is present in C.S., other process should not be allowed to enter the
C.S. (should be waiting outside the C.S.)
• POME is ensured by syn. Mechanism or synchronization Tool, placed between
processes and C.S. waiting
P1 P2 P3
ENTRY To notify by Exit
POME
mechanism
CS SYN.
Mngr
EXIT P1 Exit
Classification of synchronization Mechanism:
Mutual Exclusion Mutual Exclusion
with Busy Waiting with out Busy Waiting
Spin Lock Blocking
Software solution Hardware solution OS based solution
• Lock variable • Test and Set Lock (TSL) • Semaphore
• Peterson • Swap
• Strict alteration
Solution Processes Practical
Lock variable N
Strict
2
alternation
Peterson’s 2
Mutex/Semaph More
N
ore
Requirement of Synchronization Mechanism for
solving Critical Section Problem
1. Mutual Exclusion: must always be guaranteed. //(else inconsistency/data loss)
• “All processes are interested”
2. Progress-
• “Some processes are interested”
• Def1: No process running outside CS should block the other interested
processes from entering CS.
• Def.2: Processes running outside CS (Non-CS) should not participate in decision
making of allowing a process to enter CS, among those who are interested.
waiting waiting
P1 P2 P3 P1 P2 P3
interested interested
P4 Not
ENTRY ENTRY interested
POME POME P5
P1 P1
CS CS
EXIT Exit EXIT Exit
Cont.
3. Bounded Waiting-
• “All processes are interested”
• Def1: No process has to wait forever to access its CS.
• Def.2: There should be a bound on number of times a process is
allowed to enter CS before the other waiting process’s request is
granted.
waiting
P1 P2 P3
Leading to starvation
ENTRY of processes P2 and P3
POME
Again run CS
P1
P1
EXIT
Lock Variable
• ME with Busy waiting &S/W solution
• Multi process solution, ˃2 processes
• Busy waiting solutions are always implemented using loop
mechanism for repeatedly checking.
• Int Lock =0;
P1 P2 P3 • Void process (int i)
• {
0 CS is free
ENTRY Lock • While (1)
variable CS is in use Busy
1 • {
waiting
• a) Non-CS ();
CS • b) While (Lock!=0);
ENTRY
• c) Lock=1
• d) <C.S.>
EXIT
EXIT • e) Lock =0;
• }
• }
P1 P2
Cont. 0 CS is free
ENTRY Lock
CS is in use
• ME guaranteed no, variable
1
only if No preemption
• Progress guaranteed yes CS
• BW guaranteed no (in either case
of Pre/Non-Preemption)
EXIT
SETUP:
Lock
• I. load Ri, M [Lock] RQ P1 P2
0
• II. Compare Ri, #0;
• III. Jump IF not Zero, Step I 0 P1
R1
• IV. Store M[Lock], #1; P2 • ti : P1 : I, II, III
R2 0 C.S.
• V. <CS> • tj: P2 : I, II, III, IV, V
• VI. Store M[Lock], #0 • tk: P1 : IV, V
Which process will enter to CS, first ?
Strict Alteration
• ME with Busy waiting &S/W solution
• 2 process solution (P0, P1; Pi, Pj)
• Strictly on alternate basis, processes takes turn to enter C.S.
• Int turn = rand (0,1);
P0’s turn to enter CS P1’s turn to enter CS • Void process (int i)
0 1
• {
P1 P2 • While (1)
ENTRY • {
Turn
• a) Non-CS ();
ENTRY • b) While (turn!=i);
CS • c) <C.S.>
EXIT • d) turn =j;
• }
EXIT • ME guaranteed yes • }
• Progress guaranteed no
• BW guaranteed yes
First enter CS then after coming out change the value of ID unlike LOCK variable.
Observations
• It does not guarantee Progress due to the fact that a process which is
not interested in CS having its ID set to turn prevent the other
interested process from accessing CS.
• In general, an interested process must be allowed to enter CS anytime
and any number of times as long as CS is free and other processes are
not interested.
Another solution
P0 P1
• While (1) • While (1)
• { • {
• flag [0] =T • flag [1] =T
• While (flag [1]); • While (flag [0]);
• <C.S.> • <C.S.>
• flag [0] =F • flag [1] =F
• It will lead to deadlock if both becomes interested at same time