Professional Documents
Culture Documents
Operating Systems: Syed Mansoor Sarwar
Operating Systems: Syed Mansoor Sarwar
Systems
Lecture 23
Syed Mansoor Sarwar
Agenda for Today
Review of previous lecture
Busy waiting
Process synchronization
Recap of lecture
14 September 2019 © Copyright Virtual University of
Pakistan
Review of Lecture 22
A good hardware based solution
Semaphores
typedef struct {
int value;
struct process *L;
} semaphore;
14 September 2019 © Copyright Virtual University of
Pakistan
Semaphore
Implementation
Assume two simple operations:
block() suspends the
process that invokes it.
wakeup(P) resumes the
execution of a blocked process
P.
14 September 2019 © Copyright Virtual University of
Pakistan
Semaphore
Implementation
The negative value of S.value
indicates the number of
processes waiting for the
semaphore
A pointer in the PCB needed to
maintain a queue of processes
waiting for a semaphore
14 September 2019 © Copyright Virtual University of
Pakistan
Semaphore wait()
Semaphore operations now
defined as
void wait(semaphore S){
S.value--;
if (S.value < 0) {
add this process to S.L;
block();
}
}
14 September 2019 © Copyright Virtual University of
Pakistan
Semaphore signal()
void signal(semaphore S){
S.value++;
if (S.value <= 0) {
remove process P from S.L;
wakeup(P);
}
}
P0 P1
14 September 2019
signal(Q);
© Copyright Virtual University of
Pakistan
Starvation
(Infinite Blocking)
P0 P1
wait(S); wait(S);
wait(S); signal(S);
14 September 2019 © Copyright Virtual University of
Pakistan
Violation of Mutual
Exclusion
P0 P1
signal(S); wait(S);
wait(S); signal(S);
14 September 2019 © Copyright Virtual University of
Pakistan
Main Cause of Problem
and Solution
Cause: Programming errors due
to the tandem use of wait()
and signal() operations
Solution: Higher-level language
constructs such as critical region
(region statement) and monitor.
14 September 2019 © Copyright Virtual University of
Pakistan
Recap of Lecture
Busy waiting
New definition of semaphore
Process synchronization