用c语言编写的关于生产者和消费者进程同步问题

时间:2022-08-03 20:22:28
下面一段代码是关于生产者和消费者进程的同步和互斥的代码,我就是不明白为什么运行后最多只显示九十多步就停止了呀。

#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;
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");
else
printf("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),argv[3](sleep time)*/
/*Initialize buffer*/
int sleeptime,pnum,snum;
DWORD *ThreadIdP,*ThreadIdS,i;
struct v *countp,*counts;
HANDLE *ThreadHandleP,*ThreadHandleS;
/*sleeptime=atoi(argv[1]);
pnum=atoi(argv[2]);
snum=atoi(argv[3]);*/
//srand(time(NULL));
sleeptime=9000;
pnum=3;
snum=3;
ThreadHandleP=(HANDLE * )malloc(pnum * sizeof(HANDLE));
ThreadHandleS=(HANDLE * )malloc(snum * sizeof(HANDLE));
ThreadIdP=(DWORD * )malloc(pnum * sizeof(DWORD));
ThreadIdS=(DWORD * )malloc(pnum * sizeof(DWORD));
mutex=CreateMutex(NULL,FALSE,NULL);
empty=CreateSemaphore(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<pnum;i++)
{
countp[i+1].i=i+1;
ThreadHandleP[i]=CreateThread(NULL,0,producer,&countp[i+1],0,&ThreadIdP[i]);
}
/*Create consumer thread(s)*/
for(i=0;i<snum;i++)
{
counts[i+1].i=i+1;
ThreadHandleS[i]=CreateThread(NULL,0,consumer,&counts[i+1],0,&ThreadIdS[i]);
}
/*Sleep*/
Sleep(sleeptime);
/*Exit*/
return 0;
}

2 个解决方案

#1


时间到了,主程序退出了。

  Sleep(sleeptime);
    /*Exit*/
    return 0;

#2


谢谢了。把那行代码给忘了

#1


时间到了,主程序退出了。

  Sleep(sleeptime);
    /*Exit*/
    return 0;

#2


谢谢了。把那行代码给忘了