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

读者写者问题实现操作系统课程设计 第6页
 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);//每秒时钟中断100次
 m_ThreadRunTime=(DWORD)(((ThreadInfo *)(p))->ThreadRunTime*100); Sleep(m_delay);                                  
 cout<<"写者线程 "<<m_Threadhao<<" 发出写请求."<<endl;
 wait_for_mutex=WaitForSingleObject(h_Mutex1,-1); 
 writecount++;
 if (writecount==1)
  EnterCriticalSection(&cs_Read);
 ReleaseMutex(h_Mutex1);
 EnterCriticalSection(&cs_Write);
 cout<<"写者线程 "<<m_Threadhao<<" 开始写文件."<<endl;
 Sleep(m_ThreadRunTime);
 cout<<"写者线程 "<<m_Threadhao<<" 完成写文件."<<endl;
 LeaveCriticalSection(&cs_Write);
 wait_for_mutex=WaitForSingleObject(h_Mutex1,-1); 
 writecount--;
 if(writecount == 0)
  LeaveCriticalSection(&cs_Read);
 ReleaseMutex(h_Mutex1);
}
//写者优先-----读者线程
void W_ReaderThread(void *p)
{
 HANDLE h_Mutex2, h_Mutex3;
 h_Mutex2 = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readcount");
 h_Mutex3 = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_read");
 DWORD wait_for_mutex, wait_for_mutex1;
//等待互斥变量所有权
 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;
 wait_for_mutex1=WaitForSingleObject(h_Mutex3,-1);
 EnterCriticalSection(&cs_Read);
 LeaveCriticalSection(&cs_Read);
 ReleaseMutex(h_Mutex3);
 wait_for_mutex=WaitForSingleObject(h_Mutex2,-1);
 readcount++;
 if (readcount == 1)
  EnterCriticalSection(&cs_Write);
 ReleaseMutex(h_Mutex2);

 cout<<"读者线程 "<<m_Threadhao<<" 开始读文件."<<endl;
 Sleep(m_ThreadRunTime);
 cout<<"读者线程 "<<m_Threadhao<<" 完成读文件."<<endl;

 wait_for_mutex=WaitForSingleObject(h_Mutex2,-1);
 readcount--;
 if (readcount == 0)
  LeaveCriticalSection(&cs_Write);
 ReleaseMutex(h_Mutex2);

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

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