操作系统课程设计报告读者写者问题.doc
- 文档编号:4857218
- 上传时间:2023-05-07
- 格式:DOC
- 页数:10
- 大小:70KB
操作系统课程设计报告读者写者问题.doc
《操作系统课程设计报告读者写者问题.doc》由会员分享,可在线阅读,更多相关《操作系统课程设计报告读者写者问题.doc(10页珍藏版)》请在冰点文库上搜索。
操作系统课程设计
课题:
读者写者问题
姓名:
赫前进
班级:
1020552
学号102055211
指导教师:
叶瑶
提交时间:
2012/12/30
(一)实验目的
1.进一步理解“临界资源”的概念;
2.把握在多个进程并发执行过程中对临界资源访问时的必要约束条件;
3.理解操作系统原理中“互斥”和“同步”的涵义。
(二)实验内容
利用程序设计语言编程,模拟并发执行进程的同步与互斥(要求:
进程数目不少于3个)。
(三)、程序分析
读者写者问题的定义如下:
有一个许多进程共享的数据区,这个数据区可以是一个文件或者主存的一块空间;有一些只读取这个数据区的进程(Reader)和一些只往数据区写数据的进程(Writer),此外还需要满足以下条件:
(1)任意多个读进程可以同时读这个文件;
(2)一次只有一个写进程可以往文件中写;
(3)如果一个写进程正在进行操作,禁止任何读进程度文件。
实验要求用信号量来实现读者写者问题的调度算法。
实验提供了signal类,该类通过P()、V()两个方法实现了P、V原语的功能。
实验的任务是修改Creat_Writer()添加写者进程,Creat_Reader()创建读者进程。
Reader_goon()读者进程运行函数。
读优先:
要求指一个读者试图进行读操作时,如果这时正有其他读者在进行操作,他可直接开始读操作,而不需要等待。
读者优先的附加限制:
如果一个读者申请进行读操作时已有另一读者正在进行读操作,则该读者可直接开始读操作。
写优先:
一个读者试图进行读操作时,如果有其他写者在等待进行写操作或正在进行写操作,他要等待该写者完成写操作后才开始读操作。
写者优先的附加限制:
如果一个读者申请进行读操作时已有另一写者在等待访问共享资源,则该读者必须等到没有写者处于等待状态后才能开始读操作。
在Windows7环境下,创建一个控制台进程,此进程包含n个线程。
用这n个线程来表示n个读者或写者。
每个线程按相应测试数据文件(格式见下)的要求进行读写操作。
用信号量机制分别实现读者优先和写者优先的读者/写者问题。
运行结果显示要求:
要求在每个线程创建、发出读写操作申请、开始读写操作和结束读写操作时分别显示一行提示信息,以确定所有处理都遵守相应的读写操作限制。
测试数据文件包括n行测试数据,分别描述创建的n个线程是读者还是写者,以及读写操作的开始时间和持续时间。
每行测试数据包括4个字段,各个字段间用空格分隔。
Ø 第一个字段为一个正整数,表示线程序号
Ø 第二个字段表示相应线程角色,R表示读者,W表示写者
Ø 第三个字段为一个正数,表示读/写操作的开始时间:
线程创建后,延迟相应时间(单位为秒)后发出对共享资源的读/写请求
Ø 第四个字段为一正数,表示读/写操作的持续时间:
线程读写请求成功后,开始对共享资源的读/写操作,该操作持续相应时间后结束,并释放共享资源
例如:
1R35
2W45
3R52
4R65
5W5.13
读者写者问题是操作系统中经典的互斥问题:
一块数据被多个读者和写者的访问,需要考虑读写互斥、写写互斥(可以同时由多个读者读取)。
具体的又可以分为读者优先和写者优先两类。
读者优先算法:
当新的读者到来的时候,若当前正有读者在进行读操作,则该读者无需等待前面的写操作完成,直接进行读操作。
设置两个互斥信号量:
rwmutex用于写者与其他读者/写者互斥的访问共享数据
rmutex用于读者互斥的访问读者计数器readcount
varrwmutex,rmutex:
semaphore:
=1,1;
intreadcount=0;
cobegin
readeribegin//i=1,2,….
P(rmutex);
Readcount++;
If(readcount==1)P(rwmutex);
V(rmutex);
读数据;
P(rmutex);
Readcount--;
If(readcount==0)V(rwmutex);
V(rmutex);
End
Writerjbegin//j=1,2,….
P(rwmutex);
写更新;
V(rwmutex);
End
Coend
写者优先:
条件:
1)多个读者可以同时进行读
2)写者必须互斥(只允许一个写者写,也不能读者写者同时进行)
3)写者优先于读者(一旦有写者,则后续读者必须等待,唤醒时优先考虑写者)
设置三个互斥信号量:
rwmutex用于写者与其他读者/写者互斥的访问共享数据
rmutex用于读者互斥的访问读者计数器readcount
nrmutex用于写者等待已进入读者退出,所有读者退出前互斥写操作
varrwmutex,rmutex,nrmutex:
semaphore:
=1,1,1;
intreadcount=0;
cobegin
readeribegin//i=1,2,….
P(rwmutex);
P(rmutex);
Readcount++;
If(readcount==1)P(nrmutex);//有读者进入,互斥写操作
V(rmutex);
V(rwmutex);//及时释放读写互斥信号量,允许其它读、写进程申请资源
读数据;
P(rmutex);
Readcount--;
If(readcount==0)V(nrmutex);//所有读者退出,允许写更新
V(rmutex);
End
Writerjbegin//j=1,2,….
P(rwmutex);//互斥后续其它读者、写者
P(nrmutex);//如有读者正在读,等待所有读者读完
写更新;
V(nrmutex); //允许后续新的第一个读者进入后互斥写操作
V(rwmutex);//允许后续新读者及其它写者
End
Coend
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*---------函数声明---------*/
voidCreat_Writer();//添加一个写者
voidDel_Writer();//删除一个写者
voidCreat_Reader();//添加一个读者
voidReader_goon();//读者进程运行函数
voidR_Wakeup();//唤醒等待读者
voidDel_Reader();//删除一个读者
voidShow();//显示运行状态
/*===============classsignal===============*/
classsignal//信号量对象.
{
private:
intvalue;
intqueue;//用int型数据模拟等待队列.
public:
signal();
signal(intn);
intP();//检查临界资源
intV();//释放临界资源
intGet_Value();
intGet_Queue();
};
////////////////////////////////////////////////////////////////////
#include
#include
#include
#include
usingnamespacestd;
constintMaxThread=20;
structThreadInfo
{
intnum;
chartype;
doublestart;
doubletime;
}thread_info[MaxThread];
HANDLEhX;
HANDLEhWsem;
HANDLEthread[MaxThread];
intreadcount;
doubletotaltime;
voidWRITEUNIT(intiProcess)
{
printf("Thread%dbeginstowrite.\n",iProcess);
Sleep((DWORD)(thread_info[iProcess-1].time*1000));
printf("Endofthread%dforwriting.\n",iProcess);
}
voidREADUNIT(intiProcess)
{
printf("Thread%dbeginstoread.\n",iProcess);
Sleep((DWORD)(thread_info[iProcess-1].time*1000));
printf("Endofthread%dforreading.\n",iProcess);
}
DWORDWINAPIreader(LPVOIDlpVoid)
{
intiProcess=*(int*)lpVoid;
Sleep((DWORD)(thread_info[iProcess-1].start*1000));
DWORDwait_for=WaitForSingleObject(hX,INFINITE);
printf("Thread%drequresreading.\n",iProcess);
readcount++;
if(readcount==1)WaitForSingleObject(hWsem,INFINITE);
ReleaseMutex(hX);
READUNIT(iProcess);
wait_for=WaitForSingleObject(hX,INFINITE);
readcount--;
if(readcount==0)
ReleaseSemaphore(hWsem,1,0);
ReleaseMutex(hX);
returniProcess;
}
DWORDWINAPIwriter(LPVOIDlpVoid)
{
intiProcess=*(int*)lpVoid;
Sleep((DWORD)(thread_info[iProcess-1].start*1000));
printf("Thread%drequreswriting.\n",iProcess);
DWORDwait_for=WaitForSingleObject(hWsem,INFINITE);
WRITEUNIT(iProcess);
ReleaseSemaphore(hWsem,1,0);
returniProcess;
}
intmain()
{
intthreadNum;
intthreadcount;
ifstreamfile;
hX=CreateMutex(NULL,FALSE,NULL);
hWsem=CreateSemaphore(NULL,1,1,NULL);
//?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
readcount=0;
threadcount=0;
totaltime=0;
file.open("thread.dat",ios:
:
in);
if(file==0)
{
printf("FileOpenError.\n");
return0;
}
while(file>>threadNum)
{
thread_info[threadNum-1].num=threadNum;
file>>thread_info[threadNum-1].type;
file>>thread_info[threadNum-1].start;
file>>thread_info[threadNum-1].time;
totaltime+=thread_info[threadNum-1].time;
switch(thread_info[threadNum-1].type)
{
case'W':
printf("CreatingThread%dforwriting.\n",thread_info[threadNum-1].num);
thread[threadNum-1]=CreateThread(NULL,0,writer,&thread_info[threadNum-1].num,0,0);
break;
case'R':
printf("CreatingThread%dforreading.\n",thread_info[threadNum-1].num);
thread[threadNum-1]=CreateThread(NULL,0,reader,&thread_info[threadNum-1].num,0,0);
break;
}
threadcount++;
}
file.close();
Sleep((DWORD)(totaltime*1000));
return1;
}
//////////////////////////////////////////////////////////////////////////////////
semaphorefmutex=1,rdcntmutex=1;
//fmutex-->accesstofile;rdcntmutex-->accesstoreadcount
intreadcount=0;
voidreader()
{
while
(1)
{
P(rdcntmutex);
if(readcount==0)
P(fmutex);
readcount=readcount+1;
V(rdcntmutex);
//Doreadoperation
P(rdcntmutex);
readcount=readcount-1;
if(readcount==0)
V(fmutex);
V(rdcntmutex);
}
}
voidwriter()
{
while
(1)
{
P(fmutex);
//Dowriteoperation
V(fmutex);
}
}
///////////////////////////////////////////////////////////////////////////////////////
semaphorefmutex=1,rdcntmutex=1,wtcntmutex=1,queue=1;
//fmutex-->accesstofile;rdcntmutex-->accesstoreadcount
//wtcntmutex-->accesstowritecount
intreadcount=0,writecount=0;
voidreader()
{
while
(1)
{
P(queue);//申请队列信号
P(rdcntmutex);//修改readcount,互斥
if(readcount==0)
P(fmutex);//accesstofile互斥
readcount=readcount+1;
V(rdcntmutex);//释放
V(queue);//释放
//Doreadoperation
P(rdcntmutex);
readcount=readcount-1;
if(readcount==0)
V(fmutex);
V(rdcntmutex);
}
}
voidwriter()
{
while
(1)
{
P(wtcntmutex);
if(writecount==0)
P(queue);
writecount=writecount+1;
V(wtcntmutex);
P(fmutex);
//Dowriteoperation
V(fmutex);
P(wtcntmutex);
writecount=writecount-1;
if(writecount==0)
V(queue);
V(wtcntmutex);
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 操作系统 课程设计 报告 读者 问题