Title: Computer Systems and Systems Software Lecture 7
1Computer Systems and Systems Software Lecture 7
- We will cover in this lecture
- semaphores as a solution to the critical section
problem - semaphores as inter process synchronisation and
co-ordination primitives - Introduction to the concept of deadlock
- the use of resource allocation graphs
2- Reading associated with lecture
- A. Silbershatz, P. B. Galvin and G. Gagne,
Operating Systems Concepts, 6th edition, chapter
7 p.201-209 chapter 8 p. 243-248
3Semaphore
- More general solution to critical section
problems - Semaphore S integer variable that can only be
accessed via two indivisible (atomic) operations - wait (S) while S? 0 do no-op
S S 1 - signal (S) S S 1
- This original semaphore definition requires busy
waiting - wasting CPU cycles that could be used
productively by other processes - often called a
spinlock (process spins while waiting for lock)
4ExampleCritical Section of n Processes
- Shared variables
- var mutex semaphore
- initially mutex 1
- Process Pi
repeat - wait(mutex)
- critical section
- signal(mutex)
- remainder section
- until false
5Semaphore Implementation
- Rather than busy waiting we can define a
semaphore to block a process that cannot proceed
placing it on a queue of processes associated
with the semaphore. signal(S) then removes a
process from waiting queue - Define a semaphore as a record
type semaphore record
value integer
L list of
process end - Assume two simple operations
- block suspends the process that invokes it.
- wakeup(P) resumes the execution of a blocked
process P.
6- Semaphore operations now defined as
- wait(S) S.value S.value 1
if S.value lt 0
then begin
add this process to S.L block
end
signal(S) S.value S.value 1
if S.value ? 0
then begin
remove
a process P from S.L wakeup(P)
end
7Semaphore for general synchronization
- Execute B in process Pj only after A executed in
process Pi - Use semaphore flag initialized to 0
- Code
- Pi Pj
- ? ?
- A wait(flag)
- signal(flag) B
8Deadlock and Starvation
- Deadlock two or more processes are waiting
indefinitely for an event that can only be caused
by one of the waiting processes - this could be
a signal operation. e.g. let S and Q be two
semaphores initialized to 1 - P0 P1
- wait(S) wait(Q)
- wait(Q) wait(S)
- ? ?
- signal(S) signal(Q)
- signal(Q) signal(S)
- Starvation indefinite blocking of a process on
a queue
9Two Types of Semaphores
- Counting semaphore integer value can range over
an unrestricted domain. Useful for controlling
access to a type of resource with a some given
number of instances of that resource e.g. with n
resources of a given type, with semaphore value
set to n, process will wait when n instances of
resource are already allocated. - Binary semaphore integer value can range only
between 0 and 1. Useful for implementing mutual
exclusion.
10Classical Problems of Synchronization - Bounded
Buffer Problem
- Shared data - as before
- buffer for shared data - an array organised as a
circular buffer - n is the size of the buffer
- BUT IN ADDITION now have semaphores
- full, empty, mutex initialised as below
- full 0 // meaning number of elements in
buffer - empty n // meaning no. of empty spaces in
buf - mutex 1
11Bounded-Buffer Problem (Cont.)
- Producer process
- repeat
-
- produce an item
-
- wait(empty)
- wait(mutex)
- put item in buffer
- signal(mutex)
- signal(full)
- until false
-
12Bounded-Buffer Problem (Cont.)
- Consumer process
- repeat
- wait(full)
- wait(mutex)
-
- remove an item from buffer
-
- signal(mutex)
- signal(empty)
-
- consume the item
-
- until false
13The Deadlock Problem
- A set of blocked processes each holding a
resource and waiting to acquire a resource held
by another process in the set. - Example
- System has 2 tape drives
- P1 and P2 each hold one tape drive and each needs
another one. - Example - semaphores A and B, initialized to 1
- P0 P1
- wait (A) wait(B)
- wait (B) wait(A)
14Bridge Crossing Example
- Traffic only in one direction.
- Each section of a bridge can be viewed as a
resource. - If a deadlock occurs, it can be resolved if one
car backs up (preempt resources and rollback). - Several cars may have to be backed up if a
deadlock occurs. - Starvation is possible.
15Deadlock Characterization
- Deadlock can arise if four conditions hold
simultaneously - Mutual exclusion only one process at a time can
use a resource. - Hold and wait a process holding at least one
resource is waiting to acquire additional
resources held by other processes. - No preemption a resource can be released only
voluntarily by the process holding it, after that
process has completed its task. - Circular wait there exists a set P0, P1, ,
Pn of waiting processes such that P0 is waiting
for a resource that is held by P1, , Pn1 is
waiting for a resource that is held by Pn, and Pn
is waiting for a resource that is held by P0.
16Resource-Allocation Graph
- A set of vertices V and a set of edges E.
- V is partitioned into two types
- P P1, P2, , Pn, the set consisting of all
the processes in the system. - R R1, R2, , Rm, the set consisting of all
resource types in the system. - request edge directed edge Pi ? Rj
- assignment edge directed edge Rj ? Pi
17Resource-Allocation Graph (Cont.)
- Process
- Resource Type with 4 instances
- Pi requests instance of Rj
- Pi is holding an instance of Rj
18Example of a Resource Allocation Graph
19Resource Allocation Graph With A Deadlock
20Resource Allocation Graph With A Cycle But No
Deadlock
21Basic Facts
- If graph contains no cycles ? no deadlock.
- If graph contains a cycle ?
- if only one instance per resource type, then
deadlock. - if several instances per resource type,
possibility of deadlock.