0% found this document useful (0 votes)
3 views9 pages

A67 Os Exp6

The document outlines an experiment for implementing the Producer-Consumer problem using semaphores in C programming. It explains the roles of the producer and consumer, the conditions for buffer access, and the use of three semaphores (Full, Empty, and Mutex) to manage synchronization. The conclusion emphasizes that students will learn to implement and analyze synchronization and deadlock concepts through this experiment.

Uploaded by

aryanshetty840
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

A67 Os Exp6

The document outlines an experiment for implementing the Producer-Consumer problem using semaphores in C programming. It explains the roles of the producer and consumer, the conditions for buffer access, and the use of three semaphores (Full, Empty, and Mutex) to manage synchronization. The conclusion emphasizes that students will learn to implement and analyze synchronization and deadlock concepts through this experiment.

Uploaded by

aryanshetty840
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Operating System Lab 2025

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.

What is the problem?

TEC[Artificial Intelligence and Data Science] Page 1


Operating System Lab 2025
• The producer should produce data only when the buffer is not full. In case it is found
that the buffer is full, the producer is not allowed to store any data into the memory
buffer.
• Data can only be consumed by the consumer if and only if the memory buffer is not
empty. In case it is found that the buffer is empty, the consumer is not allowed to use
any
data from the memory buffer.
• Accessing memory buffer should not be allowed to producer and consumer at the same
time.

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.

Signal() and wait() operations are performed to arrive at a solution to the


ProducerConsumer problem.
Signal(): The signal function increases the semaphore value by 1.

Wait(): The wait operation decreases the semaphore value by 1.

The code for Producer Process is as follows:

TEC[Artificial Intelligence and Data Science] Page 2


Operating System Lab 2025
void Producer(){ while(true){
// producer produces an item/data
wait(Empty); wait(mutex); add();
signal(mutex); signal(Full);
}
}

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.

The code for the Consumer Process is as follows :


void Consumer() { while(true){
// consumer consumes an item
wait(Full); wait(mutex);
consume(); signal(mutex);
signal(Empty);
}
}

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

TEC[Artificial Intelligence and Data Science] Page 3


Operating System Lab 2025
zero i.e the buffer is empty then the consumer process cannot consume any item from the
buffer and it goes in the busy-waiting state.

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:

TEC[Artificial Intelligence and Data Science] Page 4


Operating System Lab 2025

TEC[Artificial Intelligence and Data Science] Page 5


Operating System Lab 2025

Output:

TEC[Artificial Intelligence and Data Science] Page 6


Operating System Lab 2025

TEC[Artificial Intelligence and Data Science] Page 7


Operating System Lab 2025

TEC[Artificial Intelligence and Data Science] Page 8


Operating System Lab 2025
Conclusion:

Successful completion of this experiment students learnt to, Implement and analyze
concepts of synchronization and deadlocks.

TEC[Artificial Intelligence and Data Science] Page 9

You might also like