Find Jobs
Hire Freelancers

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

$30-250 USD

已完成
已发布大约 6 年前

$30-250 USD

货到付款
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: 16724266

关于此项目

4提案
远程项目
活跃6 年前

想赚点钱吗?

在Freelancer上竞价的好处

设定您的预算和时间范围
为您的工作获得报酬
简要概述您的提案
免费注册和竞标工作
颁发给:
用户头像
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 在1天之内
5.0 (43条评论)
5.9
5.9

关于客户

UNITED STATES的国旗
champaign, United States
5.0
3
付款方式已验证
会员自4月 4, 2018起

客户认证

谢谢!我们已通过电子邮件向您发送了索取免费积分的链接。
发送电子邮件时出现问题。请再试一次。
已注册用户 发布工作总数
Freelancer ® is a registered Trademark of Freelancer Technology Pty Limited (ACN 142 189 759)
Copyright © 2024 Freelancer Technology Pty Limited (ACN 142 189 759)
加载预览
授予地理位置权限。
您的登录会话已过期而且您已经登出,请再次登录。