ICS53-Assignment 6 Producer-Consumer Problem Solved

30.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: zip solution files instantly, after Payment

Securely Powered by: Secure Checkout

Description

Rate this product

The producer-consumer problem is a well-known problem in concurrent programming.

In this assignment, you will implement an extension of this problem using the POSIX Threads library and you would need to synchronize executions of the threads to have the program working properly.

 

Project Specifications

 

We have two kinds of threads: producers and consumers. In this assignment there can be many producers and consumers. Each producer and each consumer must execute in a different thread. A producers’ job is to produce items and put them into a bounded buffer. A consumer’s job is to remove items from the buffer. The buffer can hold 8 items. There are the following constraints on how producers and consumers can access the buffer.

  1. Only one thread can access the buffer at a time.
  2. A producer cannot write into the buffer if it is full.
  3. A consumer cannot read from the buffer if it is empty.

 

        –    Producer Behavior

 

  • Each producer will have an associated producer number, ​n​, which is an integer from ​0​ to ​p-1​, where ​p​ is the number of producers.
  • Each producer will produce a series of items and place each item in the buffer.
  • Producers will not produce items in an infinite loop. Instead, each producer will produce only ​i ​ After a producer thread has produced i items and placed them in the buffer, the producer thread will terminate.
  • Each item produced is an integer between ​(n * i)​ and ​((n+1) * i – 1)​. This ensures that the items produced by all producers are unique; no two producers ever produce the same item.
  • When a producer produces an item, it prints​ “producer_​n​ ​produced item item_value​” ​. The term ​n​ in the printed output should be replaced with the producer number, and the term ​item_value​ should be replaced with the item value.

 

        –    Consumer Behavior

 

  • Each consumer will have an associated consumer number, ​m​, which is an integer from ​0​ to ​c-1​, where ​c​ is the number of consumers.
  • Each consumer will remove items from the buffer.
  • Consumers will not produce items in an infinite loop. Instead, each consumer will consume only ​(p*i)/​c ​ After a consumer thread has consumed (p*i)/​c​ items, the consumer thread will terminate.
  • When a consumer consumes an item, it prints​ “consumer_​m​ ​consumed item item_value​” ​. The term ​m​ in the printed output should be replaced with the consumer number, and the term ​item_value​ should be replaced with the item value.

 

        –    Delays

 

In order to demonstrate the behavior of the system under different conditions, you will add delays to the producer and consumer threads using the ​usleep()​ function. There will be two options for the insertion of delays. In the first option, a 0.5 second delay will be added to each producer thread immediately after it adds an item to the buffer. This will greatly slow down the rate of production. In the second option, a 0.5 second delay will be added to each consumer thread immediately after it removes an item from the buffer. The choice between these two delay options will be made by a command-line argument passed to your program.

 

        –    Command-Line Arguments

 

Your program will accept 4 command-line arguments which specify parameters of the producer-consumer system.

  • p​ – The number of producers. This must be between 1 and 16.
  • c​ – The number of consumers. This must be between 1 and 16. The number of consumers should always be smaller than the number of total items being produced (i.e. ​c < p*i​).
  • i​ – The number of items produced by each producer.
  • d – The selection of the delay option. If d = 0 then the delay will be added to the​ producer threads,and if d = 1 then the delay will be added to the consumer threads.

 

  • assignment6-0o9gyy.zip