Operating Systems; Implementation of the Producer-Consumer Problem Non-Threading implementation

Implement the Producer Consumer Problem as discussed in class.

Assume that there is only one Producer and there is only one Consumer.

The problem you will be solving is the bounded-buffer producer-consumer problem. You are required to implement this assignment in Java or in C/C++. This buffer can hold a fixed number of items. This buffer needs to be a first-in first-out (FIFO) buffer. You should implement this as a Circular Buffer that satisfies the FIFO. There should be exactly one instance of the buffer. The producer and the consumers must reference the same buffer.
The producer is responsible for producing data items to be added to the buffer. If the buffer is full, the producer must wait for a consumer to consume at least one item before it can add a new item to the buffer. The producer is required to produce a given number of items. The item that the producer adds to the buffer is a random uppercase letter ‘A’ to ‘Z’, inclusive. When a producer successfully inserts an item in the buffer it should print the location of insertion and time when insertion occurs with nanosecond resolution, using this format (spaces matter):Producer inserted ‘S’ at index 0 at 323187855 ns
The consumer is responsible for consuming elements, generated by the producer, from the buffer. If the buffer is empty, the consumer must wait for the producer to add an item to the buffer. When a consumer successfully removes an item from the buffer it should print the location of removal and time when removal occurs with nanosecond resolution, using this format:Consumer consumed ‘I’ at index 7 at 324491579 ns
Implement the Producer and the Consumer as two separate functions (no multithreading).
The main driver (function) must do all necessary initialization, then it must iterate for a period of 10seconds (not 10 iterations).
In each iteration, the main driver calls randomly either the Producer function or the Consumerfunction. Once either of these two functions is called, it should return within one second time period from the times that it has been called as specified below:If the corresponding function (Producer/Consumer) is successful in Producing/Consuming an item (character) before the one second is expired, then the function will return normally.Otherwise (in case of the Buffer is Full/Empty and the one second period has expired), then the corresponding function should return to the main driver without Producing/Consuming and item.
Sample Output:
Start time in nanosecond: 443727532 ns
Producer inserted ‘G’ at index 0 at 443729161 ns Producer inserted ‘Q’ at index 1 at 443730194 ns Producer inserted ‘R’ at index 2 at 443761114 ns Consumer consumed ‘G’ at index 0 at 443763544 ns Consumer consumed ‘Q’ at index 1 at 443769914 ns Consumer consumed ‘R’ at index 2 at 443774734 ns Producer inserted ‘E’ at index 0 at 443776594 ns Consumer consumed ‘E’ at index 0 at 443783544 ns Producer inserted ‘W’ at index 1 at 443785334 ns Producer inserted ‘U’ at index 2 at 443787024 ns Consumer consumed ‘W’ at index 1 at 443789048 ns Consumer consumed ‘U’ at index 2 at 443790011 ns…

  1. What to submit:

a) The source code program
b) Instruction on how to compile and run this program
c) Output of the program
Hint: The following might help you understand some time functions, try them.

include #include

// main function to print the current time in nanosecondint main()

page2image10584
{

}

struct timespec mytime; clock_gettime(CLOCK_REALTIME, &mytime);
double xyz = mytime.tv_nsec;
printf(“Time elpased since epoch is %.0f secondsn”, xyz); return 0;

since epoch (or Unix time) is the

page2image13624
// number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT)

page3image376

include

include // for clock_t, clock() #include // for sleep()

define BILLION 1000000000.0

// main function to find the execution time of a C program int main()
{

struct timespec start, end; clock_gettime(CLOCK_REALTIME, &start);

// do some stuff here
sleep(3); // sleep for three seconds

clock_gettime(CLOCK_REALTIME, &end);

// time_spent = end – start
double time_spent = (end.tv_sec – start.tv_sec) + (end.tv_nsec – start.tv_nsec) / BILLION;

printf(“Time elpased is %f seconds”, time_spent);

return 0; }

This question has been answered.

Get Answer