DHU 操作系统读者写者实验

来源:互联网 发布:单片机仿真软件介绍 编辑:程序博客网 时间:2024/04/29 17:15

实验报告链接:

http://wenku.baidu.com/view/49ff72eb08a1284ac8504351.html

 

测试数据内容:

1 R 3 52 W 4 53 R 5 24 R 6 55 W 5.1 3


Code:

/*************************************************************************************ExpName: 读者写者Author: J_DarkDate: 2013/10/15*************************************************************************************/#include  "windows.h"#include  <conio.h>#include  <io.h>#include  <cstdlib>#include  <fstream.h>#include  <string>#include  <cstdio>const int max_thread_num = 64;//最大线程数目const int max_file_num = 32;const int max_str_len = 32;const int perSecond = 1000;int readCount = 0;//申请资源的读者线程数目int writeCount = 0;//申请资源的写者线程数目CRITICAL_SECTION RP_Writer;//临界区对象, 读者优先-遏制写者CRITICAL_SECTION CS_Writer;//临界区对象, CRITICAL_SECTION CS_Reader;   //临界区对象,struct Thread{int    Number;// 线程序号char   Type;// 线程种类(读者或写者)double WaitTime;// 等待时间double RunTime;// 运行时间};// 读者优先代码部分/*************************************************************************************/// 读者优先-读者线程void RP_ReaderThread(void *p){HANDLE Mutex;Mutex=OpenMutex(MUTEX_ALL_ACCESS, FALSE, "mutex_for_readCount");DWORD WaitForMutex, RunTime, WaitTime;int Number;Number=((Thread*)(p))->Number;    WaitTime=(DWORD)(((Thread*)(p))->WaitTime * perSecond);    RunTime=(DWORD)(((Thread*)(p))->RunTime * perSecond);    Sleep(WaitTime);printf ("Reader thread %d sents the reading require.\n",Number);//读者线程从创建m_delay秒后,发送读申请WaitForMutex = WaitForSingleObject(Mutex, -1);// P(h_mutex)readCount++;if(readCount == 1)// 读者线程进入临界区,实现读者优先{EnterCriticalSection(&RP_Writer);}ReleaseMutex(Mutex);// V(h_mutex)printf("Reader thread %d begins to read file.\n",Number);// 读者线程开始作业Sleep(RunTime);// 作业时间printf("Reader thread %d finished reading file.\n",Number);// 作业完成WaitForMutex=WaitForSingleObject(Mutex,-1);// P(h_mutex)  读者线程对象已触发readCount--;// 读者线程退出if(readCount == 0)// 所有读者线程都已作业完毕,释放临界区对象,写者可作业{LeaveCriticalSection(&RP_Writer);}ReleaseMutex(Mutex);// V(h_mutex)}// 读者优先-写者线程void RP_WriterThread(void *p){DWORD WaitTime;DWORD RunTime;int Number;Number=((Thread*)(p))->Number;    WaitTime=(DWORD)(((Thread*)(p))->WaitTime * perSecond);    RunTime=(DWORD)(((Thread*)(p))->RunTime * perSecond);    Sleep(WaitTime);printf("Writer thread %d sents the writing require.\n", Number);    EnterCriticalSection(&RP_Writer);    printf("Writer thread %d begins to write the  file.\n", Number);    Sleep(RunTime);     printf("Writer thread %d finishing writing to the file.\n", Number);    LeaveCriticalSection(&RP_Writer);}// 读者优先主控程序void ReaderPriority(char *file){DWORD n_Thread = 0;DWORD Thread_ID;DWORD WaitForAll;HANDLE Mutex = CreateMutex(NULL, FALSE, "mutex_for_readCount");HANDLE h_Thread[max_thread_num];Thread thread[max_thread_num];readCount = 0;InitializeCriticalSection(&RP_Writer);ifstream inFile;inFile.open(file);printf("****************  Reader Priority(读者优先)  ****************\n\n");while(inFile){inFile >> thread[n_Thread].Number;inFile >> thread[n_Thread].Type;inFile >> thread[n_Thread].WaitTime;inFile >> thread[n_Thread++].RunTime;inFile.get();}for(int i=0; i<(int)(n_Thread); i++){if(thread[i].Type == 'R' || thread[i].Type == 'r'){h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_ReaderThread),&thread[i],0,&Thread_ID);}else{h_Thread[i]=CreateThread(NULL,0,                   (LPTHREAD_START_ROUTINE)(RP_WriterThread),   &thread[i],0,&Thread_ID);}}WaitForAll = WaitForMultipleObjects(n_Thread, h_Thread, TRUE, -1);printf("ALL reader and writwe have finished operating! \n\n");}// 写者优先代码部分/*************************************************************************************/// 写者优先-写者线程void WP_WriterThread(void *p){DWORD WaitForMutex3;DWORD WaitTime, RunTime;int Number;HANDLE Mutex3;Mutex3 = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "mutex3");Number=((Thread*)(p))->Number;    WaitTime=(DWORD)(((Thread*)(p))->WaitTime * perSecond);    RunTime=(DWORD)(((Thread*)(p))->RunTime * perSecond);    Sleep(WaitTime);printf ("Writer thread %d sents the reading require.\n", Number);// 读者线程从创建m_delay秒后,发送读申请WaitForMutex3 = WaitForSingleObject(Mutex3, -1);// P(Mutex3) 维护writeCount修改// P(h_mutex)writeCount++;if(writeCount == 1)// 临界区有写者线程作业时,阻塞一切读者线程{EnterCriticalSection(&CS_Reader);}ReleaseMutex(Mutex3);// V(Mutex3)EnterCriticalSection(&CS_Writer);// 写写互斥printf("Writer thread %d begin to write the file.\n",Number);Sleep(RunTime);printf("writer thread %d finishing  writing file.\n",Number);LeaveCriticalSection(&CS_Writer);WaitForMutex3 = WaitForSingleObject(Mutex3,-1);// 维护writeCount修改writeCount--;if(writeCount==0)// 当所有写者线程作业完毕,释放读者线程临界区对象{LeaveCriticalSection(&CS_Reader);               }ReleaseMutex(Mutex3);// V(Mutex3)}// 写者优先-读者线程void WP_ReaderThread(void *p){HANDLE Mutex1;Mutex1=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex1");HANDLE Mutex2;Mutex2=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex2");DWORD WaitForMutex1;DWORD WaitForMutex2;DWORD WaitTime;DWORD RunTime;int   Number;Number=((Thread*)(p))->Number;    WaitTime=(DWORD)(((Thread*)(p))->WaitTime * perSecond);    RunTime=(DWORD)(((Thread*)(p))->RunTime * perSecond);    Sleep(WaitTime);printf("Reader thread %d sents the reading require.\n", Number);WaitForMutex1=WaitForSingleObject(Mutex1,-1);    EnterCriticalSection(&CS_Reader);// 写者优先WaitForMutex2=WaitForSingleObject(Mutex2,-1);// P(Mutex1)  维护readCount修改readCount++;if(readCount==1)// 读写互斥{   EnterCriticalSection(&CS_Writer);}ReleaseMutex(Mutex2);LeaveCriticalSection(&CS_Reader);ReleaseMutex(Mutex1);printf("Reader thread %d begin to read the file.\n", Number);Sleep(RunTime);printf("Reader thread %d finished reading file.\n", Number);WaitForMutex2=WaitForSingleObject(Mutex2, -1);readCount--;if(readCount==0){LeaveCriticalSection(&CS_Writer);// 解除读写互斥}ReleaseMutex(Mutex2);}// 写者优先主控程序void WriterPriority(char *file){DWORD n_Thread = 0;DWORD Thread_ID;DWORD WaitForAll;HANDLE Mutex1;Mutex1 = CreateMutex(NULL, FALSE, "mutex1");// 用于实现读者优先    HANDLE Mutex2;    Mutex2=CreateMutex(NULL, FALSE, "mutex2");// 用于维护readCount互斥对象    HANDLE Mutex3;    Mutex3=CreateMutex(NULL, FALSE, "mutex3");// 用于维护writeCount互斥对象Thread thread[max_thread_num];HANDLE h_Thread[max_thread_num];readCount = 0;writeCount = 0;InitializeCriticalSection(&CS_Writer);    InitializeCriticalSection(&CS_Reader);ifstream inFile;inFile.open(file);printf("****************  Writer Priority(写者优先)  ****************\n\n");while(inFile){inFile >> thread[n_Thread].Number;inFile >> thread[n_Thread].Type;inFile >> thread[n_Thread].WaitTime;inFile >> thread[n_Thread++].RunTime;inFile.get();}for(int i=0; i<(int)(n_Thread); i++){if(thread[i].Type == 'R' || thread[i].Type == 'r'){h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_ReaderThread),&thread[i],0,&Thread_ID);}else{h_Thread[i]=CreateThread(NULL,0,                   (LPTHREAD_START_ROUTINE)(WP_WriterThread),   &thread[i],0,&Thread_ID);}}WaitForAll = WaitForMultipleObjects(n_Thread, h_Thread, TRUE, -1);printf("ALL reader and writwe have finished operating! \n\n");}/*************************************************************************************/// Main函数菜单界面int main(){bool OP = true;int Ch;char ReMenu;while(OP){printf("******     Exp1: 读者写者     ***********\n");printf("*                                       *\n");printf("*        1: Reader Priority             *\n");     printf("*        2: Writer Priority             *\n");          printf("*        3: Exit                        *\n");printf("*                                       *\n");printf("*****************************************\n");printf("Input your op choice(1, 2 or 3):  ");scanf("%d", &Ch);system("cls");if(Ch == 1){ReaderPriority("Testdata.dat");printf("Press Any Key To Continue: ");ReMenu=(char)_getch();}else if(Ch == 2){ WriterPriority("Testdata.dat");printf("Press Any Key To Continue: ");ReMenu=(char)_getch();}else if(Ch == 3){OP = false; } system("cls");}return 0;}


 

原创粉丝点击