Concurrency: Mutual Exclusion, Synchronization, Deadlock and Starvation
Concurrency: Mutual Exclusion, Synchronization, Deadlock and Starvation
Concurrency: Mutual Exclusion, Synchronization, Deadlock and Starvation
Exclusion , Synchronization
,Deadlock and Starvation
Roadmap
• Principals of Concurrency
Multiple Processes/Threads
• Central to the design of modern Operating
Systems is managing multiple processes
– Multiprogramming
– Multiprocessing
– Distributed Processing
• Big Issue is Concurrency
– Managing the interaction of all of these
processes
Concurrency
Concurrency arises in:
• Multiple applications
– Sharing time
• Structured applications
– Extension of modular design
• Operating system structure
– OS themselves implemented as a set of
processes or threads
Key Terms
Interleaving and
Overlapping Processes
• Earlier (Ch2) we saw that processes may
be interleaved on uniprocessors
Interleaving and
Overlapping Processes
• And not only interleaved but overlapped
on multi-processors
Difficulties of
Concurrency
• Sharing of global resources
• Optimally managing the allocation of
resources
• Difficult to locate programming errors as
results are not deterministic and
reproducible.
A Simple Example
void echo()
{
chin = getchar();
chout = chin;
putchar(chout);
}
A Simple Example:
On a Multiprocessor
Process P1 Process P2
. .
chin = getchar(); .
. chin = getchar();
chout = chin; chout = chin;
putchar(chout); .
. putchar(chout);
. .
Enforce Single Access
• If we enforce a rule that only one process
may enter the function at a time then:
• P1 & P2 run on separate processors
• P1 enters echo first,
– P2 tries to enter but is blocked – P2 suspends
• P1 completes execution
– P2 resumes and executes echo
Race Condition
• A race condition occurs when
– Multiple processes or threads read and write
data items
– They do so in a way where the final result
depends on the order of execution of the
processes.
• The output depends on who finishes the
race last.
Operating System
Concerns
• What design and management issues are
raised by the existence of concurrency?
• The OS must
– Keep track of various processes
– Allocate and de-allocate resources
– Protect the data and resources against
interference by other processes.
– Ensure that the processes and outputs are
independent of the processing speed
Competition among
Processes for Resources
Three main control problems:
• Need for Mutual Exclusion
– Critical sections
• Deadlock
• Starvation
Requirements for
Mutual Exclusion
• Only one process at a time is allowed in
the critical section for a resource
• A process that halts in its noncritical
section must do so without interfering with
other processes
• No deadlock or starvation
Requirements for
Mutual Exclusion
• A process must not be delayed access to
a critical section when there is no other
process using it
• No assumptions are made about relative
process speeds or number of processes
• A process remains inside its critical section
for a finite time only
Roadmap
• Principals of Deadlock
– Deadlock prevention
– Deadlock Avoidance
– Deadlock detection
– An Integrated deadlock strategy
Deadlock
• A set of processes is deadlocked when
each process in the set is blocked awaiting
an event that can only be triggered by
another blocked process in the set
– Typically involves processes competing for
the same set of resources
• No efficient solution
Potential Deadlock
I need I need
quad C quad B and
and B C
I need
I need quad A and
quad D B
and A
Actual Deadlock
HALT until
HALT until B is free
A is free
Two Processes P and Q
• Lets look at this with
two processes P and Q
• Each needing
exclusive access to a
resource A and B for a
period of time
Alternative logic
• Suppose that P does
not need both
resources at the same
time so that the two
processes have this
form
Resource Categories
Two general categories of resources:
• Reusable
– can be safely used by only one process at a
time and is not depleted by that use.
• Consumable
– one that can be created (produced) and
destroyed (consumed).
Reusable Resources
• Such as:
– Processors, I/O channels, main and
secondary memory, devices, and data
structures such as files, databases, and
semaphores
• Deadlock occurs if each process holds
one resource and requests the other
Example of
Reuse Deadlock
• Consider two processes that compete for
exclusive access to a disk file D and a
tape drive T.
• Deadlock occurs if each process holds
one resource and requests the other.
Reusable Resources
Example
Example 2:
Memory Request
• Space is available for allocation of
200Kbytes, and the following sequence of
events occur
P1 P2
... ...
Request 80 Kbytes; Request 70 Kbytes;
... ...
Request 60 Kbytes; Request 80 Kbytes;
• Circular Wait
– Define a linear ordering of resource types
Roadmap
• Principals of Deadlock
– Deadlock prevention
– Deadlock Avoidance
– Deadlock detection
– An Integrated deadlock strategy
Deadlock Avoidance
• A decision is made dynamically whether
the current resource allocation request
will, if granted, potentially lead to a
deadlock
• Requires knowledge of future process
requests
Two Approaches to
Deadlock Avoidance
• Process Initiation Denial
– Do not start a process if its demands might
lead to deadlock
Resources
Amount of available
Existing after
Resources allocation
Process i
• Cij - Aij ≤ Vj, for all j
• This is not possible for P1,
– which has only 1 unit of R1 and requires 2
more units of R1, 2 units of R2, and 2 units of
R3.
• If we assign one unit of R3 to process P2,
– Then P2 has its maximum required resources
allocated and can run to completion and
return resources to ‘available’ pool
After P2
runs to completion
• Can any of the remaining processes can
be completed?
Note P2 is
completed
After P1 completes
P3 Completes