logo资料库

读者写者问题的实现.doc

第1页 / 共13页
第2页 / 共13页
第3页 / 共13页
第4页 / 共13页
第5页 / 共13页
第6页 / 共13页
第7页 / 共13页
第8页 / 共13页
资料共13页,剩余部分请下载后查看
一、实验目的和要求
二、实验中的关键词
三、源代码
四、运行结果
五、实验总结
2012--2013 学年第 一 学期 计算机科学与 工程 学院期末实验《 操作系统 》 学号: 201071030216 姓名: 何莹 成绩: 评语: 1、读者写者问题的实现 2、用 P、V 操作实现读者写者问题 3、要求:所有读者和写者访问同一个文件
目录 一、 实验目的和要求................................................................ 3 二、 实验中的关键词................................................................ 3 三、 源代码.................................................................................3 四、 运行结果...........................................................................12 五、 实验总结...........................................................................13 2
一、 实验目的和要求 用 P、V 操作实现读者写者问题,写者不多于 5 个,读者不多于 20 个。所有读者和写者访问同一个文件 all.txt,每个写者向文件添加一 行,内容是“I'm the writer number X. It is TIME. ”,其中 X 是 pid,TIME 是访问文件的时间,格式为 hh-mm-ss.ddd;每个读者读出最后一个写者 的 pid 和写的时间,并在屏幕上输出。每个读者或写者的访问次数为 32-100 之间的随机数,同一个写者相邻两次的访问间隔不得短于 0.1 秒, 同一个读者相邻两次的访问间隔不得短于 0.05 秒。 二、 实验中的关键词 1、 CreateThread 完成线程创建 2、 ExitThread 用于结束当前线程 3、 Sleep 可在指定的时间内挂起当前线程 4、 信号量控制: WaitForSingleObject 可在指定的时间内等待指定对象为可用状 态; hHandle 为等待的对象,也就是实现同步或者互斥的对象。 5、实现信号量互斥和同步: CreateSemaphore 用于创建信号量,根据参数的不同可以利用它实 现互斥和同步。 ReleaseSemaphore 用于释放信号量,使用后相应的信号量加 1。 三、 源代码 #include 3
#include #include #include #include #include #define MAX_PERSON 10 #define READER #define WRITER 0 1 #define END -1 #define R READER #define W WRITER typedef struct _Person { HANDLE Thread; int Type; int StartTime; int WorkTime; int ID; }Person; Person Persons[MAX_PERSON]; int NumPerson = 0; long CurrentTime= 0; int PersonLists[] = { 4
1,R,1,3, 2,W,2,5, 3,W,5,5, 4,R,3,5, 5,R,15,2, END, }; int rfirst = 0; int wfirst = 0; int NumOfReaders = 0; int NumOfWriters = 0; HANDLE rsem; HANDLE wsem; HANDLE z; HANDLE ReadMutex; HANDLE WriteMutex; void CheckPersonList(int *pPersonList); bool CreateReader(int StartTime,int WorkTime); bool CreateWriter(int StartTime,int WorkTime); DWORD WINAPI ReaderProc(LPVOID lpParam); DWORD WINAPI WriterProc(LPVOID lpParam); #include "Writerprior.h" 5
int main() { rsem= CreateSemaphore(NULL,1,1,NULL); wsem = CreateSemaphore(NULL,1,1,NULL); z= CreateSemaphore(NULL,1,1,NULL); ReadMutex = CreateSemaphore(NULL,1,1,NULL); WriteMutex = CreateSemaphore(NULL,1,1,NULL); CurrentTime = 0; while(true) { } CheckPersonList(PersonLists); CurrentTime++; Sleep(600); printf("当前时间 = %d:\n",CurrentTime); if(CurrentTime==20) break; system("pause"); CloseHandle(rsem); CloseHandle(wsem); CloseHandle(z); CloseHandle(ReadMutex); CloseHandle(WriteMutex); 6
return 0; } void CheckPersonList(int *pPersonLists) { int i=0; int *pList = pPersonLists; bool P; while(pList[0] != END) { if(pList[2] == CurrentTime) { } switch(pList[1]) { case R: P = CreateReader(pList[2],pList[3]); break; case W: P = CreateWriter(pList[2],pList[3]); break; } if(!P) printf("Create Person %d is wrong\n",pList[0]); 7
pList += 4; } } DWORD WINAPI ReaderProc(LPVOID lpParam) { Person *pPerson = (Person*)lpParam; pPerson->ID = ++NumOfReaders; WaitForSingleObject(z,INFINITE);//P(z), printf("\t\t 读者 %d 申请读数据...\n",pPerson->ID); WaitForSingleObject(rsem,INFINITE); printf("Reader %d is requesting the Shared Buffer...\n",pPerson->ID); WaitForSingleObject(ReadMutex,INFINITE); rfirst++; if(rfirst == 1) { WaitForSingleObject(wsem,INFINITE); } ReleaseSemaphore(ReadMutex,1,NULL); V(ReadMutex) ReleaseSemaphore(rsem,1,NULL); ReleaseSemaphore(z,1,NULL); printf("\t\t 读者 %d 申请成功\n",pPerson->ID); pPerson->StartTime = CurrentTime; 8
分享到:
收藏