『易坊知识库摘要_操作系统|操作系统生产者消费者问题C语言』本程序中,main()函数需要三个参数:主线程休眠时间;生产者线程数;消费者线程数。各线程的休眠等待时间是随机的。程序代码:#include#include#include#include#define BUFFER_SIZE 5typedef int bu...
按关键词阅读:
生产者
操作系统
语言
问题
消费者
1、操作系统概念第七版 中的实验项目:生产者消费者问题 。
本程序中 , main()函数需要三个参数:主线程休眠时间;生产者线程数;消费者线程数 。
各线程的休眠等待时间是随机的 。
程序代码:#include#include#include#include#define BUFFER_SIZE 5typedef int buffer_item;
struct vint i;
buffer_item bufferBUFFER_SIZE+1;
buffer_item front=0,rear=0;
HANDLE mutex,empty,full;
int insert_item(buffer_item item)/*ins 。
2、ert item into bufferreturn 0 if successful,otherwise return -1 indicating an error condition*/if(rear+1)%(BUFFER_SIZE+1)=front)return 1;
bufferrear=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,ot 。
3、herwise reutrn -1 indication an error condition */if(front = rear)return 1;
*item=bufferfront;
front=(front+1) % (BUFFER_SIZE+1);
return 0;
DWORD WINAPI producer(PVOID Param)int rand1;
struct v data=https://www.renrendoc.com/paper/*(struct v *)Param;
srand(unsigned)time(0);
while (1) Sleep(rand()%101*10);
WaitForSingleObject(empty,INFINIT 。
4、E);
WaitForSingleObject(mutex,INFINITE);
rand1 =rand();
printf(producer has producerd %d By %dn,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=https://www.renrendoc.com/paper/*(struct v *)Param;
srand(unsigned)ti 。
5、me(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 co 。
6、mmand line arguments argv1)(the number of producer threads),argv2(the number of consumer threads),argv3(sleep time)*/*Initialize buffer*/int sleeptime,pnum,snum;
int *ThreadIdP,*ThreadIdS,i;
struct v *countp,*counts;
HANDLE *ThreadHandleP,*ThreadHandleS;
sleeptime=atoi(argv1);
pnum=atoi(argv2);
snum=atoi( 。
7、argv3);
/*srand(time(NULL);
sleeptime=9000;
pnum=3;
snum=3;
*/ThreadHandleP=(HANDLE * )malloc(pnum * sizeof(HANDLE);
ThreadHandleS=(HANDLE * )malloc(snum * sizeof(HANDLE);
ThreadIdP=(int * )malloc(pnum * sizeof(int);
ThreadIdS=(int * )malloc(pnum * sizeof(int);
mutex=CreateMutex(NULL,FALSE,NULL);
empty=Create 。
8、Semaphore(NULL,BUFFER_SIZE,BUFFER_SIZE,NULL);
full=CreateSemaphore(NULL,0,BUFFER_SIZE+1,NULL);
/*Create producer thread(s)*/countp=(struct v *)malloc(pnum+1)*sizeof(struct v);
counts=(struct v *)malloc(snum+1)*sizeof(struct v);
for(i=0;
i#include#include#include#define BUFFER_SIZE 5typedef int buffer_ite 。
9、m;
struct vint i;
buffer_item bufferBUFFER_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;
bufferrear=item;
rear=(rear+ 。
10、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=bufferfront;
front=(front+1) % (BUFFER_SIZE+1);
return 0;
DWORD WINAPI producer(PVOID 。
11、 Param)int rand1;
struct v data=https://www.renrendoc.com/paper/*(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 %dn,rand1,data.i);
if(insert_item(rand1)printf(insert data error!n);
Release 。
12、Mutex(mutex);
ReleaseSemaphore(full,1,NULL);
DWORD WINAPI consumer(PVOID Param)int rand1;
struct v data=https://www.renrendoc.com/paper/*(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);
elsep 。
13、rintf(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 argv1)(the number of producer threads),argv2(the number of consumer threads),argv3(sleep time)*/*Initialize buffer*/int sleeptime,pnum,snum;
DW 。
14、ORD *ThreadIdP,*ThreadIdS,i;
struct v *countp,*counts;
HANDLE *ThreadHandleP,*ThreadHandleS;
来源:(未知)
【傻大方】网址:http://www.shadafang.com/a/2020/1223/002642067.html
标题:操作系统|操作系统生产者消费者问题C语言