读者写者问题实现操作系统课程设计 第4页

读者写者问题源代码实现操作系统课程设计
#include <windows.h>
#include <conio.h>
#include "fstream.h"

int readcount=0;          //读者数目
int writecount=0;         //写者数目

CRITICAL_SECTION RP_Write; //临界区
CRITICAL_SECTION cs_Write;
CRITICAL_SECTION cs_Read;
struct ThreadInfo               //线程信息
{ int Threadhao;              //线程序号
 char ThreadClass;           //线程类别
 double ThreadStartTime;     //线程开始时间
 double ThreadRunTime;       //线程读写持续时间
};
void ReaderFun(char* file);//读者优先函数 
void R_ReaderThread(void *p);//处理读者优先读者线程
void R_WriterThread(void *p);//处理读者优先写者线程
void WriterFun(char* file); 
void W_ReaderThread(void *p);
void W_WriterThread(void *p);
int main()//主函数
{
 char select;
 while (true)
 {
  cout<<"***************本程序实现读者-写者问题*******\n"<<endl;
  cout<<"              1:读者优先"<<endl;
       cout<<"              2:写者优先"<<endl;
  cout<<"              3:退出"<<endl;
 cout<<"\n*********************************************"<<endl;
  cout<<"请选择要进行的操作:"<<endl;
     do
  {
   cin>>select;
   if(select!='1' && select!='2' && select!='3')
    cout<<"你操作有误,请重试!"<<endl;
  }while (select!='1' && select!='2' && select!='3');
  system("cls");
  if (select=='3')
   return 0;//退出
  else if (select=='1')//调用读者优先
    ReaderFun("peizhi.txt");
    else if(select=='2')//调用写者优先
       WriterFun("peizhi.txt");
  cout<<"\n是否还有继续?  1. 继续   2.退出"<<endl;;
  do
  {
   cin>>select;
   if(select!='1' && select!='2' )
    cout<<"你操作有误,请重试!"<<endl;
  }while (select!='1' && select!='2');
  if(select=='2')
   return 0;// 退出
  system("cls");
 }
 return 0;
}

//读者优先函数
void ReaderFun(char* file)
{
 DWORD n_thread=0;        //线程数初值为0
 DWORD thread_ID;         //线程ID
 DWORD wait_for_all;      //等待所有线程结束
 //临界资源
 HANDLE h_Mutex;
 //互斥对象(h_Mutex)确保线程拥有对单个资源的互斥访问权
 h_Mutex=CreateMutex(NULL,FALSE,"mutex_for_readcount");
 HANDLE h_Thread[64]; //线程对象数组,最大64
 ThreadInfo thread_info[64];
 readcount=0;    
 InitializeCriticalSection(&RP_Write);  //初始化临界区
 ifstream inFile;
 inFile.open(file);                    
 cout<<"读者优先:\n"<<endl;
 while (inFile)
 {//读入每一个读者、写者的信息
 inFile>>thread_info[n_thread].Threadhao
>>thread_info[n_thread].ThreadClass
  >>thread_info[n_thread].ThreadStartTime
>>thread_info[n_thread].ThreadRunTime;
  if (-1 == inFile.get())
   break;
  n_thread++;//线程数加一
 }
 for (int i=0;i<(int)(n_thread);i++)
 {
  if (thread_info[i].ThreadClass=='r')
   //创建读者线程
   h_Thread[i]=CreateThread(NULL,0,\
   (LPTHREAD_START_ROUTINE)(R_ReaderThread),\
   &thread_info[i],0,&thread_ID);
  else
   //创建写者线程
   h_Thread[i]=CreateThread(NULL,0,\
   (LPTHREAD_START_ROUTINE)(R_WriterThread),\
   &thread_info[i],0,&thread_ID);
 }
 wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1);
 cout<<"所有的读者和写者线程完成操作."<<endl;
}
//读者优先-----读者线程
void R_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_ThreadRunTime;        //读文件持续时间
 int m_Threadhao;           //线程序号
 m_Threadhao=((ThreadInfo *)(p))->Threadhao;
 m_delay=(DWORD)(((ThreadInfo *)(p))->ThreadStartTime*100);
 m_ThreadRunTime=(DWORD)(((ThreadInfo *)(p))->ThreadRunTime*100);
 Sleep(m_delay);                                   //延迟等待
 cout<<"读者线程 "<<m_Threadhao<<" 发出读请求."<<endl;
//等待互斥对象通知

上一页  [1] [2] [3] [4] [5] [6] 下一页

Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有