Professional Documents
Culture Documents
Synchronization
Background
Concurrent Processes
Inter process Communication
Producer Consumer Problem
The Critical-Section Problem
Semaphores
Classical Problems of Synchronization
Operating System Concepts – 8th Edition 6.1 Silberschatz, Galvin and Gagne ©2009
Background
Operating System Concepts – 8th Edition 6.2 Silberschatz, Galvin and Gagne ©2009
Concurrent Processing
Concurrent processes means processes existing at the same
time
Concurrent processes may be
independent of each other or
cooperating each other
Independent process cannot affect or be affected by the
execution of another process.
Cooperating process can affect or be affected by the execution
of another process
Operating System Concepts – 8th Edition 6.3 Silberschatz, Galvin and Gagne ©2009
Interleaving of Concurrent Processes on a
Single CPU
Operating System Concepts – 8th Edition 6.4 Silberschatz, Galvin and Gagne ©2009
Interleaving and Overlapping of Concurrent Processes
with Multiprocessing
Operating System Concepts – 8th Edition 6.5 Silberschatz, Galvin and Gagne ©2009
Need for Process Cooperation
Information Sharing: Concurrent cooperating needs to share
information (e.g. a shared file)
Computation speedup: by breaking the task into number of
parallel tasks
Modularity: dividing the software system modules/functions,
each of which may be divided into separate processes or
threads.
Convenience: There are many tasks that a user needs to do
such as compiling, printing, editing etc. It is convenient if these
tasks can be managed by cooperating processes.
Operating System Concepts – 8th Edition 6.6 Silberschatz, Galvin and Gagne ©2009
Interprocess Communication(IPC)
• Interprocess communication(IPC) is a mechanism for
processes to synchronize and communicate with each other.
Courtesy: (https://kelvin.ink/2018/10/27/IPC_and_Synchronization/)
Operating System Concepts – 8th Edition 6.7 Silberschatz, Galvin and Gagne ©2009
Approaches for IPC
a) Shared memory
b) Message passing
Operating System Concepts – 8th Edition 6.8 Silberschatz, Galvin and Gagne ©2009
Interprocess Communication – Message Passing
Mailbox sharing
P1, P2, and P3 share mailbox A
P1, sends; P2 and P3 receive
Who gets the message?
Solutions
Allow a link to be associated with at most two processes
Allow only one process at a time to execute a receive operation
Allow the system to select arbitrarily the receiver. Sender is notified who
the receiver was.
Synchronization
Operating System Concepts – 8th Edition 6.14 Silberschatz, Galvin and Gagne ©2009
Producer-Consumer Problem
Operating System Concepts – 8th Edition 6.15 Silberschatz, Galvin and Gagne ©2009
Bounded-Buffer
buffer empty -> counter=0; //(in=out)
//Initially the pointers to buffer slots, in and out are set to zero
//Buffer is implemented as a circular array
in = (in + 1) % BUFFER_SIZE;
Operating System Concepts – 8th Edition 6.16 Silberschatz, Galvin and Gagne ©2009
Bounded-Buffer
The shared buffer is implemented as a circular array with two pointers : in and
out.
#define BUFFER_SIZE 10
typedef struct {
..
} item;
item
buffer[BUFFER_SIZE];
int in = 0; // points to the next free position in the
buffer int out = 0; // points to the first full position in
the buffer int counter = 0;
Operating System Concepts – 8th Edition 6.17 Silberschatz, Galvin and Gagne ©2009
Producer
Producer process :
item nextProduced;
while (1) {
while (counter ==
BUFFER_SIZE)
; /* buffer full do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
Operating System Concepts – 8th Edition 6.18 Silberschatz, Galvin and Gagne ©2009
Consumer
Consumer process :
item nextConsumed;
while (1) {
while (counter ==
0)
; /* buffer empty, do nothing */
nextConsumed = buffer[out];
out = (out + 1) %
BUFFER_SIZE;
}
counter--;
Operating System Concepts – 8th Edition 6.19 Silberschatz, Galvin and Gagne ©2009
Where is the problem ?
The statements
Operating System Concepts – 8th Edition 6.15 Silberschatz, Galvin and Gagne ©2009
The statement “counter++” may be implemented in machine language
as:
register1 = counter
register1 = register1 + 1
counter = register1
register2 = counter
register2 = register2 – 1
counter = register2
Operating System Concepts – 8th Edition 6.21 Silberschatz, Galvin and Gagne ©2009
Data inconsistency
If both the producer and consumer attempt to update the buffer
concurrently, the assembly language statements may get
interleaved.
Interleaving depends upon how the producer and consumer
processes are scheduled.
Example: Let the present value of Counter is 5. Then what is
the value of Counter after one execution each of producer and
consumer ?
Operating System Concepts – 8th Edition 6.22 Silberschatz, Galvin and Gagne ©2009
Race Condition
Operating System Concepts – 8th Edition 6.24 Silberschatz, Galvin and Gagne ©2009
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 (common
variables/data structures or common file) is accessed.
E.g. machine language statements for counter++ and
counter -- are the critical sections of producer and
consumer processes respectively.
Ensure that when one process is executing in its critical
section, no other process is allowed to execute in its
critical section.
Operating System Concepts – 8th Edition 6.25 Silberschatz, Galvin and Gagne ©2009
Banking Example
Operating System Concepts – 8th Edition 6.26 Silberschatz, Galvin and Gagne ©2009
Critical Section
" General structure of process Pi (other process Pj)
do {
entry section
critical section
--negotiation protocol
exit section --release
protocol
remainder section
} while (1);
Operating System Concepts – 8th Edition 6.27 Silberschatz, Galvin and Gagne ©2009
Solution to Critical-Section Problem
Must satisfy the following conditions:-
1. Mutual Exclusion. If process Pi is executing in
its critical section, then no other processes can be
executing in their critical sections.
2. Progress. No process running outside its critical
section may block other processes from entering
the critical section.
3. Bounded Waiting. No process should have to wait
forever to enter its critical section, i.e. the solution to
critical section problem should be free of starvation.
Operating System Concepts – 8th Edition 6.28 Silberschatz, Galvin and Gagne ©2009
Hardware Support for Accessing Critical Section
Pessimistic Approaches
Disable/Enable Interrupts
Test and Set Instruction
Optimistic Approach
Compare and Swap Instruction
Operating System Concepts – 8th Edition 6.29 Silberschatz, Galvin and Gagne ©2009
Disable/Enable Interrupts
Operating System Concepts – 8th Edition 6.31 Silberschatz, Galvin and Gagne ©2009
Initial Attempts to Solve Problem
Only 2 processes, P0 and P1
General structure of process Pi (other process Pj)
do {
entry section --negotiation
protocol
critical section
exit section --
release protocol
remainder section
" } while (1);
Processes may share some common variables to synchronize their
actions.
Operating System Concepts – 8th Edition 6.37 Silberschatz, Galvin and Gagne ©2009
Software Solutions
Operating System Concepts – 8th Edition 6.38 Silberschatz, Galvin and Gagne ©2009
Algorithm 1
Shared variables:
int turn;
initially turn = 0
turn = i Pi
can enter its
critical section
Process Pi
d
o
{
while (turn != i) ;
critical
section
turn = j;
remainder
section
} while (1);
Satisfies mutual exclusion,
Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition 6.39
Algorithm 2
Shared variables
boolean flag[2];
initially flag [0] = flag [1] = false.
flag [i] = true Pi ready to enter its critical section
Process Pi
do {
flag[i] := true;
while (flag[j]) ;
critical
section
flag [i] = false;
remainder
section
} while (1);
Satisfies mutual exclusion,
Removes strict turn taking restriction
progress requirement is not satisfied.(ex. Pi sets its flag and
preempted, and Pj scheduled for execution, it sets its flag also
Operating System Concepts –then
th both are deadlocked) 6.40
8 Edition Silberschatz, Galvin and Gagne ©2009
Peterson’s Algorithm for two processes
do { do {
Operating System Concepts – 8th Edition 6.41 Silberschatz, Galvin and Gagne ©2009
Semaphore
Synchronization tool proposed by Dijkstra in 1968
Used to achieve mutual exclusion among n processes
Semaphore S – integer variable (0 : busy, 1:free)
Two standard operations modify S:
wait() and signal()
Originally called P() and V()
Semaphore may be provided as a programming language construct or
as the OS service invoked via system calls or APIs.
Operating System Concepts – 8th Edition 6.45 Silberschatz, Galvin and Gagne ©2009
Semaphore operations
Can only be accessed via two indivisible (atomic)
operations
wait (S) {
while S <= 0 //busy
; // no-op-busy waiting
S--;
}
signal (S) {
S++;
}
Operating System Concepts – 8th Edition 6.46 Silberschatz, Galvin and Gagne ©2009
Semaphore as
General Synchronization Tool
Operating System Concepts – 8th Edition 6.47 Silberschatz, Galvin and Gagne ©2009
Disadvantage
Note that applications may spend lots of time in critical sections and
therefore this is not a good solution
Operating System Concepts – 8th Edition 6.48 Silberschatz, Galvin and Gagne ©2009
Queuing implementation of Semaphore
Operating System Concepts – 8th Edition 6.49 Silberschatz, Galvin and Gagne ©2009
FIFO service discipline may be used to service the processes waiting on
a semaphore
Semaphore definition (queuing implementation):
Each semaphore contains and integer value and a pointer to a list
of PCBs representing processes waiting
typedef struct {
int value;
struct
process
*list;//list
of
process
es
waiting
on
Operating System Concepts – 8th Edition 6.50 Silberschatz, Galvin and Gagne ©2009
Semaphore Implementation with
no Busy waiting (Cont.)
This implementation allows semaphore to take on negative values
If the semaphore value is negative; its magnitude is the number of processes waiting on that semaphore
Implementation of wait:
wait(semaphore *S) {
S->value--;
// if S is busy suspend the invoking process if
(S->value < 0) {
add this process to S->list;
block();
//else directly enter critical section that
follows wait()
}
Implementation of signal:
signal(semaphore *S) {
S->value++;
Bounded-Buffer Problem
Readers and Writers Problem
Sleeping Barber Problem
Dining-Philosophers Problem
Operating System Concepts – 8th Edition 6.52 Silberschatz, Galvin and Gagne ©2009
Dining-Philosophers Problem
Operating System Concepts – 8th Edition 6.53 Silberschatz, Galvin and Gagne ©2009
Dining-Philosophers Problem Algorithm
The structure of Philosopher i:
do {
// think_for_a_while;
// eat(Critical Section)
} while (TRUE);
Operating System Concepts – 8th Edition 6.54 Silberschatz, Galvin and Gagne ©2009
Deadlock free solutions
Allow a philosopher to pick up his chopsticks only if both the
chopsticks are available (this must be done in a critical section)
Use an asymmetric solution:
An odd philosopher picks up first his left chopstick and then right
chopstick, whereas an even philosopher picks up his right chopstick
and then his left chopstick.
Operating System Concepts – 8th Edition 6.55 Silberschatz, Galvin and Gagne ©2009
Problems with Semaphores
Incorrect use of semaphore operations:
Operating System Concepts – 8th Edition 6.56 Silberschatz, Galvin and Gagne ©2009
Asymmetric Solution(Deadlock and Starvation Free)
Operating System Concepts – 8th Edition 6.57 Silberschatz, Galvin and Gagne ©2009
Thank You
Operating System Concepts – 8th Edition 6.58 Silberschatz, Galvin and Gagne ©2009