Professional Documents
Culture Documents
Lecture 4 Synchronization v1
Lecture 4 Synchronization v1
Lecture 4 Synchronization v1
Dep. of IT
Operating
Systems
Lecture 4: Process
Synchronization
2019/2020 Prepared by: Dr. Rasha Bin-Thalab
Lecture Out Lines
2
Background
The Critical-Section Problem
Peterson’s Solution
Synchronization Hardware
Mutex Locks
Semaphores
Classic Problems of Synchronization
Monitors
Synchronization Examples
Alternative Approaches
Communication in client-server processes
Background
3
Producer
Consumer
while (true) {
while (true) {
// produce an item in next produced
while (counter == 0);
/* do nothing */
while (counter == BUFFER_SIZE) ;
next_consumed = buffer[out];
/* do nothing */
out = (out + 1) % BUFFER_SIZE;
buffer[in] = next_produced;
counter--;
in = (in + 1) % BUFFER_SIZE;
// consume the item in next consumed
counter++;
}
}
Race counter++ could be implemented as
register1 = counter
register1 = register1 + 1
Condition
7
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
Critical Section Problem
8
int turn;
boolean flag[2];
do {
flag[i] = true;
turn = j;
while (flag[j] && turn = = j);
critical section
flag[i] = false;
remainder section
} while (true);
Critical section solution
12
Consider a system consisting of two processes P1 and P2, each process accessing two semaphores, S
and Q set to the value 1: What is the output if the processes executed concurrently?
P1 P2 Output
Wait(S) Wait(Q)
Wait(Q)
Signal(S)
Signal(Q) Signal(Q)
Communications in Client-Server
18
Systems
Sockets
Remote Procedure Calls
Pipes
Remote Method Invocation (Java)
Sockets
19