Question: -Linux C Program Create a monitor using semaphores and shared memory and use it to solve the producer and consumer problem. x producers xx consumers

-Linux C Program

Create a monitor using semaphores and shared memory and use it to solve the producer and consumer problem.

x producers

xx consumers

Your main process, called monitor, will fork all these producers and consumers. Use for a template

/////////////////////////

// Dining-Philosophers Solution Using Monitors

monitor DP

{

status state[5];

condition self[5];

// Pickup chopsticks

Pickup(int i)

{

// indicate that Im hungry

state[i] = hungry;

// set state to eating in test()

// only if my left and right neighbors

// are not eating

test(i);

// if unable to eat, wait to be signaled

if (state[i] != eating)

self[i].wait;

}

// Put down chopsticks

Putdown(int i)

{

// indicate that Im thinking

state[i] = thinking;

// if right neighbor R=(i+1)%5 is hungry and

// both of Rs neighbors are not eating,

// set Rs state to eating and wake it up by

// signaling Rs CV

test((i + 1) % 5);

test((i + 4) % 5);

}

test(int i)

{

if (state[(i + 1) % 5] != eating

&& state[(i + 4) % 5] != eating

&& state[i] == hungry) {

// indicate that Im eating

state[i] = eating;

// signal() has no effect during Pickup(),

// but is important to wake up waiting

// hungry philosophers during Putdown()

self[i].signal();

}

}

init()

{

// Execution of Pickup(), Putdown() and test()

// are all mutually exclusive,

// i.e. only one at a time can be executing

for

i = 0 to 4

// Verify that this monitor-based solution is

// deadlock free and mutually exclusive in that

// no 2 neighbors can eat simultaneously

state[i] = thinking;

}

} // end of monitor

////////////////////////

Producers will produce an item and sleep for a random amount of time (between 1 and 5 seconds). When the item is produced, it will be logged with the time and producer number in a log file. The consumers will sleep for a random amount of time (between 1 and 10 seconds) and pick up the item and consume it. Consumers will also log their activity into the log file.

Use the same log file for both producers and consumers. Make sure that you have more consumers than producers. If all the children have finished, the parent process should deallocate shared memory and terminate.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!