Starting from:
$30

$24

WEEKS 6 & 7: Producer Consumer Problem

OBJECTIVE:

PROGRAM 1: Write a C Program to simulate race condition in Producer-Consumer Problem

PROGRAM 2: Write a C program to implement Producer Consumer problem using Mutex (prevent race condition)



PROGRAM 1: Write a C Program to simulate race condition in Producer-Consumer Problem

Implement a main program that creates two threads: producer and consumer threads which execute producer and consumer functions respectively. The producer should produce an item and update the buffer. The consumer should consume an item and update the buffer. You can use bounded buffer and both the producer and consumer threads can be infinite loops. Show how race condition occurs between producer and consumer without mutual exclusion.

PROGRAM 2: Write a C program to implement Producer Consumer problem using Mutex. (Do NOT use POSIX Semaphores in this program)

Extend Program 1 by using mutex to enclose the critical sections in both producer and consumer so that only one of them can update the buffer at a time and prevent race condition as shown in sample outputs below. Also, consumer should wait if buffer is empty and producer should signal when the buffer has at least one item. You can use unbounded buffer so that producer does not have to wait for buffer availability. Both the producer and consumer threads can be infinite loops and each can randomly sleep to let the other proceed. The output should be the number of items in buffer along with the consumer/producer that is updating the buffer.

Sample Outputs:


Your output can vary depending upon your implementation and the manner in which the threads are scheduled on your system.

With race condition

Job 1 started

Job 2 started

Job 3 started

Job 2 finished

Job 4 started

Job 3 finished

Job 1 finished

Job 4 finished

....

Without race condition

Job 1 started

Job 1 finished

Job 2 started

Job 2 finished

Job 3 started

Job 3 finished

Job 4 started

Job 4 finished

...


  
With race condition    Without race condition

Producer: Produced item 1    Producer: Produced item 1

Producer: Produced item 2    Consumer: Consumed item 1

Consumer: Consumed item 1    Producer: Produced item 2

Producer: Produced item 3    Consumer: Consumed item 2

Producer: Produced item 4    Producer: Produced item 3

Producer: Produced item 5    Consumer: Consumed item 3

Producer: Produced item 6    Producer: Produced item 4

Consumer: Consumed item 4    Consumer: Consumed item 4

Consumer: Consumed item 3    Producer: Produced item 5

Producer: Produced item 7    Consumer: Consumed item 5

....    ...
































 

More products