You are on page 1of 38

05 Process Synchronization

Operating
Systems

Outline

Introduction

Synchronization Problems

Synchronization Solutions

Operating
Systems

Cooperating Processes

Cooperating processes are processes that are working


together to perform a certain task
For two processes to cooperate, there should be a way for
processes to communicate with one another

Shared memory

Shared file

Mailboxes or pipes

Operating
Systems

Race condition

If processes are cooperating, then a particular scenario


called the race condition can occur.
Consider the following situation

You have a producer process that places items in a stack

You have a consumer process that removes items from a stack

(to visualize it better, consider a bartender pours beer in the first


empty glass while a beer drinker drinks the most freshly poured
glass)

Operating
Systems

Race Condition

Operating
Systems

Race Condition

If we convert our situation to code, it would look like this:

Operating
Systems

Race Condition

As the CPU can only run one process at a time, consider the
following instruction execution when context switches occur
in a particular pattern.

Beerdrinker: top = top 1


Bartender: beerstack[top] = new Beer // this would place a beer on top of an existing beer!
Bartender: top = top + 1
Beerdrinker: drink beerstack[top] // beerdrinker would drink an empty mug!

If the context switch does not occur here, then the program
would run correctly.
However, as there is no process control when the context
switch takes place, then there is a chance the system can
end up in an inconsistent state
Operating
Systems

Race Condition
Beerdrinker: top = top 1
Bartender: beerstack[top] = new Beer // this would place a beer on top of an existing beer!
Bartender: top = top + 1
Beerdrinker: drink beerstack[top] // beerdrinker would drink an empty mug!

If the context switch does not occur here, then the program
would run correctly.
However, as there is no process control when the context
switch takes place, then there is a chance the system can
end up in an inconsistent state.
There is therefore a need for a process to determine a
critical section of code where no context switch may occur.

Operating
Systems

Resource Competition

Another problem that occurs in multiprocessor systems is


resource competition
As the systems resources are limited, a process may need
to wait for another process to finish with its use of the
resource to become available before it can use it.

Consider a printer being in use by two applications, both cannot print


at the same time

Operating
Systems

Need for Synchronization

Synchronization addresses the race condition

Allows processes to define critical region

Synchronization addresses resource competition

Allows processes to wait for resources and notify others when they
are made available

Operating
Systems

10

Critical Section Problem

A critical section of code is where shared data between


other cooperating processes are manipulated

Errors due to race condition would be prevented by a solution to the


Critical Section Problem

Operating
Systems

11

Critical Section Problem

Processes having a critical section are defined to have its


code in three parts

Entry section code that implements the critical section problem


solution

Critical section code that only one process can execute

Remainder section the rest of the program that has no effect on


the critical section

Operating
Systems

12

Critical Section Problem

Operating
Systems

13

Critical Section Problem

Any solution to the Critical Section Problem would have the


following characteristics:

Mutual exclusion

Progress

Bounded wait

Operating
Systems

14

Mutual Exclusion

Only a single process is allowed to run its critical section

All other processes must wait

Operating
Systems

15

Progress

If there are more than one process waiting to enter its critical
section...

Selection must be made by other processes waiting to enter critical


section

Selection must be done at the earliest possible time

Operating
Systems

16

Bounded Wait

A process cannot be
allowed to wait forever to
enter its critical section

Operating
Systems

17

Critical Section

Our producer-consumer problem can be solved with a


solution to the critical section problem

Simply have the manipulation of the top variable and the addition (or
deletion) of a stack object be in a critical section.

Operating
Systems

18

Readers and Writers

A file can be read by multiple users at the same time


However, if a file is modified by a process, all other
processes must wait for the modification to finish

Operating
Systems

19

Dining Philosophers

Consider a group of philosophers seated at a table.

Dining table has exactly enough plates for all philosophers

Not enough chopsticks, philosophers must share

Operating
Systems

20

Dining Philosophers

The table is setup so that each philosopher's right chopstick


is also the left chopstick of the one seated on the right and
vice versa

Operating
Systems

21

Solutions to Synch Problems

Busy Wait

Wait and Notify

Semaphores

Monitors

Operating
Systems

22

Busy Wait

A solution for a two process Critical Section problem is as


follows

Have a turn variable indicating whose process turn it is

Busy Wait means having a loop that constantly checks if it is


its that particular process' turn

Operating
Systems

23

Busy Wait

Consider two processes i and j.


The following slide is the (incomplete) solution for busy wait
for two processes

The code is for process i. Process j code is simply the inverse of this

Operating
Systems

24

Busy Wait
do {
yet

while (turn!=i) { }

criticalsection();
turn = j;
remaindersection();
} while (true);

// loop while it is not my turn


// after i'm done, its j's turn

Operating
Systems

25

Busy Wait

Mutual exclusion is established as process i can only enter


its critical section by process j leaving its critical section and
vice versa
However, this code fails the progress requirement

If process i loops very fast, it would wait at the busy loop until
process j finishes its remainder section and enter its critical section
again

Also, this wastes CPU time by constantly checking the


condition

Operating
Systems

26

Wait and Notify

An alternative to busy wait is to have a function called wait()


which simply stops the process from running
Process only starts moving when it is notify()-ed from
another process.

Operating
Systems

27

Wait and Notify


do {

if (turn != i) {
wait(); // stop running until i am notified by j
}
criticalsection();
turn = j;
//after i is done done, its j's turn
notify(j);
// wakeup j if it is waiting.
remaindersection();
} while (true);

Operating
Systems

28

Semaphores

A semaphore is an object that modifies the wait() and


notify() primitives to take into account

an integer value that indicates how many processes are waiting to


enter the critical section

a linked list that will store the waiting processes

Operating
Systems

29

Semaphores
class Semaphore {
int ctr = 1;
ProcessList L;
void wait() {
ctr = ctr 1;
if (ctr < 0) {
add this process to L;
block;
}
}
void notify() {
ctr = ctr + 1;
if (ctr <= 0) {
get a process from L and resume its
running
}
}
}

Operating
Systems

30

Semaphores

Our two processes would now use the semaphore


Semaphore s; //shared by the two processes
do {
s.wait();
criticalsection();
s.notify();
remaindersection();
} while (true);

Operating
Systems

31

Semaphore
class Semaphore {
int ctr = 1;
ProcessList L;
void wait() {
ctr = ctr 1;
if (ctr < 0) {
add this process
to L;
block;
}
}
void notify() {
ctr = ctr + 1;
if (ctr <= 0) {
get a process
from L
and resume its
running
}
}
}

Operating
Systems

The initial value of ctr


indicates how many
processes are allowed to
proceed.
The first process to invoke
wait() goes through
The next process to invoke
wait() finds ctr < 0 and is
forced to block
ctr now contains the
negative of the number of
waiting processes
32

Semaphore
class Semaphore {
int ctr = 1;
ProcessList L;
void wait() {
ctr = ctr 1;
if (ctr < 0) {
add this process
to L;
block;
}

}
void notify() {
ctr = ctr + 1;
if (ctr <= 0) {
get a process
from L
and resume its
running
}
}
}
Operating
Systems

Once a process leaves its


critical section, it notifies all
other processes
One of the processes is
chosen
ctr is decreased by 1 to
reflect this

33

Semaphores

The advantage of semaphores are if there are no processes


waiting or running in the critical section (i.e. ctr =1) then
wait() will allow any process to pass through.

Operating
Systems

34

Dining Philosopers
do {

chopstick[i].wait() //wait on the left chopstick


chopstick[(i+1) % N].wait() // wait on the right chopstick
eat;
chopstick[(i+1) % N].notify() // release the right chopstick
chopstick[i].notify() // release the left chopstick
think;
} until(true)

Still not the best solution as it is prone to deadlocks

i.e. What happens when everyone grabs their left chopstick all at
once?

Operating
Systems

35

Monitors

An object that only allows a single process to run any of its


methods.
It abstracts process synchronization from the programmer

Operating
Systems

36

Monitors

Operating
Systems

37

Exercises
Give examples of problems (situational) in process
synchronization (at least 3 problems).
Provide the solutions for your given problems based from the
stated process synchronization solutions.

Operating
Systems

38

You might also like