Session 5 - Process Synchronization

download Session 5 - Process Synchronization

of 14

Transcript of Session 5 - Process Synchronization

  • 8/8/2019 Session 5 - Process Synchronization

    1/14

    OPERATING SYSTEMSDesign and Implementation

    Chapter 2Processes

    Instructor:Hadi Salimi

    Computer Engineering Department

    IRAN University of Science and [email protected]

    IPC

    Processes frequently need to

    communicate with other processes

    For example, in a shell pipeline, the output

    of the first rocess must be assed to the

    Operating Systems - By: Hadi Salimi - IUST-CE 25/19/2009

    second process. This is called Inter-Process

    Communication orIPC.

  • 8/8/2019 Session 5 - Process Synchronization

    2/14

    Race Conditions

    Operating Systems - By: Hadi Salimi - IUST-CE 35/19/2009

    Two processes want to access shared memory at same time. What

    happens if they try to access it simultaneously?

    Race Conditions

    Situations like this are called race

    conditions.

    What will happen if two processes execute

    the followin code?

    Operating Systems - By: Hadi Salimi - IUST-CE 45/19/2009

    X=0;

    Read(x);

    X++;

    Write(x);

  • 8/8/2019 Session 5 - Process Synchronization

    3/14

    Critical Sections

    We should prohibit more than one process

    from reading and writing the shared data

    at the same time.

    In other words what we need is mutual

    Operating Systems - By: Hadi Salimi - IUST-CE 55/19/2009

    exclusion.

    The part of the program where the shared

    memory is accessed is called the critical

    section orcritical region.

    Solution Criteria

    Conditions to hold to have a good solution:

    No two processes may be simultaneously

    inside their critical regions.

    No assumptions may be made about speeds

    Operating Systems - By: Hadi Salimi - IUST-CE 65/19/2009

    or the number of CPUs.No process running outside its critical section

    may block other processes.

    No process should have to wait forever to

    enter its critical region.

  • 8/8/2019 Session 5 - Process Synchronization

    4/14

    Mutual Exclusion

    Busy Waiting

    Disabling interrupts

    Strict alternation

    Petersons solution

    Operating Systems - By: Hadi Salimi - IUST-CE 75/19/2009

    TSL instruction

    Sleep and Wakeup

    Semaphores

    Monitors

    Message passing

    Disabling Interrupts

    Let each process to disable all interrupts

    just after entering its critical section and

    re-enable them just before leaving.

    Is it wise to let the rocesses do such a ob?

    Operating Systems - By: Hadi Salimi - IUST-CE 85/19/2009

    What will happen if one process never turnthe interrupts on?

    What if the program is running on a

    multiprocessor?

  • 8/8/2019 Session 5 - Process Synchronization

    5/14

    Lock Variables

    Does the following code solve the

    problem?

    If (lock ==0) then

    Operating Systems - By: Hadi Salimi - IUST-CE 95/19/2009

    lock = 1;

    Begin Critical Section

    lock = 0;

    End Critical Section

    Strict Alternation

    Operating Systems - By: Hadi Salimi - IUST-CE 105/19/2009

    Which condition is violated?

  • 8/8/2019 Session 5 - Process Synchronization

    6/14

    Petersons Algorithm

    Operating Systems - By: Hadi Salimi - IUST-CE 115/19/2009

    TSL Instruction

    It is another proposal that requires a little

    help from hardware.

    Many computers have a TEST AND LOCK

    instruction that works as follows:

    Operating Systems - By: Hadi Salimi - IUST-CE 125/19/2009

    Reads the memory into a register and storesa non-zero value into it.

  • 8/8/2019 Session 5 - Process Synchronization

    7/14

    TSL (cont.)

    Operating Systems - By: Hadi Salimi - IUST-CE 135/19/2009

    Sleep and Wakeup

    The described solutions requiring busy

    waiting.

    Operating Systems - By: Hadi Salimi - IUST-CE 145/19/2009

  • 8/8/2019 Session 5 - Process Synchronization

    8/14

    Sleep and Wakeup

    Semaphores

    Monitors Message Passing

    Operating Systems - By: Hadi Salimi - IUST-CE 155/19/2009

    Sleep and Wakeup

    Despite busy waiting methods which

    waste CPU cycles, in this method

    processes may sleep orwakeup using

    system calls.

    Operating Systems - By: Hadi Salimi - IUST-CE 165/19/2009

    Lets clarify this approach using anexample, namely, producers and

    consumers.

  • 8/8/2019 Session 5 - Process Synchronization

    9/14

    Sleep and Wakeup

    Consider two

    processes

    which produce

    and consume

    Consumer

    Process

    Producer

    Process

    Operating Systems - By: Hadi Salimi - IUST-CE 175/19/2009

    items from/to

    a buffer with

    size N.

    Producer/Consumer

    Operating Systems - By: Hadi Salimi - IUST-CE 185/19/2009

  • 8/8/2019 Session 5 - Process Synchronization

    10/14

    Problems

    This solution is also wrong.

    Consider the situations in which both theproducer and the consumer access the

    shared variable count simultaneousl .

    Operating Systems - By: Hadi Salimi - IUST-CE 195/19/2009

    This may cause both of the processes go

    sleep.

    Semaphores

    In many problems there is a need to count

    an event, like producing an item or

    consuming it.

    Accessin to this counter should be

    Operating Systems - By: Hadi Salimi - IUST-CE 205/19/2009

    protected against concurrent processes. Such a protected counter is called a

    semaphore which has more features.

  • 8/8/2019 Session 5 - Process Synchronization

    11/14

    Semaphores (cont.)

    Two operators are defined on a

    semaphore: Down and Up (generalizations

    ofsleep and wakeup)

    Operating Systems - By: Hadi Salimi - IUST-CE 215/19/2009

    If (x > 0)

    x--;

    else

    Sleep() };

    If (there is any waiting process)Pick a process from queue and make it

    ready;

    else

    x++ };

    Semaphores (cont.)

    How to protect a critical section usingsemaphores?

    int s = 1;

    Operating Systems - By: Hadi Salimi - IUST-CE 225/19/2009

    Critical Section

    Up(s);

  • 8/8/2019 Session 5 - Process Synchronization

    12/14

    Semaphores (cont.)

    Consider a resource which can be shared by 3

    processes. How accessing this device can be

    protected using semaphores?

    int x = 3;

    Operating Systems - By: Hadi Salimi - IUST-CE 235/19/2009

    Down(x);

    Accessing the shared resource.

    Up(x);

    Producer/Consumer

    What happens if the

    down operator first

    Operating Systems - By: Hadi Salimi - IUST-CE 245/19/2009

    applied on mutex?

  • 8/8/2019 Session 5 - Process Synchronization

    13/14

    Monitors

    The unfortunate situation which causes no

    process to proceed is called deadlock.

    We study it in detail in the next chapter.

    Operating Systems - By: Hadi Salimi - IUST-CE 255/19/2009

    careful one must be when using

    semaphores.

    Monitors (cont.)

    To make it easier to write correct

    programs, a higher level primitive called

    monitoris introduced.

    It is a collection of rocedures variables

    Operating Systems - By: Hadi Salimi - IUST-CE 265/19/2009

    and data structures that are all grouped ina package.

    An important property:

    Only one process can be active in a monitor

    at any time.

  • 8/8/2019 Session 5 - Process Synchronization

    14/14

    Monitors (cont.)

    monitorexample

    int x;procedure producer(x)

    Operating Systems - By: Hadi Salimi - IUST-CE 275/19/2009

    end;

    procedure consumer(x)

    end;

    Monitors (cont.)

    Monitors are a programming language

    construct, so the compiler should handle

    calls to procedures.

    When a rocess calls a monitor

    Operating Systems - By: Hadi Salimi - IUST-CE 285/19/2009

    procedure, the first few instructions of theprocedure will check to see if any other

    process is currently active or not.