Find Jobs
Hire Freelancers

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

$30-250 USD

Selesai
Dibuat sekitar 6 tahun yang lalu

$30-250 USD

Dibayar ketika dikirim
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 Proyek: 16724266

Tentang proyek

4 proposal
Proyek remot
Aktif 6 tahun yang lalu

Ingin menghasilkan uang?

Keuntungan menawar di Freelancer

Tentukan anggaran dan garis waktu Anda
Dapatkan bayaran atas pekerjaan Anda
Uraikan proposal Anda
Gratis mendaftar dan menawar pekerjaan
Diberikan kepada:
Avatar Pengguna
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 dalam 1 hari
5,0 (43 ulasan)
5,9
5,9

Tentang klien

Bendera UNITED STATES
champaign, United States
5,0
3
Memverifikasi Metode pembayaran
Anggota sejak Apr 4, 2018

Verifikasi Klien

Terima kasih! Kami telah mengirim Anda email untuk mengklaim kredit gratis Anda.
Anda sesuatu yang salah saat mengirimkan Anda email. Silakan coba lagi.
Pengguna Terdaftar Total Pekerjaan Terpasang
Freelancer ® is a registered Trademark of Freelancer Technology Pty Limited (ACN 142 189 759)
Copyright © 2024 Freelancer Technology Pty Limited (ACN 142 189 759)
Memuat pratinjau
Izin diberikan untuk Geolokasi.
Sesi login Anda telah kedaluwarsa dan Anda sudah keluar. Silakan login kembali.