当前位置:
文档之家› (完整word版)读者写者问题实验报告
(完整word版)读者写者问题实验报告
当读者拥有临界区所有权时,写者阻塞在临界区对象write上.当写者拥有临界区所有权时,第一个读者判断完”read_count==1”后阻塞在write上,其余的读者由于等待对read_count的判断,阻塞在mutex上.
b)写者优先
写者优先与读者优先类似.不同之处在于一旦一个写者到来,它应该尽快对文件进行写操作,如果有一个写者在等待,则新到来的读者不允许进行读操作.为此应当添加一个整形变量write_count,用于记录正在等待的写者的数目,当write_count=0时,才可以释放等待的读者线程队列.
while(g_CurrentTime <= pPerson->m_nStartTime + pPerson->m_nWorkTime)
{}
printf("Reader %d is Exit...\n",pPerson->m_nID);
printf("\n\n************************************************\n");
WaitForSingleObject(g_hReadSemaphore,INFINITE); if(g_NumOfReading ==0)
{
WaitForSingleObject(g_hWriteSemaphore,INFINITE); }
g_NumOfReading++;
ReleaseSemaphore(g_hReadSemaphore,1,NULL);
二、实验设备(环境)
实验设备:WINDOWS 2000环境、VC平台、WIN 32 API
三、实验内容与步骤
实验内容与步骤:可以将所有读者和写者分别存于一个读者等待队列和一个写者等待队列,每当读允许时,就从读者队列中释放一个或多个读者线程进行读操作;每当写允许时,就从写者队列中释放一个写者进行写操作.
g_CurrentTime = 0;
while(true)
{
g_CurrentTime++;
Sleep(300); // 300 ms
printf("CurrentTime = %d\n",g_CurrentTime);
if(finished) return 0;
system ("pause");
}// return 0;
WaitForSingleObject(g_hReadSemaphore,INFINITE);
g_NumOfReading--;
if(g_NumOfReading == 0)
{ReleaseSemaphore(g_hWriteSemaphore,1,NULL);//此时没有读者,可以写
}
ReleaseSemaphore(g_hReadSemaphore,1,NULL);
《操作系统》实验报告
实验序号:操作系统实验(二)实验项目名称:读者写者问题
学 号
姓 名
队别
实验地点
指导教员
实验时间
一、实验目的及要求
实验目的:综合运用多进程并发控制,进程同步互斥,信号量机制等原理解决问题。
实验要求:本题目安排16学时,要求党员根据题目要求,写出算法,在VC平台上进行程序设计和调试,在运行成功的基础上,整理出源程序和运行结果.写出设计报告。
}
void CreatePersonList(int *pPersonLists)
{
int i=0;
int *pList = pPersonLists;
bool Ret;
while(pList[0] != END)
{
switch(pList[1])
{
case R:
Ret = CreateReader(pList[2],pList[3],pList[0]);//351,w452,523,654
WaitForSingleObject(g_hWriteSemaphore,INFINITE);
// modify the writer's real start time
pPerson->m_nStartTime = g_CurrentTime;
printf("Writer %d is Writting the Shared Buffer...\n",pPerson->m_nID);
//g_NumOfWriteRequest--;
ReleaseSemaphore(g_hWriteSemaphore,1,NULL);
if(pPerson->m_nID == 4) finished = true;//所有的读写完成
ExitThread(0);
return 0;
}
bool CreateReader(int StartTime,int WorkTime,int ID)
int m_nWorkTime;//运行时间
int m_nID;//进程号
}Person;
Person g_Persons[MAX_PERSON];
int g_NumPerson = 0;
long g_CurrentTime= 0;//基本时间片数
int g_PersonLists[] = {//进程队列
break; case W:
Ret = CreateWriter(pList[2],pList[3],pList[0]);
break;
}
if(!Ret)
printf("Create Person %d is wrong\n",pList[0]);
pList += 4; // move to next person list
g_hWriteSemaphore = CreateSemaphore(NULL,1,100,NULL); //创建信号灯,当前可用的资源数为,最大为
CreatePersonList(g_PersonLists); // Create All the reader and writers
printf("Created all the reader and writer\n...\n");
}
}
DWORD WINAPI ReaderProc(LPVOID lpParam)//读过程
{
Person *pPerson = (Person*)lpParam;
// wait for the start time
while(g_CurrentTime != pPerson->m_nStartTime)
while(g_CurrentTime != pPerson->m_nStartTime)
{}
printf("Writer %d is Requesting ...\n",pPerson->m_nID);
printf("\n\n************************************************\n");
if(pPerson->m_nID == 4) finished = true; //所有的读写完成
ExitThread(0);
return 0;
}
DWORD WINAPI WriterProc(LPVOID lpParam)
{
Person *pPerson = (Person*)lpParam;
// wait for the start time
while(g_CurrentTime <= pPerson->m_nStartTime + pPerson->m_nWorkTime)
{}
printf("Writer %d is Exit...\n",pPerson->m_nID);
printf("\n\n************************************************\n");
{
DWORD dwThreadID;
if(g_NumPerson >= MAX_PERSON)
return false;
Person *pPerson = &g_Persons[g_NumPerson];
pPerson->m_nID = ID;
pPerson->m_nStartTime = StartTime;
四、实验结果与数据处理
源代码
#include <windows.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX_PERSON 100
#define READER 0 //读者
pPerson->m_nStartTime =;Reader %d is Reading the Shared Buffer...\n",pPerson->m_nID);
printf("\n\n************************************************\n");
为了对全局变量write_count实现互斥,必须增加一个对象mutex3.
为了实现写者优先,应当添加一个临界区对象read,当有写者在写文件或等待时,读者必须阻塞在read上.
读者线程除了要对全局变量read_count实现操作上的互斥外,还必须有一个互斥对象对阻塞read这一过程实现互斥.这两个互斥对象分别命名为mutex1和mutex2。