Producer-Consumer

24 Jun 2022 . os .


Problem

  1. Producer can produce data only when the buffer is not full.
  2. Consumer can consume data only when the buffer is not empty.
  3. Producer and Consumer should not access the buffer at same time.

Solution - Semaphore

  • Semaphore S

    Achieve mutual exclusion between processes.

  • Semaphore E

    Define the empty space in the buffer.

  • Semaphore F

    Define the space that is filled by the producer.

  • wait()

    Decrease the semaphore variable by 1.

  • signal()

    Increase the semaphore variable by 1.

Producer

void producer() {
    while(T) {
        produce()
        wait(E)
        wait(S)
        append()
        signal(S)
        signal(F)
    }
}

If the buffer is full i.e. ā€œEā€ is ā€œ0ā€, the program will stop and no production will be done.

Consumer

void consumer() {
    while(T) {
        wait(F)
        wait(S)
        take()
        signal(S)
        signal(E)
        use()
    }
}

If the buffer is empty i.e. ā€œFā€ is ā€œ0ā€, no consumption will be made.