Operating Systems - CS604 Power Point Slides Lecture 28

download Operating Systems - CS604 Power Point Slides Lecture 28

of 45

description

Operating Systems - CS604 Power Point Slides Lecture 28

Transcript of Operating Systems - CS604 Power Point Slides Lecture 28

  • Operating Systems

    Lecture 28Syed Mansoor Sarwar

  • January 27, 2016 Copyright Virtual University of Pakistan

    Agenda for TodayReview of previous lectureDeadlock avoidanceBankers algorithmsSafety algorithmSafe SequenceRecap of lecture

  • January 27, 2016 Copyright Virtual University of Pakistan

    Review of Lecture 27Deadlock handlingDeadlock preventionDeadlock avoidance

  • January 27, 2016 Copyright Virtual University of Pakistan

    RAG AlgorithmClaim edge Pi Rj indicates

    that process Pi may request an instance of resource Rj; represented by a dashed line.

    Claim edge converts to request edge when a process requests a resource.

  • January 27, 2016 Copyright Virtual University of Pakistan

    When a resource is assigned to a process, request edge reconverts to an assignment edge.

    When a resource is released by a process, assignment edge reconverts to a claim edge.

    Resources must be claimed a priori in the system.

    RAG Algorithm

  • January 27, 2016 Copyright Virtual University of Pakistan

    RAG For Deadlock Avoidance

  • January 27, 2016 Copyright Virtual University of Pakistan

    Unsafe State In RAG

  • January 27, 2016 Copyright Virtual University of Pakistan

    Bankers Algorithm Multiple instances of resources. Each process must a priori claim

    maximum use. When a process requests a

    resource it may have to wait. When a process gets all its

    resources it must return them in a finite amount of time.

  • January 27, 2016 Copyright Virtual University of Pakistan

    Data Structures for the Bankers Algorithm

    n = number of processesm = number of resource typesAvailable: Vector of length m.

    If available[j] = k, there are k instances of resource type Rj available.

  • January 27, 2016 Copyright Virtual University of Pakistan

    Data Structures for the Bankers Algorithm

    Max: n x m matrix. If Max[i,j] = k, then process Pi may request at most k instances of resource type Rj.

    Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj.

  • January 27, 2016 Copyright Virtual University of Pakistan

    Data Structures for the Bankers Algorithm

    Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task.

    Need[i,j] =

    Max[i,j] Allocation [i,j]

  • January 27, 2016 Copyright Virtual University of Pakistan

    Safety Algorithm

    1. Let Work and Finish be vectors of length m and n, respectively. Initialize:Work = AvailableFinish[i] = false; i = 1, 2, , n.

  • January 27, 2016 Copyright Virtual University of Pakistan

    Safety Algorithm2. Find and i such that both:

    (a) Finish[i] = false (b) Needi Work If no such i exists, go to step 4.

  • January 27, 2016 Copyright Virtual University of Pakistan

    3. Work = Work + AllocationiFinish[i] = truego to step 2.

    4. If Finish [i] == true for all i, then the system is in a safe state.

    Safety Algorithm

  • January 27, 2016 Copyright Virtual University of Pakistan

    Requesti = request vector for Pi. If Requesti [j] = k then process Pi

    wants k instances of resource type Rj.

    1. If Requesti Needi go to step 2. Otherwise, report error since process has exceeded its maximum claim.

    Resource-Request Algorithm for Pi

  • January 27, 2016 Copyright Virtual University of Pakistan

    Resource-Request Algorithm for Pi

    2. If Requesti Available, go to step 3. Otherwise Pi must wait, since resources are not available.

    3. Pretend to allocate requested resources to Pi by modifying the state as follows:

  • January 27, 2016 Copyright Virtual University of Pakistan

    Available = Available Requestj;Allocationi = Allocationi + Requesti;Needi = Needi Requesti;

    If safe the resources are allocated to Pi.

    If unsafe Pi must wait, and the old resource-allocation state is restored

    Resource-Request Algorithm for Pi

  • January 27, 2016 Copyright Virtual University of Pakistan

    Example System

    Five processes: P0 P4 Three resource types: A (10 instances)B (5 instances)C (7 instances)

  • January 27, 2016 Copyright Virtual University of Pakistan

    Example System

    Process

    P0P1P2P3P4

    AvailableA B C3 3 2

    AllocationA B C0 1 02 0 03 0 22 1 10 0 2

    MaxA B C7 5 33 2 29 0 22 2 24 3 3

    System state: Is it safe?

  • January 27, 2016 Copyright Virtual University of Pakistan

    Needi = Maxi AllocationiNeed matrix

    Process A B CP0 7 4 3P1 1 2 2P2 6 0 0P3 0 1 1P4 4 3 1

    Example System

  • January 27, 2016 Copyright Virtual University of Pakistan

    Safe Sequence:

    Process

    P0P1P2P3P4

    WorkA B C3 3 2

    AllocationA B C0 1 02 0 03 0 22 1 10 0 2

    NeedA B C7 4 31 2 26 0 00 1 14 3 1

    Example System

  • January 27, 2016 Copyright Virtual University of Pakistan

    Process

    P0P1P2P3P4

    WorkA B C3 3 25 3 2

    AllocationA B C0 1 02 0 03 0 22 1 10 0 2

    NeedA B C7 4 31 2 26 0 00 1 14 3 1

    Safe Sequence: < P1>

    Example System

  • January 27, 2016 Copyright Virtual University of Pakistan

    Process

    P0P1P2P3P4

    WorkA B C3 3 25 3 27 4 3

    AllocationA B C0 1 02 0 03 0 22 1 10 0 2

    NeedA B C7 4 31 2 26 0 00 1 14 3 1

    Safe Sequence: < P1, P3>

    Example System

  • January 27, 2016 Copyright Virtual University of Pakistan

    Process

    P0P1P2P3P4

    WorkA B C3 3 25 3 27 4 37 4 5

    AllocationA B C0 1 02 0 03 0 22 1 10 0 2

    NeedA B C7 4 31 2 26 0 00 1 14 3 1

    Safe Sequence: < P1, P3, P4 >

    Example System

  • January 27, 2016 Copyright Virtual University of Pakistan

    Process

    P0P1P2P3P4

    WorkA B C3 3 25 3 27 4 37 4 57 5 5

    AllocationA B C0 1 02 0 03 0 22 1 10 0 2

    NeedA B C7 4 31 2 26 0 00 1 14 3 1

    Safe Sequence:

    Example System

  • January 27, 2016 Copyright Virtual University of Pakistan

    Final safe sequence:

    Not a unique sequence Possible safe sequences for the this

    example:, , , , ,

    Example System

  • January 27, 2016 Copyright Virtual University of Pakistan

    Example P1 requests (1,0,2). Can this

    request be granted immediately?

    1. Is Request1 Need1? (1,0,2) (1,2,2) true

    2. Is Request1 Available? (1,0,2) (3,3,2) true

  • January 27, 2016 Copyright Virtual University of Pakistan

    Example

    Process

    P0P1P2P3P4

    AvailableA B C3 3 2

    AllocationA B C0 1 02 0 03 0 22 1 10 0 2

    NeedA B C7 4 31 2 26 0 00 1 14 3 1

  • January 27, 2016 Copyright Virtual University of Pakistan

    Safe Sequence:

    Example

    Process

    P0P1P2P3P4

    WorkA B C2 3 0

    AllocationA B C0 1 03 0 23 0 22 1 10 0 2

    NeedA B C7 4 30 2 06 0 00 1 14 3 1

  • January 27, 2016 Copyright Virtual University of Pakistan

    Example

    Process

    P0P1P2P3P4

    WorkA B C2 3 05 3 2

    AllocationA B C0 1 03 0 23 0 22 1 10 0 2

    NeedA B C7 4 30 2 06 0 00 1 14 3 1

    Safe Sequence: < P1 >

  • January 27, 2016 Copyright Virtual University of Pakistan

    Example

    Process

    P0P1P2P3P4

    WorkA B C2 3 05 3 27 4 3

    AllocationA B C0 1 03 0 23 0 22 1 10 0 2

    NeedA B C7 4 30 2 06 0 00 1 14 3 1

    Safe Sequence: < P1, P3 >

  • January 27, 2016 Copyright Virtual University of Pakistan

    Example

    Process

    P0P1P2P3P4

    WorkA B C2 3 05 3 27 4 37 4 5

    AllocationA B C0 1 03 0 23 0 22 1 10 0 2

    NeedA B C7 4 30 2 06 0 00 1 14 3 1

    Safe Sequence: < P1, P3, P4 >

  • January 27, 2016 Copyright Virtual University of Pakistan

    Example

    Process

    P0P1P2P3P4

    WorkA B C2 3 05 3 27 4 37 4 57 4 5

    AllocationA B C0 1 03 0 23 0 22 1 10 0 2

    NeedA B C7 4 30 2 06 0 00 1 14 3 1

    Safe Sequence:

  • January 27, 2016 Copyright Virtual University of Pakistan

    Executing safety algorithm shows that sequence satisfies safety requirement.

    Yes, P1s request may be granted immediately.

    Example

  • January 27, 2016 Copyright Virtual University of Pakistan

    Can request for (0,2,0) by P0 be granted?

    1. Is Request0 Need0? (0,2,0) (7,4,3) true

    2. Is Request1 Available? (0,2,0) (3,3,2) true

    More Examples

  • January 27, 2016 Copyright Virtual University of Pakistan

    Example

    Process

    P0P1P2P3P4

    AvailableA B C3 3 2

    AllocationA B C0 1 02 0 03 0 22 1 10 0 2

    NeedA B C7 4 31 2 26 0 00 1 14 3 1

  • January 27, 2016 Copyright Virtual University of Pakistan

    Safe Sequence:

    Example

    Process

    P0P1P2P3P4

    WorkA B C3 1 2

    AllocationA B C0 3 02 0 03 0 22 1 10 0 2

    NeedA B C7 2 31 2 26 0 00 1 14 3 1

  • January 27, 2016 Copyright Virtual University of Pakistan

    Safe Sequence: < P3 >

    Example

    Process

    P0P1P2P3P4

    WorkA B C3 1 25 2 3

    AllocationA B C0 3 02 0 03 0 22 1 10 0 2

    NeedA B C7 2 31 2 26 0 00 1 14 3 1

  • January 27, 2016 Copyright Virtual University of Pakistan

    Safe Sequence: < P3,P1 >

    Example

    Process

    P0P1P2P3P4

    WorkA B C3 1 25 2 37 2 3

    AllocationA B C0 3 02 0 03 0 22 1 10 0 2

    NeedA B C7 2 31 2 26 0 00 1 14 3 1

  • January 27, 2016 Copyright Virtual University of Pakistan

    Safe Sequence: < P3,P1,P2 >

    Example

    Process

    P0P1P2P3P4

    WorkA B C3 1 25 2 37 2 3

    10 2 5

    AllocationA B C0 3 02 0 03 0 22 1 10 0 2

    NeedA B C7 2 31 2 26 0 00 1 14 3 1

  • January 27, 2016 Copyright Virtual University of Pakistan

    Safe Sequence:

    Example

    Process

    P0P1P2P3P4

    WorkA B C3 1 25 2 37 2 3

    10 2 510 5 5

    AllocationA B C0 3 02 0 03 0 22 1 10 0 2

    NeedA B C7 2 31 2 26 0 00 1 14 3 1

  • January 27, 2016 Copyright Virtual University of Pakistan

    Executing safety algorithm shows that sequence satisfies safety requirement.

    Yes, P0s request may be granted immediately.

    Example

  • January 27, 2016 Copyright Virtual University of Pakistan

    Can request for (3,3,0) by P4 be granted?

    Do it yourself!

    More Examples

  • January 27, 2016 Copyright Virtual University of Pakistan

    Recap of Lecture

    Deadlock avoidance Bankers algorithm Safety algorithm Safe state Safe sequence

  • Operating Systems

    Lecture 28Syed Mansoor Sarwar

    Operating SystemsSlide 2Slide 3Slide 4Slide 5Slide 6Slide 7Bankers AlgorithmData Structures for the Bankers AlgorithmSlide 10Slide 11Safety AlgorithmSlide 13Slide 14Resource-Request Algorithm for PiSlide 16Slide 17Example SystemSlide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26ExampleSlide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34More ExamplesSlide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45