生产者消费者线程问题

  • v3_270242
    了解作者
  • 2.3MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-28 09:56
    上传日期
生产者消费者线程实验+报告适用于操作系统课程设计。
生产着消费者问题.zip
内容介绍
#include <windows.h> #include <iostream> unsigned short SIZE_OF_BUFFER; //缓冲区长度 unsigned short ProductID = 0; //产品号 unsigned short ConsumeID = 0; //将被消耗的产品号 unsigned short in = 0; //产品进缓冲区时的缓冲区下标 unsigned short out = 0; //产品出缓冲区时的缓冲区下标 int *g_buffer; //缓冲区是个循环队列 bool g_continue = true; //控制程序结束 HANDLE g_hMutex; //用于线程间的互斥 HANDLE g_hFullSemaphore; //当缓冲区满时迫使生产者等待 HANDLE g_hEmptySemaphore; //当缓冲区空时迫使消费者等待 DWORD WINAPI Producer(LPVOID); //生产者线程 DWORD WINAPI Consumer(LPVOID); //消费者线程 unsigned short PRODUCERS_COUNT; //生产者的个数 unsigned short CONSUMERS_COUNT; //消费者的个数 unsigned short THREADS_COUNT;//总的线程数 int main() { std::cout<<"put in buffer count: "; std::cin>>SIZE_OF_BUFFER; std::cout<<"put in producers count: "; std::cin>>PRODUCERS_COUNT; std::cout<<"put in consumers count: "; std::cin>>CONSUMERS_COUNT; getchar(); THREADS_COUNT=CONSUMERS_COUNT+PRODUCERS_COUNT; g_buffer=(int *)malloc(SIZE_OF_BUFFER*sizeof(int)); HANDLE *hThreads; hThreads=(HANDLE *)malloc(PRODUCERS_COUNT*sizeof(HANDLE)); //各线程的handle DWORD *producerID; producerID=(DWORD *)malloc(CONSUMERS_COUNT*sizeof(DWORD)); //生产者线程的标识符 DWORD *consumerID; consumerID=(DWORD *)malloc(THREADS_COUNT*sizeof(DWORD)); //消费者线程的标识符 //创建各个互斥信号 g_hMutex = CreateMutex(NULL,FALSE,NULL); g_hFullSemaphore = CreateSemaphore(NULL,SIZE_OF_BUFFER,SIZE_OF_BUFFER,NULL); g_hEmptySemaphore = CreateSemaphore(NULL,0,SIZE_OF_BUFFER,NULL); //创建生产者线程 for (int i=0;i <PRODUCERS_COUNT;++i){ hThreads[i]=CreateThread(NULL,0,Producer,NULL,0,0); if (hThreads[i]==NULL) return ERROR; } //创建消费者线程 for ( i=0;i <CONSUMERS_COUNT;++i){ hThreads[PRODUCERS_COUNT+i]=CreateThread(NULL,0,Consumer,NULL,0,0); if (hThreads[i]==NULL) return ERROR; } while(g_continue){ if(getchar()) g_continue = false; } return 0; } //生产一个产品。简单模拟了一下,仅输出新产品的ID号 void Produce() { ++ProductID; std::cerr << "Producing a product ... "<< "Succeed "; g_buffer[in] = ProductID; in = (in+1)%SIZE_OF_BUFFER; //输出缓冲区当前的状态 for (int i=0;i <SIZE_OF_BUFFER;++i) if (i==in) std::cout << " buffer status: "<<ProductID-ConsumeID<<'/'<<SIZE_OF_BUFFER<< std::endl<< std::endl<< std::endl; } //消耗一个产品 void Consume() { std::cerr << "Consuming a product ... " << "Succeed " ; ConsumeID = g_buffer[out]; out = (out+1)%SIZE_OF_BUFFER; //输出缓冲区当前的状态 for (int i=0;i <SIZE_OF_BUFFER;++i) if (i==out) std::cout << " buffer status: "<<ProductID-ConsumeID<<'/'<<SIZE_OF_BUFFER<< std::endl<< std::endl<< std::endl; } //生产者 DWORD WINAPI Producer(LPVOID lpPara) { while(g_continue){ WaitForSingleObject(g_hFullSemaphore,INFINITE); WaitForSingleObject(g_hMutex,INFINITE); Produce(); Sleep(1500); ReleaseMutex(g_hMutex); ReleaseSemaphore(g_hEmptySemaphore,1,NULL); } return 0; } //消费者 DWORD WINAPI Consumer(LPVOID lpPara) { while(g_continue){ WaitForSingleObject(g_hEmptySemaphore,INFINITE); WaitForSingleObject(g_hMutex,INFINITE); Consume(); Sleep(1500); ReleaseMutex(g_hMutex); ReleaseSemaphore(g_hFullSemaphore,1,NULL); } return 0; }
评论