Operating Systems Lecture 18 - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Operating Systems Lecture 18

Description:

the ostrich algorithm. Resources. NENU - Operating ... The Ostrich Algorithm ... this is the ostrich algorithm': stick your head in the sand and pretend that ... – PowerPoint PPT presentation

Number of Views:128
Avg rating:3.0/5.0
Slides: 25
Provided by: jongar
Category:

less

Transcript and Presenter's Notes

Title: Operating Systems Lecture 18


1
Operating SystemsLecture 18
  • Interprocess CommunicationDeadlocks

phones off (please)
2
Overview
  • Resources
  • Deadlocks
  • conditions for deadlock
  • deadlock modelling
  • Deadlock strategies
  • deadlock avoidance
  • deadlock prevention
  • deadlock detection recovery
  • the ostrich algorithm

3
Resources
4
Recall - Shared Resources
  • Processes need to share resources
  • physical resources
  • processor, memory, I/O devices, disks, etc.
  • logical resources
  • message queues, inodes, records in a database
    system, etc.
  • A computer will normally have many different
    resources that can be acquired
  • for some, several identical instances may be
    available
  • e.g. multiple printers or tape drives
  • the OS may choose any of them to satisfy a
    request
  • A resource is anything that can only be used by a
    single process at any instant of time

5
Recall - Deadlocks
  • All operating systems have the ability to grant
    (temporary) exclusive access to certain resources
  • often exclusive access is needed to gt1 resource
  • suppose there are two processes, A and B, and two
    shared resources, printer and tape
  • A allocates the printer
  • B allocates the tape
  • then
  • A requests the tape and blocks until B releases
    it
  • and then
  • B requests the printer and blocks until A
    releases it
  • both processes block forever - this is a deadlock

6
Resource Classification
  • Resources come in two main types
  • pre-emptable resources
  • such a resource can be temporarily taken away
    from the process owning it with no ill effects
  • e.g. processor, memory
  • non-preemptable resources
  • once allocated to a process, removing such a
    resource (even temporarily) could cause the
    computation to fail
  • e.g. printers
  • if a process has begun to print output, then
    taking the printer away and allowing another
    process to write to it will result in garbled
    output
  • Deadlocks occur with non-preemptable resources
  • pre-emptable resources can be reallocated to
    avoid DL

7
Resource Allocation
  • Sequence of events needed to use a resource is
  • request the resource
  • use the resource
  • release the resource
  • If the resource is not available when its
    requested, the requesting process is forced to
    wait
  • in some systems, the process is automatically
    blocked
  • in others, the allocation fails and returns an
    error
  • the process decides whether to wait some time and
    try again
  • in most cases the process will sit in a tight
    loop requesting the resource until it is
    available (as no other work can be done)
  • in which case, the process is effectively blocked

8
Deadlocks
9
Deadlock Definition
  • Deadlock can be defined formally as
  • a set of processes is deadlocked if each process
    in the set is waiting for an event that only
    another process in the set can cause
  • In most cases, the event that each process is
    waiting for is the release of a resource
    currently held by another member of the set
  • e.g. suppose there are three processes A, B C
  • one form of deadlock is
  • A waits for B, B waits for C, C waits for A
  • another may be
  • A waits for B, B waits for A, C waits for B

10
Conditions for Deadlock
  • A deadlock can only occur if the following four
    conditions hold simultaneously in a system
  • mutual exclusion
  • each resource is currently assigned on only one
    process or is available for allocation
  • hold and wait
  • processes currently holding a resource granted
    earlier can request additional resources
  • no pre-emption
  • allocated resources cannot be taken away
  • circular wait
  • there must be a circular chain of two or more
    processes, each of which is waiting for a
    resource held by the next in the chain

11
Deadlock Modelling
  • Deadlocks can be modelled using directed graphs
  • processes are shown as circles
  • resources are shown as squares
  • an arrow from a resource (square) to a process
    (circle) indicates that the resource is allocated
    to the process
  • an arrow from a process (circle) to a resource
    (square) indicates the process is blocked waiting
    for the resource

12
Example Scenario
  • Imagine there are three processes A, B C
  • and three resources R, S, T
  • The resource allocations are as follows
  • If the operating system runs the processes to
    completion in order A, B then C
  • all requests can be satisfied no deadlock occurs
  • But, suppose there is pre-emptive multi-tasking

13
Unpredictable Results?
  • A C run in round-robin, then B

A requests R
C requests T
A
B
C
A requests S
C requests R
A releases R
C releases T
A releases S
C releases R
R
S
T
B requests S
B requests T
B releases S
B releases T
  • A, B, C all run in round-robin

A
B
C
A requests R
B requests S
C requests T
A requests S
B requests T
R
S
T
C requests R
DEADLOCK!
14
Deadlock Strategies
15
Strategies Available
  • Scheduling choice can avoid or cause deadlock
  • a resource graph can be created checked each
    step
  • if it contains a cycle, then there is a deadlock
  • In general, there are four strategies available
    for dealing with deadlocks
  • dynamic avoidance through careful resource
    allocation and process re-scheduling
  • prevention, by ensuring that at least one of the
    four conditions required cannot hold
  • detection and recovery
  • ignore the problem altogether!

16
Potential Deadlock Example
  • As a potential deadlock example in UNIX
  • UNIX has a finite maximum size to its process
    table
  • usually around 32,000 processes
  • if a fork fails (because the process table is
    full), then a reasonable strategy (used in
    practice by most UNIX programmers) is to wait a
    short time and try again
  • Suppose there are 100 slots free in process table
  • ten processes run, each creates 12
    (sub-)processes
  • each loops 12 times, forking and / or waiting
  • the scheduler starts them all running in
    round-robin
  • each forks 9 child processes - the process table
    is full
  • all ten processes wait forever to create the
    required children

17
Deadlock Avoidance
  • The concept of deadlock avoidance is for the
    operating system to monitor the state of the
    system and to re-schedule the order of running
    processes such that deadlock does not occur
  • The OS must keep track of the current state and
    determine whether it is safe or unsafe
  • the system is in a safe state if
  • it is not deadlocked and there is some way to
    satisfy all the pending resource requests by
    running the jobs in some order
  • the system is in an unsafe state if
  • there is no such safe order and the occurrence or
    otherwise of deadlock is purely dependent on the
    process behaviour

18
The Bankers Algorithm
  • The bankers algorithm
  • initialise an array with the number of each
    resource
  • for each process keep an array of resources in
    use and resources still needed
  • whenever a resource request is made by a process
  • look for a process that can run to completion
  • resources still needed are less than the
    available number
  • assume this process runs and frees its resources
  • continue, until all processes are verified to be
    runnable
  • if all processes can complete safe, grant the
    request
  • else unsafe, deny the request
  • Unfortunately, this depends on future knowledge!
  • and so is impossible in practice

19
Deadlock Prevention
  • Attempt to ensure that at least one of the four
    conditions required for deadlock cannot hold
  • mutual exclusion
  • if no resource was ever assigned exclusively to a
    single process, then deadlock would never occur -
    but how?
  • the spooling concept can avoid exclusive access
  • instead of allocating printers, each process
    prints to a spool queue
  • a single central spooling demon accesses the
    printers prints jobs
  • this is a good idea, but not all resources can be
    spooled
  • hold and wait
  • this is avoided if all processes are forced to
    request all their resources before starting
    execution do-able, but inflexible
  • any process requesting a new resource temporarily
    frees its held resources and then requests all
    do-able, but costly

20
Deadlock Prevention - Cont.
  • Lets look at the other two conditions
  • no pre-emption
  • the only way to prevent this condition is to
    demand that any resource may be pre-empted this
    is impossible at worst
  • how can a printer be removed from a process
    during printing
  • or just very difficult at best
  • perhaps in combination with the spooling idea for
    e.g. printers
  • circular wait
  • a simple way to avoid this is to insist that a
    process may only allocate a single resource at a
    time, but this is too inflexible
  • a practical way is to give each resource a
    priority number
  • if resources are priority ordered, e.g. R, S,
    then T, no process can allocate T before R or S
    and circular waits are avoided
  • deciding a priority order for all resources may
    be too difficult

21
Deadlock Detection
  • In the detection and recovery approach, the
    system does not try to prevent deadlock occurring
  • it simply detects when a deadlock has occurred
    and then (automatically) takes some action to
    recover
  • The first step is, obviously, to detect the
    deadlock
  • if there is a single instance of each resource
    type
  • a resource allocation graph can be constructed
  • there are many published algorithms for finding
    cycles (deadlocks) in such directed graphs
  • if there are multiple instances of resource types
  • an algorithm very similar to the bankers
    algorithm is used (but it does not require future
    knowledge to detect deadlock)

22
Deadlock Recovery
  • Deadlock recovery methods include
  • recovery through pre-emption
  • a deadlocked resource is taken away
  • again this causes big problems for resources such
    as printers
  • may be possible to carefully choose the
    pre-emption to make this work
  • recovery through rollback
  • each resource allocation causes some form of
    checkpoint for the process to be saved by the
    operating system
  • similar to a process table entry for a pre-empted
    process
  • on deadlock, processes rewound to free required
    resources
  • may be very difficult costly, if e.g. files
    have been written to
  • recovery through process termination
  • crudest method is to kill one or more deadlocked
    processes
  • with luck the deadlock with cease, if not kill
    more processes!
  • but how can processes be killed without causing
    damage?

23
The Ostrich Algorithm
  • Surprisingly, perhaps, ignoring the problem
    altogether is a popular choice of algorithm!
  • this is the ostrich algorithm stick your head
    in the sand and pretend that there is no problem
    at all
  • Is this acceptable?
  • mathematicians say no
  • engineers say calculate how often its likely to
    occur, and if that is infrequent enough, then
    its tolerable
  • computer scientists say sure ... everyone
    expects a computer to hang or crash
    occasionally!
  • Most contemporary OSs, including both UNIX and
    Windows NT, use the ostrich algorithm

24
Summary
  • Resources
  • Deadlocks
  • conditions for deadlock
  • deadlock modelling
  • Deadlock strategies
  • deadlock avoidance
  • deadlock prevention
  • deadlock detection recovery
  • the ostrich algorithm
Write a Comment
User Comments (0)
About PowerShow.com