ipc problems solutions

Upload: prasad-mullappilly

Post on 14-Apr-2018

277 views

Category:

Documents


1 download

TRANSCRIPT

  • 7/30/2019 IPC Problems Solutions

    1/23

    IPC Problems

    In this section of the tutorial module, we look at two different interprocess communication problems. In each subsection, the problem is

    presented, followed by explanations of possible solutions using each of the three types of interprocess communication we have discussed.

    THE AQUA LINE

    Producer/Consumer Problems

    Readers/Writers Problems

    IPC Home Subway Solution Demos

    Page Map Types

    http://cs.gmu.edu/cne/modules/ipc/green/demos.htmlhttp://cs.gmu.edu/cne/modules/ipc/green/solution.htmlhttp://cs.gmu.edu/cne/modules/ipc/map.htmlhttp://cs.gmu.edu/cne/modules/ipc/index.html
  • 7/30/2019 IPC Problems Solutions

    2/23

    The Producer Consumer Problem

    The producer-consumer problem illustrates the need for synchronization in systems where many processes share a resource. In the

    problem, two processes share a fixed-size buffer. One process produces information and puts it in the buffer, while the other process

    consumes information from the buffer. These processes do not take turns accessing the buffer, they both work concurrently. Herein lies

    the problem. What happens if the producer tries to put an item into a full buffer? What happens if the consumer tries to take an item froman empty buffer?

    In order to synchronize these processes, we will block the producer when the buffer is full, and we will block the consumer when

    the buffer is empty. So the two processes, Producer and Consumer, should work as follows:

  • 7/30/2019 IPC Problems Solutions

    3/23

    (1) The producer must first create a new widget. (2) Then, it checks to see if the buffer is full. If it is, the producer will put itself to sleep

    until the consumer wakes it up. A "wakeup" will come if the consumer finds the buffer empty.(3) Next, the producer puts the new widget

    in the buffer. If the producer goes to sleep in step (2), it will not wake up until the buffer is empty, so the buffer will never overflow. (4)

    Then, the producer checks to see if the buffer is empty. If it is, the producer assumes that the consumer is sleeping, an so it will wake the

    consumer. Keep in mind that between any of these steps, an interrupt might occur, allowing the consumer to run.

  • 7/30/2019 IPC Problems Solutions

    4/23

    (1) The consumer checks to see if the buffer is empty. If so, the consumer will put itself to sleep until the producer wakes it up. A

    "wakeup" will occur if the producer finds the buffer empty after it puts an item into the buffer. (2) Then, the consumer will remove a

    widget from the buffer. The consumer will never try to remove a widget from an empty buffer because it will not wake up until the bufferis full.(3) If the buffer was full before it removed the widget, the consumer will wake the producer. (4) Finally, the consumer will

    consume the widget. As was the case with the producer, an interrupt could occur between any of these steps, allowing the producer to run.

    The code might look like this:

  • 7/30/2019 IPC Problems Solutions

    5/23

    BufferSize = 3;

    count = 0;

    Producer()

    {

    int widget;

    WHILE (true) { // loop forever

    make_new(widget); // create a new widget to put in the buffer

    IF(count==BufferSize)

    Sleep(); // if the buffer is full, sleep

    put_item(widget); // put the item in the buffer

    count = count + 1; // increment count of items

    IF (count==1)

    Wakeup(Consumer); // if the buffer was previously empty, wake

    } // the consumer

    }

    Consumer()

    {

    int widget;

    WHILE(true) { // loop forever

    IF(count==0)

    Sleep(); // if the buffer is empty, sleep

    remove_item(widget); // take an item from the buffer

    count = count + 1; // decrement count of items

    IF(count==N-1)Wakeup(Producer); // if buffer was previously full, wake

    // the producer

    Consume_item(widget); // consume the item

    }

    }

    In the next three sections, the Purple Line, we will discuss solutions to this problem using each of the three methods introduced in the

    Solution Types section:

    http://cs.gmu.edu/cne/modules/ipc/green/solution.htmlhttp://cs.gmu.edu/cne/modules/ipc/green/solution.html
  • 7/30/2019 IPC Problems Solutions

    6/23

    Semaphores

    Monitors

    Message Passing

    IPC Home Subway Problems Readers/

    Page Map Writers

    http://cs.gmu.edu/cne/modules/ipc/map.htmlhttp://cs.gmu.edu/cne/modules/ipc/index.html
  • 7/30/2019 IPC Problems Solutions

    7/23

    Solution to the Producer-Consumer problem

    using Semaphores

    One problem with implementing a Sleep and Wakeup policy is the potential for losing Wakeups. Semaphores solve the problem of lost

    wakeups. In the Producer-Consumer problem, semaphores are used for two purposes:

    q mutual exclusion and

    q synchronization.

    In the following example there are three semaphores.Full, used for counting the number of slots that are full; empty, used for counting

    the number of slots that are empty; andmutex, used to enforce mutual exclusion.

    BufferSize = 3;

    semaphore mutex = 1; // Controls access to critical section

    semaphore empty = BufferSize; // counts number of empty buffer slots

    semaphore full = 0; // counts number of full buffer slots

    Producer()

    {

    http://cs.gmu.edu/cne/modules/ipc/red/sleep.htmlhttp://cs.gmu.edu/cne/modules/ipc/pink/swsem.htmlhttp://cs.gmu.edu/cne/modules/ipc/pink/swsem.htmlhttp://cs.gmu.edu/cne/modules/ipc/red/sleep.html
  • 7/30/2019 IPC Problems Solutions

    8/23

    int widget;

    while (TRUE) { // loop forever

    make_new(widget); // create a new widget to put in the buffer

    down(&empty); // decrement the empty semaphore

    down(&mutex); // enter critical section

    put_item(widget); // put widget in buffer

    up(&mutex); // leave critical section

    up(&full); // increment the full semaphore}

    }

    Consumer()

    {

    int widget;

    while (TRUE) { // loop forever

    down(&full); // decrement the full semaphore

    down(&mutex); // enter critical section

    remove_item(widget); // take a widget from the buffer

    up(&mutex); // leave critical section

    up(&empty); // increment the empty semaphore

    consume_item(widget); // consume the item

    }

    }

    IPC Home Subway Problems Producer/ Monitors

    Page Map Consumer

    http://cs.gmu.edu/cne/modules/ipc/map.htmlhttp://cs.gmu.edu/cne/modules/ipc/index.html
  • 7/30/2019 IPC Problems Solutions

    9/23

    Solution to the Producer-Consumer problem

    using Monitors

    Monitors make solving the producer-consumer a little easier. Mutual exclusion is achieved by placing the critical section of a program

    inside a monitor. In the code below, the critical sections of the producer and consumer are inside the monitor ProducerConsumer. Once

    inside the monitor, a process is blocked by the Wait and Signal primitives if it cannot continue.

    monitor ProducerConsumer

    condition full, empty;

    int count;

    procedure enter();

    { if (count == N) wait(full); // if buffer is full, block

    put_item(widget); // put item in buffer

    count = count + 1; // increment count of full slots

    if (count == 1) signal(empty); // if buffer was empty, wake consumer

    }

    procedure remove();

    {

    http://cs.gmu.edu/cne/modules/ipc/pink/swmon.htmlhttp://cs.gmu.edu/cne/modules/ipc/pink/swmon.html
  • 7/30/2019 IPC Problems Solutions

    10/23

    if (count == 0) wait(empty); // if buffer is empty, block

    remove_item(widget); // remove item from buffer

    count = count - 1; // decrement count of full slots

    if (count == N-1) signal(full); // if buffer was full, wake producer

    }

    count = 0;

    end monitor;

    Producer();

    {

    while (TRUE)

    {

    make_item(widget); // make a new item

    ProducerConsumer.enter; // call enter function in monitor

    }

    }

    Consumer();

    {

    while (TRUE)

    {

    ProducerConsumer.remove; // call remove function in monitor

    consume_item; // consume an item

    }

    }

    http://cs.gmu.edu/cne/modules/ipc/map.htmlhttp://cs.gmu.edu/cne/modules/ipc/index.html
  • 7/30/2019 IPC Problems Solutions

    11/23

    IPC Home Subway Problems Producer/ Message

    Page Map Consumer Passing

  • 7/30/2019 IPC Problems Solutions

    12/23

    Solution to the Producer-Consumer problem

    using Message Passing

    Message Passing allows us to solve the Producer-Consumer problem on distributed systems. In the problem below, an actual buffer does

    not exit. Instead, the producer and consumer pass messages to each other. These messages can contain the items which, in the previous

    examples, were placed in a buffer. They can also contain empty messages, meaning that a slot in the "buffer" is ready to receive an new

    item. In this example, a buffer size of four has been chosen. The Consumer begins the process by sending four empty messages to the

    producer. The producer creates a new item for each empty message it receives, then it goes to sleep. The producer will not create a new

    item unless it first receives an empty message. The consumer waits for messages from the producer which contain the items. Once it

    consumes an item, it sends an empty message to the producer. Again, there is no real buffer, only a buffer size which dictates the number

    of items allowed to be produced.

    BufferSize = 4;

    Producer()

    {

    int widget;

    message m; // message buffer

    http://cs.gmu.edu/cne/modules/ipc/pink/swmes.htmlhttp://cs.gmu.edu/cne/modules/ipc/pink/swmes.html
  • 7/30/2019 IPC Problems Solutions

    13/23

    while (TRUE) {

    make_item(widget); // create a new item

    receive(consumer, &m); // wait for an empty message to arrive

    build_message(&m, widget); // make a message to send to the consumer

    send(consumer, &m); // send widget to consumer

    }

    }

    Consumer(){

    int widget;

    message m;

    for(0 to N) send(producer, &m); // send N empty messages

    while (TRUE) {

    receive(producer, &m); // get message containing a widget

    extract_item(&m, widget); // take item out of message

    send(producer, &m); // reply with an empty message

    consume_item(widget); // consumer the item

    }

    }

    IPC Home Subway Problems Producer/

    Page Map Consumer

    http://cs.gmu.edu/cne/modules/ipc/map.htmlhttp://cs.gmu.edu/cne/modules/ipc/index.html
  • 7/30/2019 IPC Problems Solutions

    14/23

    The Readers and Writers Problem

    The Readers and Writers problem is useful for modeling processes which are competing for a limited shared resource. A practical

    example of a Readers and Writers problem is an airline reservation system consisting of a huge data base with many processes that read

    and write the data. Reading information from the data base will not cause a problem since no data is changed. The problem lies in writing

    information to the data base. If no constraints are put on access to the data base, data may change at any moment. By the time a readingprocess displays the result of a request for information to the user, the actual data in the data base may have changed. What if, for

    instance, a process reads the number of available seats on a flight, finds a value of one, and reports it to the customer. Before the customer

    has a chance to make their reservation, another process makes a reservation for another customer, changing the number of available seats

    to zero.

    In the following pages, we will look at solutions to this problem using semaphores, monitors, and message passing.

    In the next three sections, the Orange Line, we will discuss solutions to this problem using each of the three methods mentioned in the

    Solution Types section:

    Semaphores

    Monitors

    Message Passing

    http://cs.gmu.edu/cne/modules/ipc/green/solution.htmlhttp://cs.gmu.edu/cne/modules/ipc/green/solution.html
  • 7/30/2019 IPC Problems Solutions

    15/23

    IPC Home Subway Problems Producer/

    Page Map Consumer

    http://cs.gmu.edu/cne/modules/ipc/map.htmlhttp://cs.gmu.edu/cne/modules/ipc/index.html
  • 7/30/2019 IPC Problems Solutions

    16/23

    Solution to the Readers and Writers problem

    using Semaphores

    Semaphores can be used to restrict access to the database under certain conditions. In this example, semaphores are used to prevent any

    writing processes from changing information in the database while other processes are reading from the database.

    semaphore mutex = 1; // Controls access to the reader count

    semaphore db = 1; // Controls access to the database

    int reader_count; // The number of reading processes accessing the data

    Reader(){

    while (TRUE) { // loop forever

    down(&mutex); // gain access to reader_count

    reader_count = reader_count + 1; // increment the reader_count

    if (reader_count == 1)

    down(&db); // if this is the first process to read the

    database,

    // a down on db is executed to prevent access

    http://cs.gmu.edu/cne/modules/ipc/pink/swsem.htmlhttp://cs.gmu.edu/cne/modules/ipc/pink/swsem.html
  • 7/30/2019 IPC Problems Solutions

    17/23

    to the

    // database by a writing process

    up(&mutex); // allow other processes to access reader_count

    read_db(); // read the database

    down(&mutex); // gain access to reader_count

    reader_count = reader_count - 1; // decrement reader_count

    if (reader_count == 0)

    up(&db); // if there are no more processes reading from

    the// database, allow writing process to access

    the data

    up(&mutex); // allow other processes to access

    reader_countuse_data();

    // use the data read from the database (non-

    critical)

    }

    Writer()

    {

    while (TRUE) { // loop forever

    create_data(); // create data to enter into database (non-

    critical)

    down(&db); // gain access to the database

    write_db(); // write information to the database

    up(&db); // release exclusive access to the database

    }

    IPC Home Subway Problems Readers/ Monitors

    http://cs.gmu.edu/cne/modules/ipc/map.htmlhttp://cs.gmu.edu/cne/modules/ipc/index.html
  • 7/30/2019 IPC Problems Solutions

    18/23

    Page Map Writers

  • 7/30/2019 IPC Problems Solutions

    19/23

    Solution to the Readers and Writers problem

    using Monitors

    Monitors can be used to restrict access to the database. In this example, the read and write functions used by processes which access the

    database are in a monitor called ReadersWriters. If a process wants to write to the database, it must call the writeDatabase function. If a

    process wants to read from the database, it must call the readDatabase function.

    Remember that monitors use the primitives Wait and Signal to put processes to sleep and to wake them up again. In writeDatabase, the

    calling process will be put to sleep if the number of reading processes, stored in the variable count, is not zero. Upon exiting the

    readDatabase function, reading processes check to see if they should wake up a sleeping writing process.

    monitor ReadersWriters

    condition OKtoWrite, OKtoRead;int ReaderCount = 0;

    Boolean busy = false;

    procedure StartRead()

    {

    if (busy) // if database is not free, block

    OKtoRead.wait;

    http://cs.gmu.edu/cne/modules/ipc/pink/swmon.htmlhttp://cs.gmu.edu/cne/modules/ipc/pink/swmon.html
  • 7/30/2019 IPC Problems Solutions

    20/23

    ReaderCount++; // increment reader ReaderCount

    OKtoRead.signal();

    }

    procedure EndRead()

    {

    ReaderCount-- ; // decrement reader ReaderCount

    if ( ReaderCount == 0 )OKtoWrite.signal();

    }

    procedure StartWrite()

    {

    if ( busy || ReaderCount != 0 )

    OKtoWrite.wait();

    busy = true;

    }

    procedure EndWrite()

    {

    busy = false;

    If (OKtoRead.Queue)

    OKtoRead.signal();

    else

    OKtoWrite.signal();

    }

    Reader()

    {

    while (TRUE) // loop forever

    {

    ReadersWriters.StartRead();

    ) // i i i

  • 7/30/2019 IPC Problems Solutions

    21/23

    readDatabase(); // call readDatabase function in monitor

    ReadersWriters.EndRead();

    }

    }

    Writer()

    {

    while (TRUE) // loop forever

    {make_data(&info); // create data to write

    ReaderWriters.StartWrite();

    writeDatabase(); // call writeDatabase function in monitor

    ReadersWriters.EndWrite();

    }

    }

    IPC Home Subway Problems Readers/ Message

    Page Map Writers Passing

    http://cs.gmu.edu/cne/modules/ipc/map.htmlhttp://cs.gmu.edu/cne/modules/ipc/index.html
  • 7/30/2019 IPC Problems Solutions

    22/23

    Solution to the Readers and Writers problem

    using Message Passing

    In this solution to the Readers/Writers problem, Message Passing is used to prevent a writing process from writing into the database while

    another process is either reading or writing. The following solution assumes that readers and writers send their requests to a databaseserver process that ensures that the above property holds.

    Reader()

    {

    while (TRUE) {

    send (server, ReadRequest);

    receive (server, value);

    display (value);

    }

    }

    Writer()

    {

    hil (TRUE) {

    http://cs.gmu.edu/cne/modules/ipc/pink/swmes.htmlhttp://cs.gmu.edu/cne/modules/ipc/pink/swmes.html
  • 7/30/2019 IPC Problems Solutions

    23/23

    while (TRUE) {

    create_data (&value);

    send (server, WriteRequest, value);

    receive (server, WriteOk);

    }

    }

    IPC Home Subway Problems Readers/

    Page Map Writers

    http://cs.gmu.edu/cne/modules/ipc/map.htmlhttp://cs.gmu.edu/cne/modules/ipc/index.html