操作系统读者写者实验报告|读者写着程序源码

 《操作系统原理》课程设计

 课程设计起止时间:2009年

 指导教师: 成绩:

 课程设计成绩评定表

 质量评价指标

 评 价 项 目

 具 体 要 求

 满 分

 得 分

 学习态度

 学习认真,态度端正,遵守纪律,出勤情况良好,能够独立完成设计工作。

 20

 工作量

 按期圆满完成规定的设计任务,工作量饱满,难度适宜。

 20

 设计说明书质量

 设计方案正确、表达清楚;设计思路、方法科学合理;达到课程设计任务书规定的要求;图、表、文字表达准确规范,上交及时。

 30

 答辩陈述和回答问题

 陈述简明扼要,思路清晰,清楚流利,回答问题准确,基本概念清楚,有理有据,有一定深度。

 30

 评定成绩

 指导教师签名

 年 月 日

 一.设计说明(四号,宋体,加粗)

 通过学习操作系统,与之前的语句基础相结合,用C语言来编写读者写着问题。读者写者问题(read—write problem)是一个经典的并发程序设计问题。有两组并发进程:读者和写者,共享一个问题F,要求:(1)允许多个读者可同时对之执行读操作;(2)只允许一个写者往文件中写信息;(3)任一写者在完成写操作之前不允许其他读者或者写者工作;(4)写者执行写操作前,应让已有的写者和读者全部退出。

 二.工作原理(四号,宋体,加粗)

 读者和写者问题是典型是经典的进程同步问题,进程同步任务是使并发的诸进程之间有效的共享资源,相互合作,从而保证程序的可再现性。在读者—写者问题中,允许多个读者同时读一个数据对象,因为读文件不会使数据发生混乱,但绝不允许一个写者进程与其他读者进程或写者进程同时访问该数据对象。文件是诸进程能互斥访问临界资源,读者进程和写者进程,写者进程和写者进程之间的互斥。

 在读者进程中,可以有多个读者在读数据库,在读者进程的计数要互斥,以免发生错误,同时注意当第一个读者进程读时,一定要封锁写者进程。当读者进程逐渐撤离时,也要针对计数变量进行互斥操作,若当前为最后一个读者进程时,读完后,则唤醒写者进程。

 当写者进程在进行写操作时,可以封锁其他读者或写者进程,当写操作完成时,唤醒其他读者或写者进程。

 所以分析了以下4种可能发生的情况:

 第 1 种情况: 读者的优先权比写者高,而且,不用调配。

 所有读者的优先权都比写者的优先权高,而且,不用调配。一个读者需要等待的唯一情况是,一个写者已经占用了文件。一个写者可以取得文件的条件是,没有一个读者处在等待状态或正在读文件。允许读者们结盟,以便能长期占用文件,而禁止写者的写。

 第 2 种情况: 在一个读者已经占有了文件的时候,全体读者的优先权才比写者高。

 在没有任何一个读者在读文件时,读者的优先权和写者的优先权相同。相反,如果有一个读者正在读文件,则其余的各读者都可以读文件,而不管有多少写者处在等待状态。所有读者都有权结盟,以便垄断文件。

 第 3 种情况: 写者的优先权比读者的优先权高。

 在一个写者提出要访问文件时,就必须使其尽可能的得到文件,而且不用调配。也就是说,在出现这一请求时,占据着文件的各进程都被执行完以后,写者可以立即得到文件。因此,在文件已为一写者请求之后到来的那些读者都必须等待,尽管某些读者正在应用文件,也是如此。所有写者可以结盟,以便能长期禁止读者的读。

 第 4 种情况: 所有写者的和所有读者有相同的优先权高,哪一类都不会有比另一类更高的优先权。

 如果一个读者正在应用文件,则在一个写者请求文件之前到来的全体读者,都能读文件,而之后到来的读者或写者,则要等待,不必区分他们属于哪一类进程。如果一个写者正在写文件,则所有新到来的请求都必须等待。在这一写者写完之后,它就要唤醒处在等待队列中的排在第一个位置的进程。如果此时有几个读者连续排在等待队列中的最前面各位置上,则它们可以同时读文件。

 三.详细设计(四号,宋体,加粗)

 了解读者—写者问题的基本思想结构,我们需要在Linux和windows环境下运行,选择语言在这里我选的是C语言。

 读者-写者的读写限制(包括读者优先和写者优先)

 1)写-写互斥,即不能有两个写者同时进行写操作

 2)读-写互斥,即不能同时有一个读者在读,同时却有一个写者在写

 3)读读允许,即可以有2个以上的读者同时读

 读者优先的限制:

  如果一个读者申请读操作时,已经有一个读者在读,则该读者可以直接读

 写者优先的限制:

  如果一个读者申请读操作时,有写者在等待访问共享资源时,则该读者要等到没有写者处于等的状态时才能开始读操作

 测试数据的格式

  在文件 thread.dat 中,

 1 r 3 5

 2 w 4 5

 其中第一个代表线程的ID,第二个字段代表是读操作还是写操作,第三个字段代表操作的开始时间,第4个字段是持续时间。

 分析:

  将所有的读者和所有的写者分别放进两个等待队列中,当读允许时就让读者队列释放一个或多个读者,当写允许时,释放第一个写者操作。

 读者优先:

  如果没有写者正在操作,则读者不需要等待,用一个整型变量readcount记录当前的读者数目,用于确定是否释放写者线程,(当readcout=0 时,说明所有的读者都已经读完,释放一个写者线程),每个 读者开始读之前都要修改readcount,为了互斥的实现对readcount 的修改,需要一个互斥对象Mutex来实现互斥。

  另外,为了实现写-写互斥,需要一个临界区对象 write,当写者发出写的请求时,必须先得到临界区对象的所有权。通过这种方法,可以实现读写互斥,当readcount=1 时,(即第一个读者的到来时,),读者线程也必须申请临界区对象的所有权.

  当读者拥有临界区的所有权,写者都阻塞在临界区对象write上。当写者拥有临界区对象所有权时,第一个判断完readcount==1 后,其余的读者由于等待对readcount的判断,阻塞在Mutex上!

 写者优先:

 写者优先和读者优先有相同之处,不同的地方在:一旦有一个写者到来时,应该尽快让写者进行写,如果有一个写者在等待,则新到的读者操作不能读操作,为此添加一个整型变量writecount,记录写者的数目,当writecount=0时才可以释放读者进行读操作!

  为了实现对全局变量writecount的互斥访问,设置了一个互斥对象Mutex3。

  为了实现写者优先,设置一个临界区对象read,当有写者在写或等待时,读者必须阻塞在临界区对象read上。

  读者除了要一个全局变量readcount实现操作上的互斥外,还需要一个互斥对象对阻塞在read这一个过程实现互斥,这两个互斥对象分别为mutex1和mutex2。

 源代码:

 #include "windows.h"

 #include <conio.h>

 #include <stdlib.h>

 #include <fstream.h>

 #include <io.h>

 #include <string.h>

 #include <stdio.h>

 ?

 #define READER 'R' //读者

 #define WRITER 'W' //写者

 #define INTE_PER_SEC 1000 //每秒时钟中断的数目

 #define MAX_THREAD_NUM 64 //最大线程数

 #define MAX_FILE_NUM 32 //最大文件数目数

 #define MAX_STR_LEN 32 //字符串的长度

 ?

 int readcount=0; //读者数目

 int writecount=0; //写者数目

 CRITICAL_SECTION RP_Write; //临界资源

 CRITICAL_SECTION cs_Write;

 CRITICAL_SECTION cs_Read;

 struct ThreadInfo

 {

  int serial; //线程序号

  char entity; //线程类别(判断是读者还是写者线程)

  double delay; //线程延迟时间

  double persist; //线程读写操作时间

 };

 ?

 ?

 ///////////////////////////////////////////////////////////////////////////

 // 读者优先读者线程

 //P:读者线程信息

 ?

 ?

 ?

 void RP_ReaderThread(void *p)

 {

 ?

  //互斥变量

  HANDLE h_Mutex;

  h_Mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readcount");

 

 

  DWORD wait_for_mutex; //等待互斥变量所有权

  DWORD m_delay; //延迟时间

  DWORD m_persist; //读文件持续时间

  int m_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("Reader thread %d sents the reading require.\n",m_serial);

 

 ?

 ?

  //等待互斥信号,保证对ReadCount 的访问,修改互斥

  wait_for_mutex=WaitForSingleObject(h_Mutex,-1);

  //读者数目增加

  readcount++;

  if(readcount==1)

  {

  //第一个读者,等待资源

  EnterCriticalSection(&RP_Write);

  }

  ReleaseMutex(h_Mutex); //释放互斥信号

 ?

 ?

 ?

  //读文件

  printf("Reader thread %d begins to read file.\n",m_serial);

 Sleep(m_persist);

 ?

  //退出线程

  printf("Reader thread %d finished reading file.\n",m_serial);

  //等待互斥信号,保证对ReadCount的访问,修改互斥

  wait_for_mutex=WaitForSingleObject(h_Mutex,-1);

  //读者数目减少

  readcount--;

  if(readcount==0)

  {

  //如果所有的读者读完,唤醒写者

  LeaveCriticalSection(&RP_Write);

  }

  ReleaseMutex(h_Mutex); //释放互斥信号

 }

 ?

 ?

 //////////////////////////////////////////////////////////////

 //P:写者线程信息

 ?

 ?

 void RP_WriterThread(void *p)

 {

  DWORD m_delay; //延迟时间

  DWORD m_persist; //写文件持续时间

  int m_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("Write thread %d sents the writing require.\n",m_serial);

 ?

 ?

  //等待资源

  EnterCriticalSection(&RP_Write);

 ?

  //写文件

  printf("Writer thread %d begins to write to the file.\n",m_serial);

  Sleep(m_persist);

 ?

 ?

  //退出线程

 printf("Write thread %d finished writing to the file.\n",m_serial);

  //释放资源

  LeaveCriticalSection(&RP_Write);

 }

 ?

 ?

 //////////////////////////////////////////////////////////////

 //读者优先处理函数

 //file:文件名

 ?

 ?

 void ReaderPriority(char *file)

 {

  DWORD n_thread=0; //线程数目

  DWORD thread_ID; //线程ID

  DWORD wait_for_all; //等待所有线程结束

 ?

 ?

 ?

 ?

  //互斥对象

  HANDLE h_Mutex;

  h_Mutex=CreateMutex(NULL,FALSE,"mutex_for_readcount");

 ?

 ?

  //线程对象的数组

  HANDLE h_Thread[MAX_THREAD_NUM];

  ThreadInfo thread_info[MAX_THREAD_NUM];

 ?

  readcount=0; //初始化readcount

  InitializeCriticalSection(&RP_Write); //初始化临界区

  ifstream inFile;

  inFile.open (file);

  printf("Reader Priority:\n\n");

  while(inFile)

  {

  //读入每一个读者,写者的信息

  inFile>>thread_info[n_thread].serial;

  inFile>>thread_info[n_thread].entity;

  inFile>>thread_info[n_thread].delay;

  inFile>>thread_info[n_thread++].persist;

  inFile.get();

  }

  for(int i=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_WriterThread),&thread_info[i],0,&thread_ID);

  }

 ?

  }

  //等待所有的线程结束

  wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1);

  printf("All reader and writer have finished operating.\n");

 }

 ?

 ////////////////////////////////////////////////////////

 //写者优先读者线程

 //P:读者线程信息

 ?

 ?

 void WP_ReaderThread(void *p)

 {

 ?

  //互斥变量

  HANDLE h_Mutex1;

  h_Mutex1=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex1");

  HANDLE h_Mutex2;

  h_Mutex2=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex2");

 ?

 ?

  DWORD wait_for_mutex1; //等待互斥变量所有权

  DWORD wait_for_mutex2;

  DWORD m_delay; //延迟时间

  DWORD m_persist; //读文件持续时间

  int m_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("Reader thread %d sents the reading require.\n",m_serial);

  wait_for_mutex1=WaitForSingleObject(h_Mutex1,-1);

 ?

  //读者进去临界区

  EnterCriticalSection(&cs_Read);

 ?

  //阻塞互斥对象Mutex2,保证对readCount的访问和修改互斥

  wait_for_mutex2=WaitForSingleObject(h_Mutex2,-1);

  //修改读者的数目

  readcount++;

  if(readcount==1)

  {

  // 如果是第1个读者,等待写者写完

  EnterCriticalSection(&cs_Write);

  }

  ReleaseMutex(h_Mutex2);// 释放互斥信号 Mutex2

  //让其他读者进去临界区

  LeaveCriticalSection(&cs_Read);

  ReleaseMutex(h_Mutex1);

  //读文件

  printf("Reader thread %d begins to read file.\n",m_serial);

  Sleep(m_persist);

 ?

 ?

  //退出线程

  printf("Reader thread %d finished reading file.\n",m_serial);

  //阻塞互斥对象Mutex2,保证对readcount的访问,修改互斥

  wait_for_mutex2=WaitForSingleObject(h_Mutex2,-1);

  readcount--;

  if(readcount==0)

  {

  //最后一个读者,唤醒写者

  LeaveCriticalSection(&cs_Write);

  }

  ReleaseMutex(h_Mutex2); //释放互斥信号

 }

 ?

 ?

 ///////////////////////////////////////////

 //写者优先写者线程

 //P:写者线程信息

 ?

 ?

 void WP_WriterThread(void *p)

 {

  DWORD wait_for_mutex3; //互斥变量

  DWORD m_delay; //延迟时间

  DWORD m_persist; //读文件持续时间

  int m_serial; //线程序号

 ?

  HANDLE h_Mutex3;

  h_Mutex3=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex3");

 ?

 ?

  //从参数中获得信息

  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("Writer thread %d sents the reading require.\n",m_serial);

  wait_for_mutex3=WaitForSingleObject(h_Mutex3,-1);

  writecount++; //修改写者数目

  if(writecount==1)

  {

  EnterCriticalSection(&cs_Read);

  }

  ReleaseMutex(h_Mutex3);

  EnterCriticalSection(&cs_Write);

  printf("Writer thread %d begins to write to the file.\n",m_serial);

  Sleep(m_persist);

 ?

 ?

  printf("Writer thread %d finished writing to the file.\n",m_serial);

  LeaveCriticalSection(&cs_Write);

 ?

  wait_for_mutex3=WaitForSingleObject(h_Mutex3,-1);

  writecount--;

  if(writecount==0)

  {

  LeaveCriticalSection(&cs_Read);

  }

 ReleaseMutex(h_Mutex3);

 }

 /////////////////////////////////////////////

 //写者优先处理函数

 // file:文件名

 ?

 void WriterPriority(char * file)

 {

  DWORD n_thread=0;

  DWORD thread_ID;

  DWORD wait_for_all;

 ?

 ?

  HANDLE h_Mutex1;

  h_Mutex1=CreateMutex(NULL,FALSE,"mutex1");

  HANDLE h_Mutex2;

  h_Mutex2=CreateMutex(NULL,FALSE,"mutex2");

  HANDLE h_Mutex3;

  h_Mutex3=CreateMutex(NULL,FALSE,"mutex3");

  HANDLE h_Thread[MAX_THREAD_NUM];

  ThreadInfo thread_info[MAX_THREAD_NUM];

 ?

  readcount=0;

  writecount=0;

  InitializeCriticalSection(&cs_Write);

  InitializeCriticalSection(&cs_Read);

 ?

  ifstream inFile;

  inFile.open (file);

  printf("Writer priority:\n\n");

  while(inFile)

  {

  inFile>>thread_info[n_thread].serial;

  inFile>>thread_info[n_thread].entity;

  inFile>>thread_info[n_thread].delay;

  inFile>>thread_info[n_thread++].persist;

  inFile.get();

  }

  for(int i=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)(WP_ReaderThread),&thread_info[i],0,&thread_ID);

  }

  else

  {

  //创建写线程

  h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_WriterThread),&thread_info[i],0,&thread_ID);

  }

 ?

  }

  //等待所有的线程结束

  wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1);

  printf("All reader and writer have finished operating.\n");

 }

 ?

 /////////////////////////////////////////////////////

 //主函数

 int main(int argc,char *argv[])

 {

  char ch;

  while(true)

  {

  printf("*************************************\n");

  printf(" 1.Reader Priority\n");

  printf(" 2.Writer Priority\n");

  printf(" 3.Exit to Windows\n");

  printf("*************************************\n");

  printf("Enter your choice(1,2,3): ");

  do{

  ch=(char)_getch();

  }while(ch!='1'&&ch!='2'&&ch!='3');

  system("cls");

  if(ch=='3')

  return 0;

  else if(ch=='1')

  ReaderPriority("thread.dat");

  else

  WriterPriority("thread.dat");

  printf("\nPress Any Key to Coutinue:");

  _getch();

  system("cls");

 }

  return 0;

 }

 图示例(下图)

 四.运行结果(四号,宋体,加粗)

  进入主界面,可以看到定义了主要标志:1,reader priority 2.writer priority 3.exit to windows. 当你键入1 的时候,可以看到第二个视图,证明进入读者占有资源临界区。这个时候,因为读者可以有很多,但是与写者互斥,之后键入任意键,退到临界面。之后键入2,可以看到第三个视图,写者进入临界区,按任意键退到临街面。当键入数字3的时候退出程序。

 截图示例(下图)

 五.分析结果(四号,宋体,加粗)

 结果分析:

 Writer thread 868993470 sents the writing require.

 当写者发出申请时,有读者在读文件,所以写者阻塞。

 Reader thread 858993460 sents the reading require.

 Reader thread 3 858993460egins to read file.

 读者发出申请后,立刻得到满足。

 之后的读者发送请求时已没有读者线程,所以他们被安排在写者线程完成写操作后读文件。

 参考文献(四号,宋体,加粗)

 (1)汤子赢等. 计算机操作系统. 西安电子科技大学出版社,2005

 (2)李善平.? 操作系统学习指导和考试指导 . 浙江大学出版社,2004

 (3)崔武子、赵重敏等.? C程序设计教程? .清华大学出版社,2001

推荐访问:操作系统 读者 实验 报告