Topic 7: Concurrency Control, Deadlock and Resources - PowerPoint PPT Presentation

1 / 58
About This Presentation
Title:

Topic 7: Concurrency Control, Deadlock and Resources

Description:

To eat a philosopher requires two forks (chopsticks). C.Sc. 110: Computer Systems ... Got left chopstick, Waiting on right. Got left chopstick, Waiting on right. C.Sc. ... – PowerPoint PPT presentation

Number of Views:95
Avg rating:3.0/5.0
Slides: 59
Provided by: compLa
Category:

less

Transcript and Presenter's Notes

Title: Topic 7: Concurrency Control, Deadlock and Resources


1
Topic 7 Concurrency Control, Deadlock and
Resources
  • L E Pages 117-134
  • Tanenbaum Pages 75-87, 166-177

2
Semaphores 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

3
Using 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 ...
4
Using Semaphores to Implement Resource Allocation
Begin prepare data wait (printer) print
(data) signal (printer) End.
Semaphore printer 1
5
Using Semaphores to Implement Resource Allocation
Begin prepare data wait (printer) print
(data) signal (printer) End.
Semaphore printer 1
6
Using Semaphores to Implement Resource Allocation
Begin prepare data wait (printer) print
(data) signal (printer) End.
Semaphore printer 0
7
Using Semaphores to Implement Resource Allocation
Begin prepare data wait (printer) print
(data) signal (printer) End.
Semaphore printer 0
8
Using Semaphores to Implement Resource Allocation
Begin prepare data wait (printer) print
(data) signal (printer) End.
Semaphore printer 1
9
Acquiring Two Resources
Begin prepare data wait (disk) wait
(printer) print (data) signal (disk) signal
(printer) End.
10
Two 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.
11
Two 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.
12
Two 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.
13
Two 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.
14
Two 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.
15
Two 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.
16
Two 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.
17
Deadlock !
Got Printer, Waiting on Disk
Got Disk, Waiting on Printer
18
Dining Philosophers
19
Dining Philosophers
  • Philosophers do only 2 things
  • Think
  • Eat
  • To eat a philosopher requires two forks
    (chopsticks).

20
Dining Philosophers
21
Dining Philosophers
22
Dining Philosophers
23
Dining 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
24
But ... Consider the Following Situation
25
But ... Consider the Following Situation
26
But ... Consider the Following Situation
27
But ... Consider the Following Situation
28
But ... Consider the Following Situation
29
Deadlock !
Got left chopstick, Waiting on right
Got left chopstick, Waiting on right
cf Got Disk, Waiting on Printer
cf Got Printer, Waiting on Disk
30
Conditions 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.

31
Solutions to the Deadlock Problem
  • Prevent deadlock.
  • Detect deadlock and try to recover.
  • Avoid deadlock by anticipating events.

32
Deadlock 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

33
Deadlock 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.

34
Deadlock Detection Continued
  • Example of a loop representing deadlock

B
A
C
D
35
Recovering 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

36
Deadlock 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

37
Deadlock Avoidance
Instructions process B
D
Instructions process A
38
Resource 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

39
The Process State Model cont
Ready
40
The Process State Model cont
Process is selected to run
Ready
Running
41
EITHER
42
The Process State Model .. contd
Process is selected to run
Ready
Running
Process finishes its time slice
43
OR
44
The Process State Model .. contd
Process is selected to run
Ready
Running
Blocked
Process blocks (e.g. I/O)
45
The 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)
46
Resource Issues and Scheduling
  • General scheduling model shown below.

Ready Queue
CPU
Blocked Queue
47
Scheduling 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.

48
Round Robin Scheduling
49
Round Robin Scheduling
50
Round Robin Scheduling
51
Round Robin Scheduling
52
Round Robin Scheduling
53
Round Robin Scheduling
54
Round Robin Scheduling
55
Priorities
  • 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.

56
Minimising 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 ?

57
Summary
  • 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.

58
Coming 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
Write a Comment
User Comments (0)
About PowerShow.com