Producer-consumer Problem using C/C++ and Pthread APIs (Thread-safe)

Producer-consumer Problem using C/C++ and Pthread APIs (Thread-safe)

For this project, you will use standard counting semaphores for empty and full and a mutex lock, rather than abinary semaphore, to represent mutex. The producer and consumer — running 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 the Windows API.

do {


/* produce an item in next produced */





/* add next produced to the buffer */




} while (true);

Figure 5.9 The structure of the producer process.

do {




/* remove an item from buffer to next consumed */





/* consume the item in next consumed */

... } while (true);

Figure 5.10 The structure of the consumer process.

#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 5.24 Outline of buffer operations.

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 willbe manipulatedasa circular [url removed, login to view] definitionof buffer item,along with the size of the buffer, can be stored in a headerfile 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 5.24. The insert item() and remove item() functions will synchronize the producer and consumer using the algorithms outlined in Figures 5.9 and 5.10. 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 parameterson the command line:

1. How long to sleep before terminating

2. The number of producer threads

3. The number of consumer threads

#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 5.25 Outline of skeleton program.

A skeletonfor this function appears in Figure 5.25.


 Makefile should be provided.

 The name of the executable file should “producer_consumer2”.

 RAND_MAX should be 10 (i.e., 10 seconds)

 Your program should be use all of the following Pthread APIs correctly!

o mutex_lock, mutex_unlock, sem_wait, sem_post

o If you don’t use both mutex and semaphore, you will automatically lose 50% of the total points.

 In your code, you can use sleep(seconds) API to make threads to sleep for seconds.

 You need to provide a write-up in plain text format describing your source code (300 words or more).

 Each producer/consumer thread should print out a message whenever some value is inserted into the buffer or removed from the buffer along with its own identifier. Use the following message formats:

[consumer thread ID: 2] removed an item (word: apple) from the buffer

[producer thread ID: 1] inserted an item (word: orange) to the buffer

Skills: Assembly, C Programming, C# Programming, C++ Programming, Software Architecture

See more: pthread shared memory example, producer consumer problem using mutex, multiple producer consumer problem in c, producer consumer problem in c using threads, producer consumer problem in c using semaphores and threads, producer consumer circular buffer c, producer consumer problem in c using queue, producer consumer problem using threads, producer consumer problem using monitors unix, producer consumer problem using, producer consumer problem using semaphores threads, write code producer consumer problem using semaphores, producer consumer problem using unix program, producer consumer problem using concurrent programming, program producer consumer problem using unix

About the Employer:
( 0 reviews ) champaign, United States

Project ID: #16724266

Awarded to:


Hello. I've actually just finished a similar assignment about Dining Philosophers. :) pthread, semaphores, a Makefile etc. I can deliver it in about the same two hours the other one took me. > using the algorithms More

$55 USD in 1 day
(2 Reviews)

7 freelancers are bidding on average $125 for this job

$155 USD in 1 day
(35 Reviews)
$155 USD in 3 days
(20 Reviews)
$111 USD in 3 days
(5 Reviews)

Dear Client! I have read your post carefully with my interest. I have had great experience programming Visual Studio C#, Visual Studio C++, Eclipse CDT, gcc, g++, and many other projects for 6 years on Windows, Mac More

$111 USD in 3 days
(9 Reviews)
$130 USD in 4 days
(4 Reviews)
$155 USD in 3 days
(0 Reviews)