0% found this document useful (0 votes)
2 views6 pages

Producer-Consumer Problem Solutions

The document presents two solutions to the producer-consumer problem: one using shared memory with semaphores and the other using message passing with a vector. In the shared memory solution, a producer thread produces items and a consumer thread consumes them, ensuring synchronization through semaphore controls. The message passing solution involves a producer thread generating messages and a consumer thread retrieving them from a queue, with synchronization managed through wait and notify mechanisms.

Uploaded by

userprofession
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)
2 views6 pages

Producer-Consumer Problem Solutions

The document presents two solutions to the producer-consumer problem: one using shared memory with semaphores and the other using message passing with a vector. In the shared memory solution, a producer thread produces items and a consumer thread consumes them, ensuring synchronization through semaphore controls. The message passing solution involves a producer thread generating messages and a consumer thread retrieving them from a queue, with synchronization managed through wait and notify mechanisms.

Uploaded by

userprofession
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

Practical1A:

Process Communication:
a) Give solution to the producer–consumer problem using shared
memory.
package q;
import [Link];
class Q {
// an item
int item;
// semCon initialized with 0 permits
// to ensure put() executes first
static Semaphore semCon = new Semaphore(0);

static Semaphore semProd = new Semaphore(1);

// to get an item from buffer


void get()
{
try {
// Before consumer can consume an item,
// it must acquire a permit from semCon
[Link]();
}
catch (InterruptedException e) {
[Link]("InterruptedException caught");
}

// consumer consuming an item


[Link]("Consumer consumed item : " + item);

// After consumer consumes the item,


// it releases semProd to notify producer
[Link]();
}

// to put an item in buffer


void put(int item)
{
try {
// Before producer can produce an item,
// it must acquire a permit from semProd
[Link]();
}
catch (InterruptedException e) {
[Link]("InterruptedException caught");
}
// producer producing an item
[Link] = item;
[Link]("Producer produced item : " + item);
// After producer produces the item,
// it releases semCon to notify consumer
[Link]();
}
}
// Producer class
class Producer implements Runnable {
Q q;
Producer(Q q)
{
this.q = q;
new Thread(this, "Producer").start();
}

public void run()


{
for (int i = 0; i < 5; i++)
// producer put items
[Link](i);
}
}

// Consumer class
class Consumer implements Runnable {
Q q;
Consumer(Q q)
{
this.q = q;
new Thread(this, "Consumer").start();
}

public void run()


{
for (int i = 0; i < 5; i++)
// consumer get items
[Link]();
}
}

// Driver class
class PC {
public static void main(String args[])
{
// creating buffer queue
Q q = new Q();

// starting consumer thread


new Consumer(q);

// starting producer thread


new Producer(q);
}
}
Output:
Producer produced item : 0
Consumer consumed item : 0
Producer produced item : 1
Consumer consumed item : 1
Producer produced item : 2
Consumer consumed item : 2
Producer produced item : 3
Consumer consumed item : 3
Producer produced item : 4
Consumer consumed item : 4

Practical1B:
Process Communication:
b) Give solution to the producer–consumer problem using message
passing.
package producer;
import [Link];
class Producer extends Thread {
// initialization of queue size
static final int MAX = 7;
private Vector messages = new Vector();
@Override
public void run()
{
try {
while (true) {
// producing a message to send to the consumer
putMessage();
// producer goes to sleep when the queue is full
sleep(1000);
}
}
catch (InterruptedException e) {
}
}
private synchronized void putMessage()
throws InterruptedException
{
// checks whether the queue is full or not
while ([Link]() == MAX)
// waits for the queue to get empty
wait();
// then again adds element or messages
[Link](new [Link]().toString());
notify();
}
public synchronized String getMessage()
throws InterruptedException
{
notify();
while ([Link]() == 0)
wait();
String message = (String)[Link]();
// extracts the message from the queue
[Link](message);
return message;
}
}
class Consumer extends Thread {
Producer producer;
Consumer(Producer p)
{
producer = p;
}
@Override
public void run()
{
try {
while (true) {
String message = [Link]();
// sends a reply to producer got a message
[Link]("Got message: " + message);
sleep(2000);
}
}
catch (InterruptedException e) {
}
}
public static void main(String args[])
{
Producer producer = new Producer();
[Link]();
new Consumer(producer).start();
}
}
Output:
Got message: Mon Jun 27 22:00:53 IST 2022
Got message: Mon Jun 27 22:00:54 IST 2022
Got message: Mon Jun 27 22:00:55 IST 2022
Got message: Mon Jun 27 22:00:56 IST 2022
Got message: Mon Jun 27 22:00:57 IST 2022
Got message: Mon Jun 27 22:00:58 IST 2022

You might also like