《操作系统概念》第七版 中的实验项目:生产者消费者问题。本程序中,main()函数需要三个参数:主线程
休眠时间;生产者线程数;消费者线程数。各线程的休眠等待时间是随机的。
程序代码:
#include〈stdio。h〉
#include<stdlib.h〉
#include〈time。h>
#include<windows。h>
#define BUFFER_SIZE 5
typedef 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 buffer
return 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;
评论0
最新资源