Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Producer Consumer Problem In Section 7.1.1, we presented a semaphore-based solution to the producer consumer problem using a bounded buffer. In this project, you

The Producer Consumer Problem

In Section 7.1.1, we presented a semaphore-based solution to the producer consumer problem using a bounded buffer. In this project, you will design a programming solution to the bounded-buffer problem using the producer and consumer processes shown in Figures 5.9 and 5.10. The solution presented in Section 7.1.1 uses three semaphores: empty and full, which count the number of empty and full slots in the buffer, and mutex, which is a binary (or mutualexclusion) semaphore that protects the actual insertion or removal of items in the buffer. For this project, you will use standard counting semaphores for empty and full and a mutex lock, rather than a binary semaphore, to represent mutex. The producer and consumerrunning as separate threads will move items to and from a buffer that is synchronized with the empty, full, and mutex structures. You can solve this problem using either Pthreads or Java API.

The Buffer

Internally, the buffer will consist of a fixed-size array of type buffer item (which will be defined using a typedef). The array of buffer item objects will be manipulated as a circular queue. The definition of buffer item, along with the size of the buffer, can be stored in a header file such as the following:

/* buffer.h */

typedef int buffer item;

#define BUFFER SIZE 5

The buffer will be manipulated with two functions, insert item() and remove item(), which are called by the producer and consumer threads, respectively. A skeleton outlining these functions appears in Figure 2. The insert item() and remove item() functions will synchronize the producer and consumer using the algorithms outlined in Figure 7.1 and Figure 7.2. The buffer will also require an initialization function that initializes the mutual- exclusion object mutex along with the empty and full semaphores. The main() function will initialize the buffer and create the separate producer and consumer threads. Once it has created the producer and consumer threads, the main() function will sleep for a period of time and, upon awakening, will terminate the application. The main() function will be passed three parameters on the command line:

1. How long to sleep before terminating

2. The number of producer threads

3. The number of consumer threads

A skeleton for this function appears in Figure 3.

#include "buffer.h"

/* the buffer */

buffer item buffer[BUFFER SIZE];

int insert item(buffer item item)

{ /* insert item into buffer

return 0 if successful, otherwise

return -1 indicating an error condition */ }

int remove item(buffer item *item)

{ /* remove an object from buffer placing it in item

return 0 if successful, otherwise

return -1 indicating an error condition */ }

Figure 2 Outline of buffer operations.

The Producer and Consumer Threads

The producer thread will alternate between sleeping for a random period of time and inserting a random integer into the buffer. Random numbers will be produced using the rand() function, which produces random integers between 0 and RAND MAX. The consumer will also sleep for a random period of time and, upon awakening, will attempt to remove an item from the buffer. An outline of the producer and consumer threads appears in Figure 4

#include "buffer.h"

int main(int argc, char *argv[])

{ /* 1. Get command line arguments argv[1],argv[2],argv[3] */

/* 2. Initialize buffer */

/* 3. Create producer thread(s) */

/* 4. Create consumer thread(s) */

/* 5. Sleep */

/* 6. Exit */ }

Figure 3 Outline of skeleton program.

#include /* required for rand() */

#include "buffer.h"

void *producer(void *param)

{ buffer item item;

while (true) {

/* sleep for a random period of time */

sleep(...);

/* generate a random number */

item = rand();

if (insert item(item))

fprintf("report error condition");

else

printf("producer produced %dn",item); }

void *consumer(void *param) {

buffer item item;

while (true) {

/* sleep for a random period of time */

sleep(...);

if (remove item(&item))

fprintf("report error condition");

else

printf("consumer consumed %dn",item); }

Figure 4 An outline of the producer and consumer threads.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

XML Data Management Native XML And XML Enabled Database Systems

Authors: Akmal Chaudhri, Awais Rashid, Roberto Zicari, John Fuller

1st Edition

0201844524, 978-0201844528

More Books

Students also viewed these Databases questions

Question

Write the history of "THE AMERICAN REVOLUTION" from 1765 to 1783.

Answered: 1 week ago

Question

1. Which position would you take?

Answered: 1 week ago