MODULE 3.1 - Process Synchronization
MODULE 3.1 - Process Synchronization
Module 3
Process Synchronization
Outline
Interprocess Communication
Race condition
Critical Section
Semaphores
Interprocess Communication
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
Modularity
Convenience
Cooperating processes need interprocess communication (IPC)
Two models of IPC
Shared memory
Message passing
Operating System Concepts – 9th Edition 3.3 Silberschatz, Galvin and Gagne ©2013
Communications Models
(a) Message passing. (b) shared memory.
Operating System Concepts – 9th Edition 3.4 Silberschatz, Galvin and Gagne ©2013
Interprocess Communication – Shared Memory
Operating System Concepts – 9th Edition 3.5 Silberschatz, Galvin and Gagne ©2013
Critical section
Operating System Concepts – 9th Edition 3.6 Silberschatz, Galvin and Gagne ©2013
Critical section
Operating System Concepts – 9th Edition 3.7 Silberschatz, Galvin and Gagne ©2013
Critical section
Operating System Concepts – 9th Edition 3.8 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 3.9 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 3.10 Silberschatz, Galvin and Gagne ©2013
Critical Section Problem
Operating System Concepts – 9th Edition 3.11 Silberschatz, Galvin and Gagne ©2013
Critical Section
Operating System Concepts – 9th Edition 3.12 Silberschatz, Galvin and Gagne ©2013
Algorithm for Process Pi
do {
critical section
turn = j;
remainder section
} while (true);
Operating System Concepts – 9th Edition 3.13 Silberschatz, Galvin and Gagne ©2013
Solution to Critical-Section Problem
Operating System Concepts – 9th Edition 3.14 Silberschatz, Galvin and Gagne ©2013
Peterson’s Solution
Operating System Concepts – 9th Edition 3.15 Silberschatz, Galvin and Gagne ©2013
Algorithm for Process Pi
Operating System Concepts – 9th Edition 3.16 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 3.17 Silberschatz, Galvin and Gagne ©2013
Peterson’s Solution (Cont.)
Operating System Concepts – 9th Edition 3.18 Silberschatz, Galvin and Gagne ©2013
Bakery Algorithm in Process Synchronization
Operating System Concepts – 9th Edition 8.19 Silberschatz, Galvin and Gagne ©2013
Bakery Algorithm in Process Synchronization
Operating System Concepts – 9th Edition 8.20 Silberschatz, Galvin and Gagne ©2013
Bakery Algorithm in Process Synchronization
lock(i);
critical section
unlock(i);
Remainder section
Operating System Concepts – 9th Edition 8.21 Silberschatz, Galvin and Gagne ©2013
Bakery Algorithm in Process Synchronization
Operating System Concepts – 9th Edition 8.22 Silberschatz, Galvin and Gagne ©2013
Bakery Algorithm in Process Synchronization
Operating System Concepts – 9th Edition 8.23 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 8.24 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 8.25 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 8.26 Silberschatz, Galvin and Gagne ©2013
Bakery Algorithm
Operating System Concepts – 9th Edition 8.27 Silberschatz, Galvin and Gagne ©2013
Bakery Algorithm
Firstly the process sets its “choosing” variable to be TRUE indicating its
intent to enter critical section.
Then it gets assigned the highest ticket number corresponding to other
processes. Then the “choosing” variable is set to FALSE indicating that it
now has a new ticket number. This is in-fact the most important and
confusing part of the algorithm. It is actually a small critical section in itself
!
The very purpose of the first three lines is that if a process is modifying its
TICKET value then at that time some other process should not be allowed
to check its old ticket value which is now obsolete. This is why inside the
for loop before checking ticket value we first make sure that all other
processes have the “choosing” variable as FALSE.
After that we proceed to check the ticket values of processes where
process with least ticket number/process id gets inside the critical section.
The exit section just resets the ticket value to zero.
Operating System Concepts – 9th Edition 8.28 Silberschatz, Galvin and Gagne ©2013
Bakery Algorithm
Operating System Concepts – 9th Edition 8.29 Silberschatz, Galvin and Gagne ©2013
Bakery Algorithm
Operating System Concepts – 9th Edition 8.30 Silberschatz, Galvin and Gagne ©2013
Bakery Algorithm
do {
choosing[i] = true;
number[i] = max(number[0], number[1], …, number [n – 1])+1;
choosing[i] = false;
for (j = 0; j < n; j++) {
while (choosing[j]) ;
while ((number[j] != 0) && (number[j,j] < number[i,i])) ;
}
critical section
number[i] = 0;
remainder section
} while (1);
Operating System Concepts – 9th Edition 8.31 Silberschatz, Galvin and Gagne ©2013
Mutex Locks
Operating System Concepts – 9th Edition 8.32 Silberschatz, Galvin and Gagne ©2013
Lock Variable
Operating System Concepts – 9th Edition 8.33 Silberschatz, Galvin and Gagne ©2013
Synchronization Hardware
.
Operating System Concepts – 9th Edition 8.34 Silberschatz, Galvin and Gagne ©2013
Mutual Exclusion with Test-and-Set
Shared data:
boolean lock = false;
Process Pi
do {
while (TestAndSet(lock)) ;
critical section
lock = false;
remainder section
}
Operating System Concepts – 9th Edition 8.35 Silberschatz, Galvin and Gagne ©2013
Synchronization Hardware
Operating System Concepts – 9th Edition 8.36 Silberschatz, Galvin and Gagne ©2013
Mutual Exclusion with Swap
Process Pi
do {
key = true;
while (key == true)
Swap(lock,key);
critical section
lock = false;
remainder section
Operating System Concepts – 9th Edition
} 8.37 Silberschatz, Galvin and Gagne ©2013
Semaphore
Synchronization tool that provides more sophisticated ways (than Mutex locks) for process
to synchronize their activities.
Semaphore S – integer variable
Can only be accessed via two indivisible (atomic) operations
wait() and signal()
Originally called P() and V()
Must guarantee that no two processes can execute the wait() and signal() on the
same semaphore at the same time
Thus, the implementation becomes the critical section problem where the wait and
signal code are placed in the critical section
Operating System Concepts – 9th Edition 8.39 Silberschatz, Galvin and Gagne ©2013
Classical Problems of Synchronization
Bounded-Buffer Problem
Dining-Philosophers Problem
Operating System
Operating System Concepts – 9th Edition 8.40 Silberschatz, Galvin and Gagne ©2013
Bounded-Buffer Problem
Shared data
N=8
Counting semaphore full – no. of filled slots A
Initially: E
Operating System Concepts – 9th Edition 8.41 Silberschatz, Galvin and Gagne ©2013
Bounded-Buffer Problem
Producer Process Consumer Process
do { do {
… wait(full);
produce an item in nextp wait(mutex);
… …
wait(empty); //remove an item from buffer to nextc
wait(mutex); nextc = Buffer[OUT];
… OUT = (OUT+1) % N
//add nextp to buffer …
Buffer[IN] = nextp; signal(mutex);
IN = (IN+1) % N signal(empty);
… …
signal(mutex); //consume the item in nextc
signal(full); …
} while (1); } while (1);
Operating System Concepts – 9th Edition 8.42 Silberschatz, Galvin and Gagne ©2013
Readers-Writers Problem
Shared data
Initially
mutex = 1, wrt = 1,
int readcount = 0
Operating System Concepts – 9th Edition 8.43 Silberschatz, Galvin and Gagne ©2013
Readers-Writers Problem Reader Process
semaphore mutex = 1;
semaphore wrt = 1;
int readcount = 0;
void Reader(){
void Writer(){
wait(mutex);
wait(wrt);
readcount++;
if (readcount == 1) …
wait(wrt); writing is performed
signal(mutex); …
… signal(wrt);
reading is performed }
…
wait(mutex);
readcount--;
if (readcount == 0)
signal(wrt);
signal(mutex);
}
Operating System Concepts – 9th Edition 8.44 Silberschatz, Galvin and Gagne ©2013
Dining-Philosophers Problem
Shared data
semaphore chopstick[5];
Initially all values are 1
Operating System Concepts – 9th Edition 8.45 Silberschatz, Galvin and Gagne ©2013
Dining-Philosophers Problem
Philosopher i:
do {
wait(chopstick[i])
wait(chopstick[(i+1) % 5])
…
eat
…
signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);
…
think
…
} while (1);
Operating System Concepts – 9th Edition 8.46 Silberschatz, Galvin and Gagne ©2013
Classical synchronization problems
Producer consumer problem
Producer consumer problem
Producer consumer problem
Producer consumer problem
Producer consumer problem
Dining philosophers problem
Dining-Philosophers Problem
Dining-Philosophers Problem
Dining-Philosophers Problem
Dining-Philosophers Problem
Dining-Philosophers Problem
Dining-Philosophers Problem
Dining-Philosophers Problem
Monitors Operating
System
Concepts
High-level synchronization construct that allows the safe sharing of an
abstract data type among concurrent processes.
monitor monitor-name
{
shared variable declarations
procedure body P1 (…) {
...
}
procedure body P2 (…) {
...
}
procedure body Pn (…) {
...
}
{
initialization code
}
}
Monitors Operating
System
Concepts
x-count++;
if (next-count > 0)
signal(next);
else
signal(mutex);
wait(x-sem);
x-count--;
Monitor Implementation Operating
System
Concepts
if (x-count > 0) {
next-count++;
signal(x-sem);
wait(next);
next-count--;
}
Monitor Implementation Operating
System
Concepts
Conditional-wait construct: x.wait(c);
c – integer expression evaluated when the wait operation is
executed.
value of c (a priority number) stored with the name of the process
that is suspended.
when x.signal is executed, process with smallest associated priority
number is resumed next.
Check two conditions to establish correctness of system:
User processes must always make their calls on the monitor in a
correct sequence.
Must ensure that an uncooperative process does not ignore the
mutual-exclusion gateway provided by the monitor, and try to access
the shared resource directly, without using the access protocols.
References 71