05-concurrency

Upload: vinay-varma

Post on 03-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 05-Concurrency

    1/75

    Chapter 5

    Concurrency: MutualExclusion andSynchronization

    Operating Systems:Internals and Design Principles, 6/E

    William Stallings

    Dave BremerOtago Polytechnic, N.Z.

    2008, Prentice Hall

  • 8/12/2019 05-Concurrency

    2/75

    Roadmap

    Principals of Concurrency Mutual Exclusion: Hardware Support

    Semaphores Monitors Message Passing

    Readers/Writers Problem

  • 8/12/2019 05-Concurrency

    3/75

    Multiple Processes

    Central to the design of modern OperatingSystems is managing multiple processes Multiprogramming Multiprocessing Distributed Processing

    Big Issue is Concurrency Managing the interaction of all of these

    processes

  • 8/12/2019 05-Concurrency

    4/75

    Concurrency

    Concurrency arises in: Multiple applications

    Sharing time Structured applications

    Extension of modular design

    Operating system structure OS themselves implemented as a set of

    processes or threads

  • 8/12/2019 05-Concurrency

    5/75

    Key Terms

  • 8/12/2019 05-Concurrency

    6/75

    Interleaving andOverlapping Processes

    Earlier (Ch2) we saw that processes maybe interleaved on uniprocessors

  • 8/12/2019 05-Concurrency

    7/75

    Interleaving andOverlapping Processes

    And not only interleaved but overlappedon multi-processors

  • 8/12/2019 05-Concurrency

    8/75

    Difficulties ofConcurrency

    Sharing of global resources Writing a shared variable: the order of writes

    is important Incomplete writes a major problem

    Optimally managing the allocation ofresources

    Difficult to locate programming errors asresults are not deterministic andreproducible.

  • 8/12/2019 05-Concurrency

    9/75

    A Simple Example

    void echo(){

    chin = getchar();chout = chin;putchar(chout);

    }

  • 8/12/2019 05-Concurrency

    10/75

    A Simple Example:On a Multiprocessor

    Process P1 Process P2. .

    chin = getchar(); .. chin = getchar();

    chout = chin; chout = chin;

    putchar(chout); .. putchar(chout);. .

  • 8/12/2019 05-Concurrency

    11/75

    Enforce Single Access

    If we enforce a rule that only one processmay enter the function at a time then:

    P1 & P2 run on separate processors P1 enters echo first,

    P2 tries to enter but is blocked P2 suspends

    P1 completes execution P2 resumes and executes echo

  • 8/12/2019 05-Concurrency

    12/75

    Race Condition

    A race condition occurs when Multiple processes or threads read and write

    data items They do so in a way where the final result

    depends on the order of execution of theprocesses.

    The output depends on who finishes therace last.

  • 8/12/2019 05-Concurrency

    13/75

    Operating SystemConcerns

    What design and management issues areraised by the existence of concurrency?

    The OS must Keep track of various processes Allocate and de-allocate resources Protect the data and resources against

    interference by other processes. Ensure that the processes and outputs are

    independent of the processing speed

  • 8/12/2019 05-Concurrency

    14/75

    Process Interaction

  • 8/12/2019 05-Concurrency

    15/75

    Competition amongProcesses for Resources

    Three main control problems: Need for Mutual Exclusion

    Critical sections

    Deadlock Starvation

  • 8/12/2019 05-Concurrency

    16/75

    Requirements forMutual Exclusion

    Only one process at a time is allowed inthe critical section for a resource

    A process that halts in its noncriticalsection must do so without interfering withother processes

    No deadlock or starvation

  • 8/12/2019 05-Concurrency

    17/75

    Requirements forMutual Exclusion

    A process must not be delayed access toa critical section when there is no otherprocess using it

    No assumptions are made about relativeprocess speeds or number of processes

    A process remains inside its critical sectionfor a finite time only

  • 8/12/2019 05-Concurrency

    18/75

    Roadmap

    Principals of Concurrency Mutual Exclusion: Hardware Support

    Semaphores Monitors Message Passing

    Readers/Writers Problem

  • 8/12/2019 05-Concurrency

    19/75

    Disabling Interrupts

    Uniprocessors only allow interleaving Interrupt Disabling

    A process runs until it invokes an operatingsystem service or until it is interrupted

    Disabling interrupts guarantees mutualexclusion

    Will not work in multiprocessor architecture

  • 8/12/2019 05-Concurrency

    20/75

    Pseudo-Code

    while (true) {/* disable interrupts */;/* critical section */;/* enable interrupts */;/* remainder */;

    }

  • 8/12/2019 05-Concurrency

    21/75

    Special MachineInstructions

    Compare&Swap Instruction also called a compare and exchange

    instruction

    Exchange Instruction

  • 8/12/2019 05-Concurrency

    22/75

    Compare&SwapInstruction

    int compare_and_swap (int *word,int testval, int newval)

    {int oldval;oldval = *word;if (oldval == testval) *word = newval;return oldval;

    }

  • 8/12/2019 05-Concurrency

    23/75

    Mutual Exclusion (fig 5.2)

  • 8/12/2019 05-Concurrency

    24/75

    Exchange instruction

    void exchange (int register, intmemory)

    {

    int temp;temp = memory;memory = register;register = temp;

    }

  • 8/12/2019 05-Concurrency

    25/75

    Exchange Instruction(fig 5.2)

  • 8/12/2019 05-Concurrency

    26/75

    Hardware MutualExclusion: Advantages

    Applicable to any number of processes oneither a single processor or multipleprocessors sharing main memory

    It is simple and therefore easy to verify It can be used to support multiple critical

    sections

  • 8/12/2019 05-Concurrency

    27/75

    Hardware MutualExclusion: Disadvantages

    Busy-waiting consumes processor time Starvation is possible when a process

    leaves a critical section and more than oneprocess is waiting. Some process could indefinitely be denied

    access.

    Deadlock is possible

  • 8/12/2019 05-Concurrency

    28/75

    Roadmap

    Principals of Concurrency Mutual Exclusion: Hardware Support

    Semaphores Monitors Message Passing

    Readers/Writers Problem

  • 8/12/2019 05-Concurrency

    29/75

    Semaphore

    Semaphore: An integer value used for signalling among

    processes.

    Only three operations may be performedon a semaphore, all of which are atomic: initialize,

    Decrement ( semWait ) increment. ( semSignal )

  • 8/12/2019 05-Concurrency

    30/75

    Semaphore Primitives

  • 8/12/2019 05-Concurrency

    31/75

    Binary SemaphorePrimitives

  • 8/12/2019 05-Concurrency

    32/75

    Strong/WeakSemaphore

    A queue is used to hold processes waitingon the semaphore In what order are processes removed from

    the queue? Stron g Sem aph ores use FIFO Weak Sem apho res dont specify the

    order of removal from the queue

  • 8/12/2019 05-Concurrency

    33/75

    Example of StrongSemaphore Mechanism

  • 8/12/2019 05-Concurrency

    34/75

    Example of SemaphoreMechanism

  • 8/12/2019 05-Concurrency

    35/75

    Mutual Exclusion UsingSemaphores

  • 8/12/2019 05-Concurrency

    36/75

    Processes UsingSemaphore

  • 8/12/2019 05-Concurrency

    37/75

    Producer/ConsumerProblem

    General Situation: One or more producers are generating data and

    placing these in a buffer

    A single consumer is taking items out of the bufferone at time Only one producer or consumer may access the

    buffer at any one time

    The Problem: Ensure that the Producer cant add data into full buffer

    and consumer cant remove data from empty buffer

    Producer/Consumer Animation

    http://gaia.ecs.csus.edu/~zhangd/oscal/ProducerConsumer/ProducerConsumer.html
  • 8/12/2019 05-Concurrency

    38/75

    Functions

    Producer Consumer

    while (true) {

    /* produce item v */

    b[in] = v;

    in++;

    }

    while (true) {

    while (in

  • 8/12/2019 05-Concurrency

    39/75

  • 8/12/2019 05-Concurrency

    40/75

    Incorrect Solution

  • 8/12/2019 05-Concurrency

    41/75

    Possible Scenario

  • 8/12/2019 05-Concurrency

    42/75

    Correct Solution

  • 8/12/2019 05-Concurrency

    43/75

    Semaphores

  • 8/12/2019 05-Concurrency

    44/75

  • 8/12/2019 05-Concurrency

    45/75

    Semaphores

  • 8/12/2019 05-Concurrency

    46/75

    Functions in aBounded Buffer

    Producer Consumer

    while (true) {

    /* produce item v */

    while ((in + 1) % n == out) /*do nothing */;

    b[in] = v;

    in = (in + 1) % n}

    while (true) {

    while (in == out)

    /* do nothing */;w = b[out];

    out = (out + 1) % n;

    /* consume item w */}

    .

  • 8/12/2019 05-Concurrency

    47/75

    Demonstration Animations

    Producer/Consumer Illustrates the operation of a producer-consumer

    buffer.

    Bounded-Buffer Problem Using Semaphores Demonstrates the bounded-buffer consumer/producer

    problem using semaphores.

    http://gaia.ecs.csus.edu/~zhangd/oscal/ProducerConsumer/ProducerConsumer.htmlhttp://gaia.ecs.csus.edu/~zhangd/oscal/semaphore/semaphore.htmlhttp://gaia.ecs.csus.edu/~zhangd/oscal/semaphore/semaphore.htmlhttp://gaia.ecs.csus.edu/~zhangd/oscal/semaphore/semaphore.htmlhttp://gaia.ecs.csus.edu/~zhangd/oscal/semaphore/semaphore.htmlhttp://gaia.ecs.csus.edu/~zhangd/oscal/ProducerConsumer/ProducerConsumer.html
  • 8/12/2019 05-Concurrency

    48/75

    Roadmap

    Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Monitors Message Passing

    Readers/Writers Problem

  • 8/12/2019 05-Concurrency

    49/75

    Monitors

    The monitor is a programming-languageconstruct that provides equivalentfunctionality to that of semaphores andthat is easier to control.

    Implemented in a number of programminglanguages, including Concurrent Pascal, Pascal-Plus, Modula-2, Modula-3, and Java.

  • 8/12/2019 05-Concurrency

    50/75

    Chief characteristics

    Local data variables are accessible onlyby the monitor

    Process enters monitor by invoking one ofits procedures

    Only one process may be executing in themonitor at a time

  • 8/12/2019 05-Concurrency

    51/75

  • 8/12/2019 05-Concurrency

    52/75

    Structure of a Monitor

  • 8/12/2019 05-Concurrency

    53/75

  • 8/12/2019 05-Concurrency

    54/75

    Solution Using Monitor

    B d d

  • 8/12/2019 05-Concurrency

    55/75

    BoundedBuffer Monitor

  • 8/12/2019 05-Concurrency

    56/75

    Roadmap

    Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Monitors Message Passing

    Readers/Writers Problem

  • 8/12/2019 05-Concurrency

    57/75

    Process Interaction

    When processes interact with one another,two fundamental requirements must besatisfied: synchronization and communication.

    Message Passing is one solution to thesecond requirement Added bonus: It works with shared memory

    and with distributed systems

  • 8/12/2019 05-Concurrency

    58/75

  • 8/12/2019 05-Concurrency

    59/75

    Bl ki g d

  • 8/12/2019 05-Concurrency

    60/75

    Blocking send,Blocking receive

    Both sender and receiver are blocked untilmessage is delivered

    Known as a rendezvous Allows for tight synchronization between

    processes.

  • 8/12/2019 05-Concurrency

    61/75

    Non-blocking Send

    More natural for many concurrentprogramming tasks.

    Nonblocking send, blocking receive Sender continues on Receiver is blocked until the requested

    message arrives

    Nonblocking send, nonblocking receive Neither party is required to wait

  • 8/12/2019 05-Concurrency

    62/75

    Addressing

    Sendin process need to be able to specifywhich process should receive themessage Direct addressing Indirect Addressing

  • 8/12/2019 05-Concurrency

    63/75

    Direct Addressing

    Send primitive includes a specific identifierof the destination process

    Receive primitive could know ahead oftime which process a message isexpected

    Receive primitive could use sourceparameter to return a value when thereceive operation has been performed

  • 8/12/2019 05-Concurrency

    64/75

    Indirect addressing

    Messages are sent to a shared datastructure consisting of queues

    Queues are called mailboxes One process sends a message to the

    mailbox and the other process picks upthe message from the mailbox

    Indirect Process

  • 8/12/2019 05-Concurrency

    65/75

    Indirect ProcessCommunication

  • 8/12/2019 05-Concurrency

    66/75

    General Message Format

    Mutual Exclusion Using

  • 8/12/2019 05-Concurrency

    67/75

    Mutual Exclusion UsingMessages

  • 8/12/2019 05-Concurrency

    68/75

  • 8/12/2019 05-Concurrency

    69/75

    Roadmap

    Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Monitors Message Passing

    Readers/Writers Problem

  • 8/12/2019 05-Concurrency

    70/75

    Readers/Writers Problem

    A data area is shared among manyprocesses Some processes only read the data area,

    some only write to the area Conditions to satisfy:

    1. Multiple readers may read the file at once.

    2. Only one writer at a time may write3. If a writer is writing to the file, no reader may

    read it.interaction of readers andwriters.

    http://gaia.ecs.csus.edu/~zhangd/oscal/ReaderWriter/ReaderWriter.html
  • 8/12/2019 05-Concurrency

    71/75

  • 8/12/2019 05-Concurrency

    72/75

    Writers have Priority

  • 8/12/2019 05-Concurrency

    73/75

    Writers have Priority

  • 8/12/2019 05-Concurrency

    74/75

    Message Passing

  • 8/12/2019 05-Concurrency

    75/75

    Message Passing