请哪为高手帮我调试一下,把里面的错误都该好啊~谢谢了~
1. 生产者-消费者问题参考源代码:(测试数据文件pc_data.dat应与源程序放在相同文件夹)
#include <windows.h>
#include <fstream.h>
#include <stdio.h>
#define PRODUCER 'P' //生产者
#define CONSUMER 'C' //消费者
#define INTE_PER_SEC 1000 //每秒时钟中断数目
#define MAX_THREAD_NUM 64 //最大线程数目
CRITICAL_SECTION PC_Buffer;
HANDLE h_semaphore_empty, h_semaphore_full;
DWORD n_buffer;
struct ThreadInfo{
int serial; //线程序号
char entity; //线程类别(P或C)
double delay; //线程延迟
double persist; //线程访问缓冲区时间
};
void ProducerThread(void* p) //生产者线程
{
DWORD m_delay;
DWORD m_persist;
int m_serial;
DWORD wait_for_semaphore_empty;
long count;
m_serial=((ThreadInfo*)(p))->serial;
m_delay=(DWORD)((ThreadInfo*)(p))->delay*INTE_PER_SEC;
m_persist=(DWORD)((ThreadInfo*)(p))->persist*INTE_PER_SEC;
Sleep(m_delay); //线程创建后,延迟等待一段时间后开始生产操作
printf("Producer %d wants to add product to buffer\n",m_serial);
wait_for_semaphore_empty=WaitForSingleObject(h_semaphore_empty,-1);//down(&empty)
EnterCriticalSection(&PC_Buffer); //down(&mutex)
printf("Producer %d begins to add product to buffer\n",m_serial);
Sleep(m_persist); //访问缓冲区
printf("Producer %d finished adding product\n",m_serial);
LeaveCriticalSection(&PC_Buffer); //up(&mutex)
ReleaseSemaphore(h_semaphore_full,1,&count); //up(&full)
}
void ConsumerThread(void* p) //消费者线程
{
DWORD m_delay;
DWORD m_persist;
int m_serial;
DWORD wait_for_semaphore_full;
long count;
m_serial=((ThreadInfo*)(p))->serial;
m_delay=(DWORD)((ThreadInfo*)(p))->delay*INTE_PER_SEC;
m_persist=(DWORD)((ThreadInfo*)(p))->persist*INTE_PER_SEC;
Sleep(m_delay); //线程创建后,延迟等待一段时间后开始消费操作
printf("Consumer %d wants to remove product from buffer\n",m_serial);
wait_for_semaphore_full=WaitForSingleObject(h_semaphore_full,-1); //down(&full)
EnterCriticalSection(&PC_Buffer); //down(&mutex)
printf("Consumer %d begins to remove product from buffer\n",m_serial);
Sleep(m_persist); //访问缓冲区
printf("Consumer %d finished removing product\n",m_serial);
LeaveCriticalSection(&PC_Buffer); //up(&mutex)
ReleaseSemaphore(h_semaphore_empty,1,&count); //up(&empty)
Sleep(m_delay); //消费产品
}
int main(int argc, char* argv[])
{
DWORD n_thread=0; //线程数目
DWORD thread_ID; //线程ID
DWORD wait_for_all; //等待所有线程结束
HANDLE h_Thread[MAX_THREAD_NUM]; //线程对象数组
ThreadInfo thread_info[MAX_THREAD_NUM];
ifstream inFile;
inFile.open("pc_data.dat"); //打开文件
printf("Begin to read file pcdata.dat\n");
inFile>>n_buffer; //读入缓冲区大小
inFile.get();
while(inFile){ //读入每个生产者/消费者信息
inFile>>thread_info[n_thread].serial;
inFile>>thread_info[n_thread].entity;
inFile>>thread_info[n_thread].delay;
inFile>>thread_info[n_thread].persist;
n_thread++;
inFile.get();
}
//初始化信号量和临界区
h_semaphore_empty=CreateSemaphore(NULL,n_buffer,MAX_THREAD_NUM,"buffer_empty");
h_semaphore_full=CreateSemaphore(NULL,0,MAX_THREAD_NUM,"buffer_full");
InitializeCriticalSection(&PC_Buffer);
for(int i=0;i<(int)n_thread;i++){
if(thread_info[i].entity==PRODUCER||thread_info[i].entity=='p'){ //创建生产者线程
h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(ProducerThread),&thread_info[i],0,&thread_ID);}
else{ //创建消费者线程
h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(ConsumerThread),&thread_info[i],0,&thread_ID);}
}
//等待所有线程结束
wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1);
printf("all producer and consumers have finished operating\n");
return 0;
}