Professional Documents
Culture Documents
Lecture 16-17
Operating System – Synchronization
Tayyeb Javed
Lecturer
Department of Computer Science
tayyeb.javed@nu.edu.pk
CPU Scheduling
Objectives
The Critical-Section Problem
Synchronization Hardware
Semaphores
Classical Problems of Synchronization
Initialize mutex to 1
signal(S):
Assume two simple S.value++;
operations: if (S.value <= 0) {
block suspends the process that remove a process P from S.L;
invokes it. wakeup(P); }
5 Advanced Operating System- Synchronization 03/04/2016
wakeup( P) resumes the
execution of a blocked process P
Synchronizing Processes using
Semaphores
Two processes: Define a semaphore “synch”
P1 and P2 Initialize synch to 0
Statement S1 in P1 needs to
be performed before Put this in P2:
statement S2 in P2 wait(synch);
We want a way to make P2 S2;
wait
Until P1 tells it is OK to And this in P1:
proceed S1;
signal(synch);
22
Shared
CS-205 Operatingdata
Systemare protected by placing them in8-Mar-2016
the
monitor
Condition Variables
Process synchronization is done using condition
variables, which represent conditions a process may
need to wait for before executing in the monitor
condition x, y;
Local to the monitor (accessible only within the
monitor)
Can be accessed and changed only by two
functions:
x.wait(): blocks execution of the calling process on
condition x
the process can resume execution only if another process executes
x.signal()
23 Operating System
x.signal(): resume execution of some process blocked
CS-205 on
8-Mar-2016
Monitors
Awaiting processes are
either in the entrance
queue or in a condition
queue
A process puts itself into
condition queue cn by
issuing cn.wait()
cn.signal() brings into
the monitor one process
in condition cn queue
signal-and-wait and
signal-and-continue
24 CS-205 Operating System 8-Mar-2016
Producer Consumer using Monitor
Two types of processes: Producer:
producers
consumers
while(TRUE){
Synchronization is now produce item;
confined within the monitor append(item);
append(.) and take(.) are }
procedures within the Consumer:
monitor: are the only means
while(TRUE){
by which P/C can access the
buffer item=take();
If these procedures are consume item;
correct, synchronization will }
be correct for all
25 participating
CS-205 processes
Operating System 8-Mar-2016
Monitor for the Bounded P/C
Problem
Buffer:
buffer: array[0..k-1] of items;
Buffer pointers and counts:
nextin: points to next item to be appended
nextout: points to next item to be taken
count: holds the number of items in the buffer
Condition variables:
notfull: notfull.signal() indicates that the buffer is not
full
notempty: notempty.signal() indicates that the buffer is
not empty
26 CS-205 Operating System 8-Mar-2016
Monitor for bounded buffer problem
Monitor boundedbuffer { Item Take(){
Item buffer[k]; if (count==0)
integer nextin, nextout, count; notempty.wait();
condition notfull, notempty; v = buffer[nextout];
Append(v){ nextout =
if (count==k) (nextout+1) mod k;
notfull.wait(); count--;
buffer[nextin] = v; notfull.signal();
nextin = (nextin+1) mod k; return v;
count++; }
notempty.signal(); }
}
initialization_code(){
nextin=0; nextout=0; count=0;
}
27 CS-205 Operating System 8-Mar-2016
Synchronization Primitives —
Summary
SHARED MEMORY NO SHARED MEMORY
hardware load/store
interrupt
disable/enable
test-and-set