0% found this document useful (0 votes)
5 views25 pages

Process Synchronization-1

The document discusses process synchronization and interprocess communication (IPC) in operating systems, highlighting the importance of cooperating processes and the need for synchronization mechanisms. It details various models of IPC, the critical section problem, and solutions such as semaphores and Peterson's solution. Additionally, it covers classical synchronization problems like the Producer-Consumer problem, Readers-Writers problem, and Dining-Philosophers problem.

Uploaded by

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

Process Synchronization-1

The document discusses process synchronization and interprocess communication (IPC) in operating systems, highlighting the importance of cooperating processes and the need for synchronization mechanisms. It details various models of IPC, the critical section problem, and solutions such as semaphores and Peterson's solution. Additionally, it covers classical synchronization problems like the Producer-Consumer problem, Readers-Writers problem, and Dining-Philosophers problem.

Uploaded by

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

12-1

Process
Synchronization
Interprocess Communication 12-2

 Processes within a system may be independent or cooperating

 Cooperating process can affect or be affected by other processes, including


sharing data

 Reasons for cooperating processes:


Information sharing
Computation speedup
Resources
Shared Code
Variables
 Cooperating processes need interprocess communication (IPC)
 Two models of IPC
Shared memory
Message passing
12-3
Communications Models
Concurrent Processes 12-4

Two processes are said to be concurrent if they over lap in their


execution.
12-5
Introduction of Process Synchronization
 counter++ could be implemented as:
register1 = counter
register1 = register1 + 1
counter = register1

 counter-- could be implemented as:


register2 = counter
register2 = register2 – 1
counter = register2
Consider this execution interleaving with “count = 5” initially:

 S0: producer execute register1 = counter {register1 = 5}


 S1: producer execute register1 = register1 + 1 {register1 = 6}
 S2: consumer execute register2 = counter {register2 = 5}
 S3: consumer execute register2 = register2 – 1 {register2 = 4}
 S4: producer execute counter = register1 {counter = 6 }
 S5: consumer execute counter = register2 {counter = 4}
Race Condition
Critical Section Problem 12-6

 Consider system of n processes {p0, p1, … pn-1}


 Each process has critical section segment of code
 Process may be changing common variables, updating table,
writing file, etc
 When one process in critical section, no other may be in its
critical section
 Critical section problem is to design protocol to solve this
 Each process must ask permission to enter critical section in
entry section, may follow critical section with exit section, then
remainder section
 Critical section is a code segment that can be accessed by only
one process at a time.
 Critical section contains shared variables which need to be
synchronized to maintain consistency of data variables.
Critical Section Problem 12-7
Critical Section Problem 12-8

Any solution to the critical section problem must satisfy three requirements:
Mutual Exclusion: If a process is executing in its critical section, then no
other process is allowed to execute in the critical section.
Progress : If no process is executing in the critical section and other
processes are waiting outside the critical section, then only those
processes that are not executing in their remainder section can participate
in deciding which will enter in the critical section next, and the selection
cannot be postponed indefinitely.
Bounded Waiting: A bound must exist on the number of times that other
processes are allowed to enter their critical sections after a process has
made a request to enter its critical section and before that request is
granted.
Two process solution for critical
section 12-9

First Solution: Boolean turn=0;

P0 P1
while(1) while(1)
{ {
While(turn!=0); While(turn!=1);
Critical Section
Critical Section

Turn=1; Turn=0;
Remainder
RemainderSection
Section Remainder Section

}
}
12-10

 Solution first is following Mutual Exclusion and fails to

follow Progress as both process spin in some order that

is both process can be executed one by one. Any

Process P0 or P1 cannot be executed two times

sequentially.
Two process solution for critical
section 12-11

F F
Second Solution: Boolean Flag[2]=
0 1

P0 P1
while(1) while(1)
{ {
Flag[0]= T; Flag[1]= T;
while(flag[1]); while(flag[0]);
Critical Section Critical Section

flag[0]=F; flag[1]=F;

Remainder Section Remainder Section

} }
Peterson’s Solution 12-12

 Peterson’s Solution is a classical software based solution to the

critical section problem.

 In Peterson’s solution, we have two shared variables:

 boolean flag[i]: Initialized to FALSE, initially no one is

interested in entering the critical section

 int turn : The process whose turn is to enter the critical section.
Peterson’s Solutions 12-13

i = 1-j
Two process solution for critical
section 12-14

F F
Second Solution: Boolean Flag[2]=
0 1

P0 P1
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);
Critical Section Critical Section

flag[0]=F; flag[1]=F;
Remainder Section Remainder Section

} }
Semaphore 12-15

 Semaphore was proposed by Dijkstra in 1965 to manage concurrent


processes by using a simple integer value, known as a semaphore.
 Semaphore is simply an integer variable which is non-negative and
shared between threads.
 This variable is used for critical section in the multiprocessing
environment.
 It uses two basic function wait(S) and Signal(S).
wait (S):
while S 0 do no-op;
S--;
signal (S):
S++;
Critical Section of n Processes 12-16

 Shared data:
semaphore mutex; //initially mutex = 1

 Process Pi: wait (S):


do { while S 0 do no-
op;
wait(mutex); S--;
critical section
signal (S):
signal(mutex);
S++;
remainder section
} while (1);
Two Types of Semaphores 12-17

 Binary Semaphore – This is also known as mutex lock. It


can have only two values – 0 and 1. Its value is initialized to
1. It is used to implement the solution of critical section
problem with multiple processes.
 Counting Semaphore – Its value can range over an
unrestricted domain. It is used to control access to a
resource that has multiple instances
 Can implement a counting semaphore S as a binary
semaphore.
Example 12-18

 There are TWO Processes P1 with Statement S1 and P2 with


S2
 CONDITION : S2 be executed only after S1. ( S1 S2)

Semaphore synch0;
_______________________
S1;
Signal(synch);
_______________________
Wait(synch);
S2;
________________________
12-19
Classical Problems of Synchronization
 Producer Consumer with Bounded-Buffer Problem

 Readers and Writers Problem

 Dining-Philosophers Problem

Operating System Concepts


Producer Consumer with Bounded-Buffer 12-20

 We have a buffer of fixed size.


 A producer can produce an item and can place in the buffer.
 A consumer can pick items and can consume them.
 We need to ensure that when a producer is placing an item in
the buffer, then at the same time consumer should not consume
any item.
 In this problem, buffer is the critical section.
 To solve this problem, two counting semaphores – Full and
Empty.
 “Full” keeps track of number of items in the buffer at any given
time and “Empty” keeps track of number of unoccupied slots.
Producer Consumer with Bounded-Buffer 12-21

 Shared data : semaphore full, empty, mutex;


Initially: full = 0, empty = n, mutex = 1

do { do {
… wait(full)
produce an item in wait(mutex);

… remove an item from buffer to
wait(empty); nextc
wait(mutex); …
… signal(mutex);
add nextp to buffer signal(empty);
… …
signal(mutex); consume the item in nextc
signal(full); …
} while (1); } while (1);
Readers-Writers Problem 12-22

 semaphore mutex, wrt;


Initially mutex = 1, wrt = 1, readcount = 0
wait(mutex);
readcount++;
wait(wrt);
if (readcount == 1)

wait(rt);
writing is signal(mutex);

… reading is performed
signal(wrt); …
wait(mutex);
readcount--;
if (readcount == 0)
signal(wrt);
signal(mutex):
Dining-Philosophers Problem 12-23

 Shared data
semaphore chopstick[5];
Initially all values are 1
Dining-Philosophers Problem 12-24

 Philosopher i:
do {
wait(chopstick[i])
wait(chopstick[(i+1) % 5])

eat

signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);

think

} while (1);
12-25

You might also like