电脑桌面
添加小米粒文库到电脑桌面
安装后可以在桌面快捷访问

操作系统生产者消费者问题C语言

操作系统生产者消费者问题C语言_第1页
1/6
操作系统生产者消费者问题C语言_第2页
2/6
操作系统生产者消费者问题C语言_第3页
3/6
《操作系统概念》第七版 中的实验项目:生产者消费者问题。本程序中,main()函数需要三个参数:主线程休眠时间;生产者线程数;消费者线程数。各线程的休眠等待时间是随机的。程序代码:#include〈stdio。h〉#include#include#define BUFFER_SIZE 5typedef int buffer_item;struct v{int i;};buffer_item buffer[BUFFER_SIZE+1];buffer_item front=0,rear=0;HANDLE mutex,empty,full;int insert_item(buffer_item item){/*insert item into bufferreturn 0 if successful,otherwise return -1 indicating an error condition*/if((rear+1)%(BUFFER_SIZE+1)==front)return 1;buffer[rear]=item;rear=(rear+1)%(BUFFER_SIZE+1);return 0;}int remove_item(buffer_item *item){/*remove an object from buffer placing it in item return 0 if successful,otherwise reutrn -1 indication an error condition */if(front == rear)return 1;*item=buffer[front];front=(front+1) % (BUFFER_SIZE+1);return 0;}DWORD WINAPI producer(PVOID Param){int rand1;struct v data=*(struct v *)Param;srand((unsigned)time(0));while (1) {Sleep(rand()%101*10);WaitForSingleObject(empty,INFINITE);WaitForSingleObject(mutex,INFINITE);rand1 =rand();printf(”producer has producerd %d By %d\n",rand1,data.i);if(insert_item(rand1))printf(”insert data error!\n”);ReleaseMutex(mutex);ReleaseSemaphore(full,1,NULL);}}DWORD WINAPI consumer(PVOID Param){int rand1;struct v data=*(struct v *)Param;srand((unsigned)time(0));while (1){Sleep(rand()%101*10);WaitForSingleObject(full,INFINITE);WaitForSingleObject(mutex,INFINITE);if(remove_item(&rand1))printf("remove data error! \n”);elseprintf("consumer consumed %d By %d \n”,rand1,data。i);ReleaseMutex(mutex);ReleaseSemaphore(empty,1,NULL);}}int main(int argc,char *argv[]){/ * Get command line arguments argv[1] ) (the number of producer threads) , argv[2] ( the number of consumer threads),arg...

1、当您付费下载文档后,您只拥有了使用权限,并不意味着购买了版权,文档只能用于自身使用,不得用于其他商业用途(如 [转卖]进行直接盈利或[编辑后售卖]进行间接盈利)。
2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。
3、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。

碎片内容

操作系统生产者消费者问题C语言

确认删除?
VIP
微信客服
  • 扫码咨询
会员Q群
  • 会员专属群点击这里加入QQ群
客服邮箱
回到顶部