You are on page 1of 25

Chapter 5

Concurrency: Mutual Exclusion


and Synchronisation: Part 2
In this session…
• We will cover:
– Difficulties in Concurrency
• Race conditions
• Operating System Concerns
• Requirements for Mutual Exclusion
– Concurrency Mechanisms- Semaphores
• Strong/Weak Semaphores
• Implementation of Semaphores

3
Difficulties of Concurrency
• Sharing of global resources: if two processes both make use of the same
global variable and both perform reads and writes on that variable, then the
order in which the various reads and writes are executed is critical
• Difficult for the OS to manage the allocation of resources optimally:
process A may request use of, and be granted control of, a particular I/O
channel and then be suspended before using that channel. It may be
undesirable for the OS simply to lock the channel and prevent its use by
other processes; indeed this may lead to a deadlock condition
• Difficult to locate programming errors as results are not deterministic
and reproducible
Race Condition
• A situation in which • Process A, Process B,
multiple threads or sharing global variable a
processes read and write
a shared data item and • P1: update a←1
the final result depends
on the relative timing of • P2: update a←2
their execution
• The “loser” of the race is • The “loser” of the race
the process that updates (the process that
last and will determine the updates last)
final value of the variable determines the final
value of a

• Here, P2 is the loser


5
Race Condition Example

• Consider the following set of operations:


• variable a = 1
• variable a = a * 5
• variable b = a – 1
• If these operations are processed in the correct sequence, variable b should
equal 4. But if operation 3 is executed before operation 2 has completed, b
would equal 0
Race Condition
• Race conditions are an important problem in CPU
design that use both parallel and concurrent
processing techniques to execute multiple instructions
in an overlapping time frame. To ensure that the output
of calculations is not corrupted, race conditions must
always be anticipated and avoided
• It's also an important problem for software developers,
who must handle any race conditions that may occur
when their code is used in real-world situations.
Famously, an improperly handled race condition in the
software of NASA's Spirit Explorer Rover nearly
resulted in the rover being lost shortly after it landed on
Mars
Operating System Concerns
• Design and management issues raised by the existence of concurrency:
– The OS must:

Be able to keep track of processes

Allocate and de-allocate resources for each active process

Protect the data and physical resources of each process against unintended
interference by other processes

The functioning of a process, and the output it produces, must be independent of the speed
at which its execution is carried out relative to the speed of other concurrent processes

8
Recap on Starvation
• Starvation problem – where a process in the ready state waits
indefinitely to get access to the CPU
• Waiting indefinitely means that the process execution keeps getting
pushed back to a further time
• This can happen with SJF as new possesses can keep entering the
RAM with a shorter burst time (meaning it has greater priority)
• This means that the SJF scheduling algorithm can suffer from
• starvation
• Any priority based scheduling algorithm can suffer from starvation

9
Requirements for Mutual Exclusion
• To provide support for mutual exclusion, the following requirements
should be met:
– Mutual exclusion must be enforced: only one process at a time is allowed
into its critical section, among all processes that have critical sections for the
same resource or shared object
– A process that halts must do so without interfering with other processes
– It must not be possible for a process requiring access to a critical section to
be delayed indefinitely: no deadlock or starvation
Requirements for Mutual Exclusion (Cont.)
• When no process is in a critical section, any process that request
entry to its critical section must be permitted to enter without delay
• No assumptions are made about relative process speeds or
number of processes
• A process remains inside its critical section for a finite time only
Concurrency Mechanisms- Semaphore
• In basic terms, Semaphores are a protected integer that can
facilitate and restrict access to shared recourses in a multi-
processing environment
• An integer value used for signaling among processes
• Only three operations may be performed on a semaphore, all of
which are atomic: initialise, decrement, and increment
• The decrement operation may result in the blocking of a process,
and the increment operation may result in the unblocking of a
process
• Also known as a counting semaphore or a general semaphore
Example:
• Suppose a library has 10 identical study rooms, to be used by one student at
a time
• Students must request a room from the front desk if they wish to use a study
room. If no rooms are free, students wait at the desk until someone
relinquishes a room
• When a student has finished using a room, the student must return to the
desk and indicate that one room has become free
• The clerk at the front desk knows only the number of free rooms available.
When a student requests a room, the clerk decreases this number. When a
student releases a room, the clerk increases this number. (Counting
Semaphore)
Example 2:
• In this scenario
– the front desk count-holder represents a counting semaphore
– the rooms are the resource
– the students represent processes/threads
• The semaphore is initially 10. 10 students arrive, then semaphore is
9,8,7,6,5,4,3,2,1, 0
• 11th student arrive, semaphore is -1 (1 student blocked), 12th student arrive:
semaphore is -2 (2 students blocked), …
• If semaphore is -5, then one student releases his/her room, semaphore will
be -4
– What does this mean? It means that one blocked student can be unblocked and get one
room
• Semaphore is a variable for mutual exclusion, not to indicate the number of
resources!! 14
Semaphore

A variable that
has an integer • There is no way to inspect or
value upon which manipulate semaphores other
three operations than these three operations
are defined:

1) A semaphore may be initialised to a nonnegative integer value


2) The semWait operation decrements the semaphore value
3) The semSignal operation increments the semaphore value

15
struct semaphore {
int count;
queueType queue;
};
void semwait(semaphore s)
{
s.count--;
if (s.counl < 0) {
I* place this process in s.queue */;
I* block this process */;
}
}
void semSignal(semaphore s)
{
s.count++ ;
if (s.count < = 0) {
I* remove a process P from s.queue */;
I* place process P on ready list */;
}
}
Strong/Weak Semaphores
• A queue is used to hold processes waiting on the semaphore

Strong Weak
Semaphores Semaphores
• The process that • The order in
has been blocked which processes
the longest is are removed from
released from the the queue is not
queue first (FIFO) specified

17
Mutual Exclusion Using
Semaphores
I* program mutualexclusion *I
const int n = I* number of processes */;
semaphore s = 1;
void P(int i)
{
while (true) {
semWait(s);
I* critical section *I ;
semSignal(s);
I* remainder */;
}
}
void main()
{
pa r b e g i n ( P(1) , P(2), ..., P(n));
}

18
Implementation of Semaphores
• Imperative that the semWait and semSignal operations be
implemented as atomic primitives
• Can be implemented in hardware or firmware
• Software schemes such as Dekker’s or Peterson’s algorithms can
be used
• However this would entail a substantial processing overhead
Message Passing
• When processes interact with one another two fundamental
requirements must be satisfied:

Syncronisation Communication

• To enforce • To exchange
mutual information
exclusion

• Message passing is one approach to providing both of these


functions
– Works with distributed systems and shared memory multiprocessor and
uniprocessor systems
20
• The actual function is normally provided in the form of a pair of
primitives:
send (destination, message)
receive (source, message)
• A process sends information in the form of a message to another
process designated by a destination
• A process receives information by executing the receive primitive,
indicating the source and the message
Synchronisation
When a receive primitive
is executed in a process
there are two
possibilities:

Communication of a
message between
two processes implies
synchronisation
between the two

22
Queuing Discipline
• The simplest queueing discipline is first-in-first-out
– This may not be sufficient if some message are more urgent than others
• Other alternatives are:
– To allow the specifying of message priority, on the basis of message type
or by designation by the sender
– To allow the receiver to inspect the message queue and select which
message to receive next
Readers/Writers Problem
• A data area is shared among many processes
– Some processes only read the data area, (readers) and some only write to the
data area (writers)
• Conditions that must be satisfied:
– Any number of readers may simultaneously read the file
– Only one writer at a time may write to the file
– If a writer is writing to the file, no reader may read it
Semaphores Readers Has Priority

25
Summary
• In this session we have covered:
– Concurrency
– Race conditions
– Semaphore’s
– Message Passing
– Synchronisation

26

You might also like