Find Jobs
Hire Freelancers

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

$30-250 USD

I përfunduar
Postuar almost 6 years ago

$30-250 USD

Paguhet në dorëzim
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 [login to view URL] 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
ID e Projektit: 16724266

Rreth projektit

4 propozime
Projekt në distancë
Aktive 6 yrs ago

Po kërkoni të fitoni para?

Përfitimet e ofertës për Freelancer

Vendosni buxhetin dhe afatin tuaj
Paguhuni për punën tuaj
Përshkruani propozimin tuaj
Është falas të regjistrohesh dhe të bësh oferta për punë
I dhënë për:
Avatari i Përdoruesit
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 outlined in Figures 5.9 and 5.10. I guess you have the document with the figures too. :)
$55 USD në 1 ditë
5,0 (43 përshtypje)
5,9
5,9

Rreth klientit

Flamuri i UNITED STATES
champaign, United States
5,0
3
Mënyra e pagesës u verifikua
Anëtar që nga pri 4, 2018

Verifikimi i klientit

Faleminderit! Ne ju kemi dërguar me email një lidhje për të kërkuar kredinë tuaj falas.
Ndodhi një gabim gjatë dërgimit të email-it tuaj. Ju lutemi provoni përsëri.
Përdorues të regjistruar Punë të postuara
Freelancer ® is a registered Trademark of Freelancer Technology Pty Limited (ACN 142 189 759)
Copyright © 2024 Freelancer Technology Pty Limited (ACN 142 189 759)
Po ngarkohet shikimi paraprak
Leja u dha për Geolocation.
Seanca e hyrjes ka skaduar dhe ke dalë. Hyr sërish.