6. Experiment
Aim: Write a program to implement classical inter process communication problem (producer consumer).
Theory:
The Producer-Consumer problem (also known as Bounded Buffer) is a classical synchronization problem.
Here :
We have two processes:
Producer: produces data and put it into a buffer.
Consumer: consumes the data from the buffer.
The buffer has a fixed size.
If the buffer is full, the Producer must wait until the Consumer removes some items.
If the buffer is empty, the Consumer must wait until the Producer produces something.
Implementation (Using Semaphores):
We typically solve this with:
Semaphore empty: initialized to buffer size.
Semaphore full: initialized to 0.
Mutex: to allow only one process at a time to modify the buffer.
Flowchart
Algorithm
Step 1: Start
Step 2: Define a buffer of fixed size.
Step 3: Producer:
- If buffer is not full, produce an item and put it in buffer.
- If buffer is full, wait.
Step 4: Consumer:
- If buffer is not empty, consume an item from buffer.
- If buffer is empty, wait.
Step 5: Repeat 3 and 4.
Step 6: End
Source Code :
// C++ program to implement classical inter process communication problem (producer consumer).
#include <iostream>
using namespace std;
#define BUFFER_SIZE 5
int buffer[BUFFER_SIZE];
int in = 0, out = 0, count = 0;
void produce(int item) {
if (count == BUFFER_SIZE) {
cout << "Buffer is full. Producer waiting…" << endl;
} else {
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
count++;
cout << "Produced: " << item << endl;
}
}
void consume() {
if (count == 0) {
cout << "Buffer is empty. Consumer waiting…" << endl;
} else {
int item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;
cout << "Consumed: " << item << endl;
}
}
int main()
{
int n;
cout << "Enter number of operations: ";
cin >> n;
for(int i = 0; i < n; i++) {
int choice, item;
cout << "Enter 1 to produce, 2 to consume: ";
cin >> choice;
if (choice == 1) {
cout << "Enter item to produce: ";
cin >> item;
produce(item);
}
else if (choice == 2) {
consume();
}
}
return 0;
}
// Program should be with output in Lab file
Objective (multiple-choice) questions based on classical inter process communication problem (producer consumer).
1.What is Producer-Consumer problem?
a) Synchronization problem
b) File sharing problem
c) Communication problem
d) File deletion problem
Answer: a) Synchronization problem
2.What prevents Producer from adding when buffer is full?
a) Mutual exclusion
b) Semaphore
c) CPU
d) File pointer
Answer: b) Semaphore
3.What is the main aim of Producer-Consumer problem?
a) To maximize CPU usage
b) To avoid race conditions
c) To minimize disk I/O
d) To execute faster
Answer: b) To avoid race conditions
4.Which kind of buffer is typically used?
a) Stack
b) File
c) Fixed-size circular buffer
d) Hash-table
Answer: c) Fixed-size circular buffer
5.What is “wait” in context of IPC?
a) To produce faster
b) To consume faster
c) To block process
d) To release CPU
Answer: c) To block process
6.Why do we need synchronization primitives?
a) To avoid busy waiting
b) To control concurrent access
c) To send signals
d) To execute faster
Answer: b) To control concurrent access
7.If buffer is empty, consumer should:
a) Produce more
b) Wait
c) Raise interrupt
d) Access anyway
Answer: b) Wait
8.If buffer is full, producer should:
a) Keep adding
b) Wait
c) Raise interrupt
d) Remove an item
Answer: b) Wait
9.Semaphore initialized to 0 typically denotes:
a) Buffer is full
b) Buffer is empty
c) Item available
d) Unsafe state
Answer: b) Buffer is empty
10.To avoid busy waiting, we use:
a) Spinlock
b) Semaphores
c) Thread
d) Fork
Answer: b) Semaphores
11.What operations do Semaphores provide?
a) Push, Pop
b) Wait, Signal
c) Send, Receive
d) Lock, Unlock
Answer: b) Wait, Signal
12.The Producer signals the:
a) full
b) empty
c) consumer
d) buffer
Answer: c) consumer
13.The Consumer signals the:
a) full
b) empty
c) producer
d) buffer
Answer: c) producer
14.Mutual Exclusion guarantees:
a) At most 1 process in CS
b) All can enter CS
c) There is no CS
d) Few can enter CS
Answer: a) At most 1 process in CS
15.he main problem without synchronization is:
a) Race conditions
b) Deadlock
c) Starvation
d) Inefficiency
Answer: a) Race conditions