producer consumer problem

Discussion in 'Operating System' started by mahor1989, May 15, 2011.

  1. mahor1989

    mahor1989 New Member

    Joined:
    May 15, 2011
    Messages:
    1
    Likes Received:
    1
    Trophy Points:
    0
    can anyone explain to me producer consumer problem given in galvin with code in operating system?
     
    komalmehra likes this.
  2. _eb75_

    _eb75_ New Member

    Joined:
    Jun 18, 2012
    Messages:
    16
    Likes Received:
    6
    Trophy Points:
    0
    Location:
    www.isosika.net
    Let's have one or more (N) factories, one storage for all factories with capacity to store L units of products of factories (for ex. cars). Then we have M customers coming to buy a car from the factory storage (random times).

    If storage is empty customers go to a waiting for a car queue. If the storage is full, a factory producing a new car goes waiting for a storage space queue. If we have at least one car in the storage the first customer can go in and buy a car. If we have at least one free space for a car the first waiting factory can place the car in the storage.

    OS provides system calls called semaphore operations (name comes from rail roads). One semaphore operation, we can call it P, stops a running process, if a condition is not met (for ex. we have not an empty space for a new produced car). An other semaphore operation signals the system (for ex. I did take a car, now we have one more free space, we call it V.


    How to build up N producer tasks and M consumer tasks?

    In every of producer tasks we have a construction as:

    main() {

    ...........

    car=produce_car();
    P(empty_space);//OS stops us if no space, we continue when space exist

    put_in_storage(car);

    V(car_available);//We inform OS to start a possibly waiting consumer
    .......
    }




    In every consumer tasks we have a code as:

    main(){
    .............
    P(car_availabe);//OS stops us if no cars

    car=take_from_storage():

    V(empty_space);
    ...........
    use_the_car();
    .............
    }


    We have some kind of key (name) when a task asks from OS a semaphore common to many tasks.
    The semaphores must be initialized as needed for ex. car_available=0, empty_space=L. We tell by an initialization system call to the OS how we want to behave. After that OS takes care that everything goes right (stopping and starting tasks as needed).
     
    komalmehra and shabbir like this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice