资源简介
用三个算法实现读者写者问题,包括写优先、无优先、读优先等,下载即可使用。
代码片段和文件信息
#include
#include
#include “fstream.h“
#include
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);
void NotFun(char* file);
void N_ReaderThread(void *p);
void N_WriterThread(void *p);
int main()//主函数
{
char select;
while (true)
{
cout<<“***************本程序实现读者-写者问题*******\n“< cout<<“ 1:读者优先“< cout<<“ 2:写者优先“< cout<<“ 3:无优先“< cout<<“ 4:退出“< cout<<“\n*********************************************“< cout<<“请选择要进行的操作:“< do
{
cin>>select;
if(select!=‘1‘ && select!=‘2‘ && select!=‘3‘)
cout<<“你操作有误,请重试!“< }while (select!=‘1‘ && select!=‘2‘ && select!=‘3‘);
system(“cls“);
if (select==‘4‘)
return 0;//退出
else if (select==‘1‘)//调用读者优先
ReaderFun(“peizhi.txt“);
else if(select==‘2‘)//调用写者优先
WriterFun(“peizhi.txt“);
else if(select==‘3‘)
NotFun(“peizhi.txt“);
cout<<“\n是否还有继续? 1. 继续 2.退出“< do
{
cin>>select;
if(select!=‘1‘ && select!=‘2‘ )
cout<<“你操作有误,请重试!“< }while (select!=‘1‘ && select!=‘2‘);
if(select==‘2‘)
return 0;// 退出
system(“cls“);
}
return 0;
}
//读者优先函数
void ReaderFun(char* file)
{
DWORD n_thread=0; //线程数初值为0 DWORD 是unsigned long
DWORD thread_ID; //线程ID
DWORD wait_for_all; //等待所有线程结束
//临界资源
HANDLE h_Mutex;
//互斥对象(h_Mutex)确保线程拥有对单个资源的互斥访问权
h_Mutex=CreateMutex(NULLFALSE“mutex_for_readcount“); //创建互斥体
HANDLE h_Thread[64]; //线程对象数组,最大64
ThreadInfo thread_info[64];
readcount=0;
InitializeCriticalSection(&RP_Write); //初始化临界区
ifstream inFile;
inFile.open(file);
cout<<“读者优先:\n“< 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()) //从输入流读取一个字符,并将它转成int值 送返
break;
n_thread++;//线程数加一
}
for (int i=0;i<(int)(n_thread);i++)
{
if (thread_info[i].ThreadClass==‘r‘)
//创建读者线程
h_Thread[i]=CreateThread(NULL0\
(LPTHREAD_START_ROUTINE)(R_ReaderThread)\
&thread_info[i]0&thread_ID);
else
//创建写者线程
h_Thread[i]=CreateThread(NULL0\
(LPTHREAD_START_ROUTINE)(R_WriterThread)\
&thread_info[i]0&thread_ID);
}
wait_for_all=WaitForMultipleobjects(n_thread
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 46 2015-12-18 13:58 peizhi.txt
文件 13354 2015-12-22 09:38 reader_writer.cpp
- 上一篇:世界地图_遥感影像_JPG格式
- 下一篇:jmavsim教程
相关资源
- Nand Flash常见操作及编程实现
- Landsat8数据处理
- Risk-Neutral Valuation-Pricing and Hedging of
- HID 免驱 描述符
- understand2.0注册机
- ParticleAndAnimation脚本.zip
- nand flash verilog contoller
- NAND控制器verilog代码
- AttachedCommandBehavior.zip
- Cloud Computing – Issues Research and Implem
- FastLineRendererforUnity-GPULineandParticleSys
- licensen-standalone.lic
- 手机摄像头检测心率脉搏开源代码
- Tcl and the Tk Toolkit
- MCodeCheck.zip
- zw_weixin_41544464-11084845-43680557DG-of-POSI
- ManageEngine_Patch_Manager_Plus_64bitStandardE
- FacilitiesandAssetManagementV1.zip
- dualband_coupling_matrix_synthesis v 1.0
- Emv Reader Writer v8.6
- FPGA实现的NandFlash控制器(带ECC)文档
- VHDLDesignandFPGAImplementationofLDPCDecode 一篇
- Suppressing Phosphatidylcholine-specific Phosp
- The mechanism of seismo-magnetic anomaly befor
- 2878 horizontally transferred genes in rice ge
- Regional cluster research on China Mobile Mark
- A Wetting-Depth Model for Explanation of Wall-
- Intermediate processes critical phenomena: the
- Global existence and blow-up phenomena for a
- Influence of Contents of Carbon and Phosphorou
评论
共有 条评论