读者写者问题的错误

hanfeng1010 2010-10-26 09:50:51
#include <windows.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>
#include <fstream.h>
#include <io.h>
#define MAX_PERSON 100 //最多100人
#define READER R //读者
#define WRITER W //写者
#define END -1 //结束
#define R READER
#define W WRITER

typedef struct _Person
{

int m_nType;//进程类型(读写)
int m_nStartTime;//开始时间
int m_nWorkTime;//运行时间
int m_nID;//进程号
}Person;
Person g_Persons[MAX_PERSON];
HANDLE m_hThread[MAX_PERSON];//定义处理线程的句柄
int g_NumPerson = 0;
int g_NumOfReading = 0;//申请读进程的个数
int g_NumOfWriteRequest = 0;//申请写进程的个数
HANDLE g_hReadMutex;//读者信号
HANDLE g_hWriteMutex;//写者信号
DWORD WINAPI ReaderProc(void* p);
DWORD WINAPI WriterProc(void* p);
DWORD WINAPI ReaderProc(void* p)//读过程
{
// wait for the start time
DWORD m_waitt;
DWORD m_workt;
int m_id;
m_id=((Person *)(p))->m_nID;
m_waitt=(DWORD)(((Person *)(p))->m_nStartTime*1000);
m_workt=(DWORD)(((Person *)(p))->m_nWorkTime*1000);
Sleep(1000);
printf("Reader %d is Requesting ...\n",m_id);
Sleep(m_waitt);
printf("\n\n************************************************\n");
WaitForSingleObject(g_hReadMutex,INFINITE);//等待g_hReadSemaphore读信号,即当g_hReadSemaphore有信号时等待结束 相当于p操作
if(g_NumOfReading ==0)
{
WaitForSingleObject(g_hWriteMutex,INFINITE); //当第一个读者到了,如果g_hWriteSemaphore信号灯灭了,说明有写者再写,读者必须等待。即互斥写操作
}
g_NumOfReading++;
ReleaseMutex(g_hReadMutex); //还有读者,但是允许下一个读进程读取,相当于V操作// modify the reader's real start time


//pPerson->m_nStartTime = g_CurrentTime;
printf("Reader %d is Reading the Shared Buffer...\n",m_id);
printf("\n\n************************************************\n");
Sleep(m_workt);
//printf("Reader %d is Exit...\n",m_id);
printf("\n\n************************************************\n");
WaitForSingleObject(g_hReadMutex,INFINITE);
printf("Reader %d is Exit...\n",m_id);
g_NumOfReading--;
printf("%d",g_NumOfReading);
if(g_NumOfReading == 0)
{
ReleaseMutex(g_hWriteMutex);//此时没有读者,可以写
}
ReleaseMutex(g_hReadMutex);

return 0;


}
DWORD WINAPI WriterProc(void *p)
{
DWORD m_waitt;
DWORD m_workt;
int m_id;
m_id=((Person *)(p))->m_nID;
m_waitt=(DWORD)(((Person *)(p))->m_nStartTime*1000);
m_workt=(DWORD)(((Person *)(p))->m_nWorkTime*1000);
Sleep(m_waitt);
// wait for the start time

printf("Writer %d is Requesting ...\n",m_id);
printf("\n\n************************************************\n");
//g_NumOfWriteRequest++;
//在写者优先的时候需要用自加来初始信号值,而在读者优先的时是通过读者操作来控制信号值
WaitForSingleObject(g_hWriteMutex,INFINITE);
printf("Writer %d is Writting the Shared Buffer...\n",m_id);
Sleep(m_workt);
printf("Writer %d is Exit...\n",m_id);
printf("\n\n************************************************\n");
ReleaseMutex(g_hWriteMutex);
return 0;
}







void READFILE(char * file)
{
int n = 0;
DWORD thread_ID;
DWORD wait_for_all;
g_hReadMutex = CreateMutex(NULL,FALSE,NULL);//创建信号量,当前可用的资源数为1,最大为100
g_hWriteMutex = CreateMutex(NULL,FALSE,NULL);//创建信号量,当前可用的资源数为1,最大为100

ifstream inFile;
inFile.open(file);
while(inFile)
{
inFile>>g_Persons[n].m_nID;
inFile>>g_Persons[n].m_nType;
inFile>>g_Persons[n].m_nStartTime;
inFile>>g_Persons[n].m_nWorkTime;
n++;
inFile.get();
}
for(int i=0;i<n;i++)
{
if(g_Persons[i].m_nType=='W')
m_hThread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WriterProc),&g_Persons[i],0,&thread_ID);
else //if(g_Persons[i].m_nType=='R')
m_hThread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(ReaderProc),&g_Persons[i],0,&thread_ID);
//else
//printf("线程创建失败\n");
}
wait_for_all=WaitForMultipleObjects(n,m_hThread,TRUE,-1);
cout<<"所有线程执行完毕!";
}
int main()
{//显示主菜单
char ch;
while(true)
{
cout<<"***********************"<<endl;
cout<<"1.读者写者问题演示"<<endl;
cout<<"2.退出"<<endl;
cout<<"请输入[1~2]:"<<endl;
cout<<"***********************"<<endl;
ch=(char)_getch();
system("cls");
if(ch=='2')return 0;
else
READFILE("c:\\pc.dat");
cout<<"请选择:";
_getch();
system("cls");
}
return 0;
}
...全文
72 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

6,850

社区成员

发帖
与我相关
我的任务
社区描述
Windows 2016/2012/2008/2003/2000/NT
社区管理员
  • Windows Server社区
  • qishine
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧