Title: Topic 7: Concurrency Control, Deadlock and Resources
1Topic 7 Concurrency Control, Deadlock and
Resources
- L E Pages 117-134
- Tanenbaum Pages 75-87, 166-177
2Semaphores A Quick Reminder
- Semaphores are non negative integers which (apart
from their initialisation) can only be operated
on by the operations wait (P) and signal (V). - Signal adds one to the value of a semaphore.
- Wait subtracts one from the value of the
semaphore unless the semaphore is zero in which
case the process is blocked until the subtraction
can be done without making the semaphore
negative. - Indivisible (atomic) operations
- cf s s 1
3Using Semaphores To Implement Mutual Exclusion
Semaphore mutex 1
.... non critical code ... wait (mutex) ...
critical code ... signal (mutex) ... non
critical code ...
.... non critical code ... wait (mutex) ...
critical code ... signal (mutex) ... non
critical code ...
4Using Semaphores to Implement Resource Allocation
Begin prepare data wait (printer) print
(data) signal (printer) End.
Semaphore printer 1
5Using Semaphores to Implement Resource Allocation
Begin prepare data wait (printer) print
(data) signal (printer) End.
Semaphore printer 1
6Using Semaphores to Implement Resource Allocation
Begin prepare data wait (printer) print
(data) signal (printer) End.
Semaphore printer 0
7Using Semaphores to Implement Resource Allocation
Begin prepare data wait (printer) print
(data) signal (printer) End.
Semaphore printer 0
8Using Semaphores to Implement Resource Allocation
Begin prepare data wait (printer) print
(data) signal (printer) End.
Semaphore printer 1
9Acquiring Two Resources
Begin prepare data wait (disk) wait
(printer) print (data) signal (disk) signal
(printer) End.
10Two Processes Acquiring Two Resources
Begin prepare data wait (printer) wait
(disk) print (data) signal (disk) signal
(printer) End.
Begin prepare data wait (disk) wait
(printer) print (data) signal
(printer) signal (disk) End.
11Two Processes Acquiring Two Resources
Begin prepare data wait (printer) wait
(disk) print (data) signal (disk) signal
(printer) End.
Begin prepare data wait (disk) wait
(printer) print (data) signal
(printer) signal (disk) End.
12Two Processes Acquiring Two Resources
Begin prepare data wait (printer) wait
(disk) print (data) signal (disk) signal
(printer) End.
Begin prepare data wait (disk) wait
(printer) print (data) signal
(printer) signal (disk) End.
13Two Processes Acquiring Two Resources
Begin prepare data wait (printer) wait
(disk) print (data) signal (disk) signal
(printer) End.
Begin prepare data wait (disk) wait
(printer) print (data) signal
(printer) signal (disk) End.
14Two Processes Acquiring Two Resources
Begin prepare data wait (printer) wait
(disk) print (data) signal (disk) signal
(printer) End.
Begin prepare data wait (disk) wait
(printer) print (data) signal
(printer) signal (disk) End.
15Two Processes Acquiring Two Resources
Begin prepare data wait (printer) wait
(disk) print (data) signal (disk) signal
(printer) End.
Begin prepare data wait (disk) wait
(printer) print (data) signal
(printer) signal (disk) End.
16Two Processes Acquiring Two Resources
Begin prepare data wait (printer) wait
(disk) print (data) signal (disk) signal
(printer) End.
Begin prepare data wait (disk) wait
(printer) print (data) signal
(printer) signal (disk) End.
17Deadlock !
Got Printer, Waiting on Disk
Got Disk, Waiting on Printer
18Dining Philosophers
19Dining Philosophers
- Philosophers do only 2 things
- Think
- Eat
- To eat a philosopher requires two forks
(chopsticks).
20Dining Philosophers
21Dining Philosophers
22Dining Philosophers
23Dining Philosophers
- The implementation should be simple -
while true do wait (right_fork) wait
(left_fork) eat signal (left_fork) signal
(right_fork) end loop
Acquire resources
Release resources
24But ... Consider the Following Situation
25But ... Consider the Following Situation
26But ... Consider the Following Situation
27But ... Consider the Following Situation
28But ... Consider the Following Situation
29Deadlock !
Got left chopstick, Waiting on right
Got left chopstick, Waiting on right
cf Got Disk, Waiting on Printer
cf Got Printer, Waiting on Disk
30Conditions for Deadlock
- The resources involved are non-sharable.
- Processes hold the resources they have been
allocated while waiting for new ones. - Resources cannot be pre-emptied.
- A circular chain of processes exists.
31Solutions to the Deadlock Problem
- Prevent deadlock.
- Detect deadlock and try to recover.
- Avoid deadlock by anticipating events.
32Deadlock Prevention
- Deny one of the four conditions for deadlock-
- The resources involved are unsharable.
- Cf Spooling
- Processes hold the resources they have been
allocated while waiting for new ones. - Make processes request all resources at once
- Resources cannot be pre-emptied while in use.
- Force a process to release all held resources
- A circular chain of processes exists.
- Allocate an order on resource types
33Deadlock Detection
- Detection algorithms work by detecting circular
wait patterns. - Processes are represented as nodes and resource
dependencies as arcs.
process holds A and wants B
B
A
- Periodically search for loops.
34Deadlock Detection Continued
- Example of a loop representing deadlock
B
A
C
D
35Recovering from Deadlock
- Abort all deadlocked processes.
- Common approach
- Restart deadlocked processes from a checkpoint.
- Relies on non-determinacy of the system
- Abort deadlocked processes one by one until
deadlock broken. - Ordering can be chosen to reduce cost.
- Release resources one by one until deadlock
broken. - Need to re-invoke detection algorithm
36Deadlock Avoidance
- Tries to predict/anticipate when deadlock is
likely to occur. - cf Deadlock Prevention
- How is deadlock avoided?
- Check each resource request and if it would cause
deadlock then abort it. - State Graph
- Problem
- Allocation of a resource does not always produce
a situation of deadlock immediately
37Deadlock Avoidance
Instructions process B
D
Instructions process A
38Resource Issues and Scheduling
- The issue of resource allocation is intimately
associated with the issue of scheduling, i.e.
deciding which process to run next. - Recall that processes cycle through 3 states.
- Ready
- Running
- Blocked
39The Process State Model cont
Ready
40The Process State Model cont
Process is selected to run
Ready
Running
41EITHER
42The Process State Model .. contd
Process is selected to run
Ready
Running
Process finishes its time slice
43OR
44The Process State Model .. contd
Process is selected to run
Ready
Running
Blocked
Process blocks (e.g. I/O)
45The Process State Model cont
Process is selected to run
Ready
Running
Blocking operation finishes (e.g. I/O)
Blocked
Process blocks (e.g. I/O)
46Resource Issues and Scheduling
- General scheduling model shown below.
Ready Queue
CPU
Blocked Queue
47Scheduling and the Dispatcher
- We designed and built a dispatcher.
- Higher level decisions taken by a process called
a scheduler. - Scheduler tries to make best use of resources.
- Most systems use some form of round-robin
scheduling algorithm. - Simple scheme which gives each non-blocked
process a fixed size time-slice.
48Round Robin Scheduling
49Round Robin Scheduling
50Round Robin Scheduling
51Round Robin Scheduling
52Round Robin Scheduling
53Round Robin Scheduling
54Round Robin Scheduling
55Priorities
- Sometimes want different priority processes.
- These can be implemented by either
- Multiple ready queues.
- Giving different processes different length
time-slices. - Giving different processes more time slices.
56Minimising Transitions to the Blocked State
- Important transitions to blocked state increase
overhead because processes not running for their
full time-slice. - Can minimise number of transitions by ensuring
resources are available when required. - This requires additional scheduling strategies,
e.g. give resource intensive processes a high
priority. - Why ?
57Summary
- Recapped on semaphores and mutual exclusion.
- Highlighted the problem of deadlock.
- Reviewed techniques for overcoming deadlock
(prevention, detection and avoidance). - Briefly covered process scheduling and impact of
resource allocation strategy.
58Coming Next Week
- Memory Management.
- Book Reminder
- Fundamentals of Operating Systems, A. M.
Lister and R. D. Eager, Fifth Edition, Macmillan
Computer Science Series, ISBN. 0-333-59848-2,
13.99. - Operating Systems Design and Implementation, A.
S. Tanenbaum, Prentice-Hall International,
ISBN. 0-13-630195-9, about 29.99. - www.comp.lancs.ac.uk/computing/staff/kc/keiths_tea
ching.html