Question: Write code to accomplish the following. Let the two processes alternate on the CPU. Let the producer process attempt to execute 4 iterations of the


Write code to accomplish the following. Let the two processes alternate on the CPU. Let the producer process attempt to execute 4 iterations of the outer while loop, while the consumer attempts three iterations. Print the value of the shared variables (in, out, counter) after each iteration of the processes. Show the items in the buffer and their location. Clearly indicate which process is the producer and which is the consumer.

 

PLease write code in java programming language 

Shared data #define BUF_SIZE 10 typedef struct { //int a; //char x;



Shared data #define BUF_SIZE 10 typedef struct { //int a; //char x; } item; item buffer[BUF_SIZE]; int in = 0; int out = 0; counter = 0; Consumer Remove() while (true) { while (counter 0) //add code to count the number of times the looping takes place and prints that value. ; // do nothing (just keep looping) // remove an item from the buffer nextConsumed buffer[out]; //write a null value to location out out = (out + 1)% BUF_SIZE; counter--; //add code here to show on the same line, the two variables contained in the variable item. Eg (21, C) Each item must be printed on a separate line. } Producer insert() while (true) { /* Produce an item */ while (counter == BUF_SIZE) //add code to count the number of times the looping takes place and prints that value. ; // do nothing (keep looping) buffer[in] = nextProduced(); in = (in+1)% BUF_SIZE; counter++ } item nextProduce(){ Assigns to each item produced a different value to the variables a and x and return the item. }

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 Programming Questions!