OS-07

download OS-07

of 32

Transcript of OS-07

  • 8/7/2019 OS-07

    1/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 1

    Process Synchronisation

    Background

    CriticalSection Problem

    Semaphores

    Classical Problems of Synchronization

    Monitors

  • 8/7/2019 OS-07

    2/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 2

    Background

    Cooperating Process

    Affects or affected by other processes

    executing in the system.

    Allowed to share data

    through files.

    Concurrent access to shared data

    may result in data inconsistency.

  • 8/7/2019 OS-07

    3/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 3

    Background

    Race Condition

    Situation where several processes

    access and manipulate shared data concurrently.

    Final value of the shared data

    depends upon which process finishes last.

    Synchronising concurrent processes

    prevents race conditions.

  • 8/7/2019 OS-07

    4/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 4

    CriticalSection Problem

    Critical Section

    Code segment of each process.

    Accesses shared data.

    When one process is executing in its critical section,no other process is allowed to execute in its critical section.

    Execution of critical sections by the processes

    is mutually exclusive in time.

  • 8/7/2019 OS-07

    5/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 5

    CriticalSection ProblemProtocol design that the processes use to cooperate.

    Requirements for solution to CriticalSection Problem1. Mutual ExclusionIf process Pi is executing in its critical section,

    then no other processes can be executing in their critical sections.

    2. Progress

    If no process is executing in its critical section andthere exist some processes that wish to enter their critical section, then

    the selection of the processes that will enter the critical section next

    cannot be postponed indefinitely.

    3. Bounded WaitingA bound must exist on the no. of times that

    other processes are allowed to enter their critical sections

    after a process has made a request to enter its critical section and

    before that request is granted.

  • 8/7/2019 OS-07

    6/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 6

    CriticalSection Problem

    General Structure of a typical process Pi

    do {

    entry section

    critical section

    exit section

    reminder section

    } while (1);

  • 8/7/2019 OS-07

    7/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 7

    CriticalSection Problem

    TwoProcess SolutionsO

    ne Process PiOther Process Pj

    Algorithm 1Shared variables:

    int turn = 0;

    Structure of Process Pido {

    while (turn != i);

    critical section

    turn = j;

    reminder section} while (1);

    Satisfies mutual exclusion, but not progress.

  • 8/7/2019 OS-07

    8/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 8

    CriticalSection Problem

    Algorithm 2Shared variables

    boolean flag[2] = false;

    Structure of Process Pido {

    flag [i] := true;

    while (flag [j]);

    critical section

    flag [i] = false;

    remainder section

    } while (1);

    Satisfies mutual exclusion, but not progress requirement.

  • 8/7/2019 OS-07

    9/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 9

    CriticalSection Problem

    Algorithm 3Combines the key ideas of algorithms 1 and 2.

    Shared variables

    boolean flag[2] = false;

    int turn = 0;

    Structure of Process Pi

    do {flag [i]:= true;

    turn = j;

    while (flag [j] && turn == j);

    critical section

    flag [i] = false;

    remainder section

    } while (1);

    Meets all the requirements

    and solves the CriticalSection problem for two processes.

  • 8/7/2019 OS-07

    10/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 10

    CriticalSection Problem

    MultipleProcess Solutions Bakery Algorithm

    CriticalSection problem for n processes.

    Processes enter their critical section on a first-come, first-served basis.

    Before entering its critical section, process receives a no.

    Holder of the smallest no. enters the critical section.

    If processes Piand Pjreceive the same no., ifi< j, then

    Piis served first; else

    Pjis served first.

    The numbering scheme always generatesnos. in increasing order of enumeration.

    i.e., 1,2,3,3,3,3,4,5, .

  • 8/7/2019 OS-07

    11/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 11

    Semaphores

    Busy waiting / Spinlock

    While a process is in its critical section,any other process that tries to enter its critical section

    must loop continuously in the entry code.

    SemaphoreSynchronization tool that does not require busy waiting.

    Semaphore SInteger variable can only be accessed via

    two indivisible (atomic) operations:

    P (for wait; from the Dutch Proberen, to test)

    V (for signal; from the Dutch Verhogen, to increment)

  • 8/7/2019 OS-07

    12/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 12

    Semaphores

    Classical definition of wait

    wait(S) {while (S e 0)

    ; //no-op

    S--;

    }

    Classical definition of signalsignal(S) {

    S++;

    }

  • 8/7/2019 OS-07

    13/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 13

    Semaphores

    Deals with Critical Section ofn Processes.

    Shared data:

    semaphore mutex = 1

    Structure of Process Pi:

    do {

    wait (mutex);critical section

    signal (mutex);

    remainder section

    } while (1);

  • 8/7/2019 OS-07

    14/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 14

    Semaphores

    Operations

    blocksuspends the process that invokes it.

    wakeup(P)

    resumes the execution of a blocked process P.

  • 8/7/2019 OS-07

    15/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 15

    Semaphores

    Deadlocks and Starvation

    DeadlockTwo or more processes waiting indefinitely for an eventthat can be caused by only one of the waiting processes.

    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);

    StarvationIndefinite blocking.

    A process may never be removed from the semaphore queue

    in which it is suspended.

  • 8/7/2019 OS-07

    16/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 16

    Semaphores

    Types of Semaphores

    Counting Semaphore

    Integer value

    range over an unrestricted domain.

    Binary Semaphore

    Integer value

    range only between 0 and 1.

    Simple to implement.

  • 8/7/2019 OS-07

    17/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 17

    Semaphores

    Implementing a Counting semaphore S as a Binary semaphore

    Data structures:

    binary-semaphore S1, S2;

    int C;

    Initialization:

    S1 = 1S2 = 0

    C = initial value of semaphore S

  • 8/7/2019 OS-07

    18/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 18

    Semaphores

    Implementing a Counting semaphore S as a Binary semaphore

    waitoperation on the counting semaphore S

    wait (S1);

    C--;

    if (C < 0) {signal (S1);

    wait (S2);

    }

    signal (S1);

  • 8/7/2019 OS-07

    19/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 19

    Semaphores

    Implementing a Counting semaphore S as a Binary semaphore

    Signaloperation on the counting semaphore S

    wait (S1);

    C++;

    if (C

  • 8/7/2019 OS-07

    20/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 20

    Classical Problems of Synchronization

    BoundedBuffer Problem

    Readers and Writers Problem

    DiningPhilosophers Problem

  • 8/7/2019 OS-07

    21/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 21

    Classical Problems of Synchronization

    BoundedBuffer Problem

    Used to illustrate the power of synchronisation primitives.

    Consists of n buffers, each capable of holding one item.

    Shared datasemaphore full, empty, mutex;

    Initialisation:

    full = 0, empty = n, mutex = 1

  • 8/7/2019 OS-07

    22/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 22

    Classical Problems of Synchronization

    BoundedBuffer Problem

    Structure of Producer Process

    Producerproduce full buffers for the Consumer.

    do {

    produce an item in nextp

    wait (empty);

    wait (mutex);

    add nextp to buffer

    signal (mutex);

    signal (full);

    } while (1);

  • 8/7/2019 OS-07

    23/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 23

    Classical Problems of Synchronization

    BoundedBuffer Problem

    Structure of Consumer Process

    Consumerproduce empty buffers for the Producer.

    do {

    wait (full)

    wait (mutex);

    remove an item from buffer to nextc

    signal (mutex);

    signal (empty);

    consume the item in nextc

    } while (1);

  • 8/7/2019 OS-07

    24/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 24

    Classical Problems of Synchronization

    ReadersWriters Problem

    A data object (file or record) shared among several concurrent processes.

    Reader processesInterested only reading

    the content of the shared object.No adverse effects, if no. of readers access

    the shared data simultaneously.

    Writer processes

    Interested only updating (reading and writing)the content of the shared object.

    Each process has exclusive access to

    the shared data.

  • 8/7/2019 OS-07

    25/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 25

    Classical Problems of Synchronization

    ReadersWriters Problem

    Shared data

    semaphore mutex, wrt;

    Initially mutex = 1, wrt = 1, readcount = 0

    Structure of Writer Processwait (wrt);

    writing is performed

    signal (wrt);

  • 8/7/2019 OS-07

    26/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 26

    Classical Problems of Synchronization

    ReadersWriters Problem

    Structure of Reader Processwait (mutex);

    readcount++;

    if (readcount == 1)

    wait (wrt);signal (mutex);

    reading is performed

    wait (mutex);

    readcount--;

    if (readcount == 0)

    signal (wrt);

    signal (mutex):

  • 8/7/2019 OS-07

    27/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 27

    Classical Problems of Synchronization

    DiningPhilosophers Problem

  • 8/7/2019 OS-07

    28/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 28

    Classical Problems of Synchronization

    DiningPhilosophers ProblemShared data

    semaphore chopstick[5];

    Initially all values are 1.

    Structure of Philosopher i:

    do {

    wait (chopstick[i])

    wait (chopstick[(i+1) % 5])

    eat

    signal (chopstick[i]);

    signal (chopstick[(i+1) % 5]);

    think

    } while (1);

  • 8/7/2019 OS-07

    29/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 29

    Monitors

    High-level synchronisation construct.

    Characterised by set of programmer-defined operations.

    Allows the safe sharing of an abstract data type

    among concurrent processes.

  • 8/7/2019 OS-07

    30/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 30

    Monitors

    Syntax of a Monitor

    monitor monitor-name {

    shared variable declarations

    procedure body P1 () {

    ...

    }

    procedure body P2 () {...

    }

    procedure body Pn () {

    ...

    }

    {

    initialization code

    }

    }

  • 8/7/2019 OS-07

    31/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 31

    Monitors

    Schematic View of a Monitor

  • 8/7/2019 OS-07

    32/32

    Prof. D.S.R. Murthy OS-7 Process Synchronisation 32

    Monitors

    Monitor With Condition Variables