A67 Os Exp6
A67 Os Exp6
Experiment No.06
Aim:
Write a C program to implement solution of Producer consumer problem through
Semaphore.
Outcome:
After successful completion of this experiment students will be able to, Implement
and analyze concepts of synchronization and deadlocks.
Theory:
Producer Consumer Problem:
The Producer-Consumer problem is a classical multi-process synchronization problem that is
we are trying to achieve synchronization between more than one processes.
There is one Producer in the producer-consumer problem, Producer is producing some
items, whereas there is one Consumer that is consuming the items produced by the
Producer. The same memory buffer is shared by both producers and consumers which is of
fixed-size.
The task of the Producer is to produce the item, put it into the memory buffer, and again
start producing items. Whereas the task of the Consumer is to consume the item from the
memory buffer.
Solution:
To solve the Producer-Consumer problem three semaphores variable are used:
Semaphore:
Semaphores are variables used to indicate the number of resources available in the system
at a particular time.
Semaphore variables are used to achieve `Process Synchronization.
Full
The full variable is used to track the space filled in the buffer by the Producer process. It is
initialized to 0 initially as initially no space is filled by the Producer process.
Empty
The Empty variable is used to track the empty space in the buffer. The Empty variable is
initially initialized to the BUFFER-SIZE as initially, the whole buffer is empty.
Mutex
Mutex is used to achieve mutual exclusion. mutex ensures that at any particular time only
the producer or the consumer is accessing the buffer. Mutex is binary semaphore variable
that has a value of 0 or 1.
wait(Empty): Before producing items, the producer process checks for the empty space in
the buffer. If the buffer is full producer process waits for the consumer process to consume
items from the buffer. so, the producer process executes wait(Empty) before producing any
item.
wait(mutex): Only one process can access the buffer at a time. So, once the producer
process enters into the critical section of the code it decreases the value of mutex by
executing wait(mutex) so that no other process can access the buffer at the same time.
add(): This method adds the item to the buffer produced by the Producer process. once the
Producer process reaches add function in the code, it is guaranteed that no other process
will be able to access the shared buffer concurrently which helps in data consistency.
signal(mutex): Now, once the Producer process added the item into the buffer it increases
the mutex value by 1 so that other processes which were in a busy-waiting state can access
the critical section.
signal(Full): when the producer process adds an item into the buffer spaces is filled by one
item so it increases the Full semaphore so that it indicates the filled spaces in the buffer
correctly.
wait(Full): Before the consumer process starts consuming any item from the buffer it
checks if the buffer is empty or has some item in it. So, the consumer process creates one
more empty space in the buffer and this is indicated by the full variable. The value of the
full variable decreases by one when the wait(Full) is executed. If the Full variable is already
wait(mutex): It does the same as explained in the producer process. It decreases the mutex
by 1 and restricts another process to enter the critical section until the consumer process
increases the value of mutex by 1.
consume(): This function consumes an item from the buffer. when code reaches the
consuming () function it will not allow any other process to access the critical section which
maintains the data consistency.
signal(mutex): After consuming the item it increases the mutex value by 1 so that other
processes which are in a busy-waiting state can access the critical section now.
signal(Empty): when a consumer process consumes an item it increases the value of the
Empty variable indicating that the empty space in the buffer is increased by 1.
Code:
Output:
Successful completion of this experiment students learnt to, Implement and analyze
concepts of synchronization and deadlocks.