Lengkap

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 */

...

wait(empty);

wait(mutex);

...

/* add next produced to the buffer */

...

signal(mutex);

signal(full);

} while (true);

Figure 5.9 The structure of the producer process.

do {

wait(full);

wait(mutex);

...

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

...

signal(mutex);

signal(empty);

...

/* 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.

Requirement

 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

Kemahiran: Himpunan, Pengaturcaraan C, Pengaturcaraan C#, Pengaturcaraan C++, Kejuruteraan Perisian

Lihat lagi: 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

Tentang Majikan:
( 0 ulasan ) champaign, United States

ID Projek: #16724266

Dianugerahkan kepada:

naryl

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 Lagi

$55 USD dalam sehari
(2 Ulasan)
1.6

7 pekerja bebas membida secara purata $125 untuk pekerjaan ini

$155 USD dalam sehari
(35 Ulasan)
5.5
$155 USD dalam 3 hari
(20 Ulasan)
5.2
$111 USD dalam 3 hari
(5 Ulasan)
4.2
Olegk0204

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 Lagi

$111 USD dalam 3 hari
(9 Ulasan)
3.6
$130 USD dalam 4 hari
(4 Ulasan)
3.0
$155 USD dalam 3 hari
(0 Ulasan)
0.0