我这是C++的 其实都一样 改个cout cin 就是C了 没错误 这是我们操作系统实验
#includewindows.h
#includeiostream.h
#includestdio.h
HANDLE hMutex;
HANDLE hFull, hEmpty; HANDLE p,c;
DWORD WINAPI Producer(LPVOID);
DWORD WINAPI Consumer(LPVOID);
int a[100],i,j;
void main()
{
cout生产者消费者问题endl;
a[100]=i=j=0;
hMutex=CreateMutex(NULL,FALSE,NULL);
hFull=CreateSemaphore(NULL,0,1,NULL);
hEmpty=CreateSemaphore(NULL,1,1,NULL);
p=CreateThread(NULL,0,Producer,NULL,0,NULL);
c=CreateThread(NULL,0,Consumer,NULL,0,NULL);
Sleep(150000);
}
void Produce(){
cout生产者生产产品...endl;
cout生产成功endl;
}
DWORD WINAPI Producer(LPVOID lpPara){
while(TRUE){
WaitForSingleObject(hEmpty,INFINITE);
WaitForSingleObject(hMutex,INFINITE);
Produce();
a[i]=i;
i=(i+1)%100;
Sleep(5000);
ReleaseMutex(hMutex);
ReleaseSemaphore(hFull,1,NULL); }
return 0;
}
void Consume()
{
cout消费者消费产品 ... endl;
cout消费成功endl;
cout请等待...endl;
}
DWORD WINAPI Consumer(LPVOID lpPara)
{
while(TRUE){
WaitForSingleObject(hFull,INFINITE);
WaitForSingleObject(hMutex,INFINITE);
cout缓冲区a[j]endl;
a[j]=999;
j=(j+1)%100;
Consume();
Sleep(5000);
ReleaseMutex(hMutex);
ReleaseSemaphore(hEmpty,1,NULL);
}
return 0;
}