多线程读者写者优先实验报告.docx
- 文档编号:10082644
- 上传时间:2023-02-08
- 格式:DOCX
- 页数:9
- 大小:47.12KB
多线程读者写者优先实验报告.docx
《多线程读者写者优先实验报告.docx》由会员分享,可在线阅读,更多相关《多线程读者写者优先实验报告.docx(9页珍藏版)》请在冰豆网上搜索。
多线程读者写者优先实验报告
多线程读者写者优先实验报告
1、熟悉多线程编程
2、熟悉使用信号量机制解决进程同步问题
二、
在Windows2000环境下,创建一个包含n个线程的控制台进程。
用这n个线程来表示n个读者或写者。
每个线程按相应测试数据文件的要求,进行读写操作。
请用信号量机制
分别实现读者优先和写者优先的读者-写者问题。
读者优先:
如果一个读者申请进行读操作时已有另一读者正在进行读操作,则该读者可直接
开始读操作。
写者优先:
如果一个读者申请进行读操作时已有另一写者在等待访问共享资源,则该读者必
须等到没有写者处于等待状态后才能开始读操作。
1、为每个学生提供一台具有WINDOWS2000/NT/XP操作系统的计算机;
2、实验机器要求安装VisualC++6.0编程平台;
3、实验要求一人一机。
1、要求在每个线程创建、发出读写操作申请、开始读写操作和结束读写操作时分别显
示一行提示信息,以确信所有处理都遵守相应的读写操作限制。
2、测试数据文件格式:
测试数据文件包括n行测试数据,分别描述创建的n个线程是读者还是写者,以及读写操作的开始时间和持续时间。
每行测试数据包括四个字段,各字
段间用空格分隔。
第一字段为一个正整数,表示线程序号。
第二字段表示相应线程角色,R表示读者是,W表示写者。
第三字段为一个正数,表示读写操作的开始时间。
线程创建后,
延时相应时间(单位为秒)后发出对共享资源的读写申请。
第四字段为一个正数,表示读写
操作的持续时间。
当线程读写申请成功后,开始对共享资源的读写操作,该操作持续相应时
间后结束,并释放共享资源。
下面是一个测试数据文件的例子:
1R35
2W45
3R52
4R65
3、若有可能请在图形方式下,将读、写者线程执行情况用图形动画显示
#include
#include
#include
#include
#include
#include
#include
#defineREADER'R'//读者
#defineWRITER'W'//写者
#defineINTE_PER_SEC1000//定义每秒中断的次数#defineMAX_THREAD_NUM64//定义最大线程数#defineMAX_FILE_NUM32//定义最大文件数目#defineMAX_STR_LEN32//
intreadcount=0;//
intwritecount=0;//
CRITICAL_SECTIONRP_Write;//临界资源CRITICAL_SECTIONRP_Write_Reader;//临界资源intchoice=0;
structThreadInfo
{
intserial;//线程序号
charentity;//线程类别
doubledelay;//线程延迟时间,即开始时间
doublepersist;//线程读写操作时间
};
voidRP_ReaderThread(void*p)
{
HANDLEh_Mutex;
//HANDLEs_Mutex;
//s_Mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readORwriter");
h_Mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readcount");
DWORDwait_for_mutex;
DWORDm_delay;
DWORDm_persist;
intm_serial;
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("读线程%d发出请求.\n",m_serial);
cout<<"读线程"< if(choice==1) { EnterCriticalSection(&RP_Write_Reader); } wait_for_mutex=WaitForSingleObject(h_Mutex,-1); //wait_for_mutex=WaitForSingleObject(s_Mutex,-1); if(readcount==0) { EnterCriticalSection(&RP_Write); } readcount++; ReleaseMutex(h_Mutex); if(choice==1) { LeaveCriticalSection(&RP_Write_Reader); } //ReleaseMutex(s_Mutex); //printf("读进程%d开始读文件.\n",m_serial); cout<<"读线程"< Sleep(m_persist); //printf("读进程%d结束读文件.\n",m_serial); cout<<"读线程"< wait_for_mutex=WaitForSingleObject(h_Mutex,-1); readcount--; if(readcount==0) { LeaveCriticalSection(&RP_Write); } ReleaseMutex(h_Mutex); } voidRP_WriteThread(void*p) { DWORDm_delay; DWORDm_persist; intm_serial; //HANDLEs_Mutex; //DWORDwait_for_mutex; //s_Mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readORwriter"); 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); cout<<"写进程"< if(choice==1) { EnterCriticalSection(&RP_Write_Reader); } //wait_for_mutex=WaitForSingleObject(s_Mutex,-1); EnterCriticalSection(&RP_Write); cout<<"写进程"< Sleep(m_persist); cout<<"写进程"< LeaveCriticalSection(&RP_Write); if(choice==1) { LeaveCriticalSection(&RP_Write_Reader); } //ReleaseMutex(s_Mutex); } voidReadPriority(char*file) { DWORDn_thread=0; DWORDthread_ID; DWORDwait_for_all; HANDLEh_Mutex; HANDLEs_MUTEX; h_Mutex=CreateMutex(NULL,FALSE,"mutex_for_readcount"); s_MUTEX=CreateMutex(NULL,FALSE,"mutex_for_readerORwriter"); HANDLEh_Thread[MAX_THREAD_NUM]; ThreadInfothread_info[MAX_THREAD_NUM]; readcount=0; InitializeCriticalSection(&RP_Write); InitializeCriticalSection(&RP_Write_Reader); ifstreaminFile; inFile.open(file); while(inFile) { inFile>>thread_info[n_thread].serial;cout< inFile>>thread_info[n_thread].entity;cout< inFile>>thread_info[n_thread].delay;cout< inFile>>thread_info[n_thread].persist;cout< inFile.get(); cout< } inFile.close(); for(inti=0;i<(int)(n_thread);i++) { if(thread_info[i].entity==READER||thread_info[i].entity=='R') { h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_ReaderThread),&thread_info[i],0,&thread_ID); } else { h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_WriteThread),&thread_info[i],0,&thread_ID); } } wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1); cout<<"所有线程运行完毕"< } intmain() { intn; cout<<"输入你的选择: \n1: 读者优先\n2: 读者优先\n3: 退出\n"< cin>>n; while(n! =3) { switch(n) { case1: choice=0;ReadPriority("1.txt");break; case2: choice=1;ReadPriority("1.txt");break; default: break; } cout<<"输入你的选择: \n1: 读者优先\n2: 写者优先\n3: 退出\n"< cin>>n; } system("PAUSE"); return0; } 首先选择读者优先,第一次选择,把读入的文件信息输出 选择写者优先: 由于此实验用到API函数,自己不知道,所以上网搜索一些关于线程的API函数,又通过 查阅一些参考书,逐渐掌握了多线程编程及其函数应用。 通过此实验掌握了使用信号量机制解决进程同步问题的方法。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 多线程 读者 优先 实验 报告