资源描述
读者——写者问题C++程序
(原创作品)
演示效果图:
程序完整代码:
#include <windows.h>
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include<string>
#include<stdlib.h>//包含清屏头文件
//全局变量
int empty=1;//信号量
int wrIn=1;//信号量
int max;
int temp[30];
int *wait,*next;
int count=0;//记录已经完成操作的线程数
int waitSemapore;
int type;//类型记录
int writc=1;
int readc=1;
void reader()/////////////////////////////////////////////////////读者函数开始
{
empty--;//P操作改变信号量
cout<<"读者获得资源"<<"(";
SYSTEMTIME sys;
GetLocalTime(&sys);//输出系统时间
cout<<"系统时间:"<<sys.wHour<<":"<<sys.wMinute<<":"<<sys.wSecond<<":"<<sys.wMilliseconds<<")"<<endl;
cout<<endl<<"*"<<"读者正在进行读操作..."<<endl;
//empty++;//信号量
count++;
}/////////////////////////////////////////////////////////读者函数结尾
void writer()//////////////////////////////////////////////////写者函数开始
{
empty--;//P操作改变信号量
wrIn--;//P操作改变信号量
cout << " 写者"<<writc<<"获得资源" <<"(";
SYSTEMTIME sys;
GetLocalTime(&sys);//输出系统时间
cout<<"系统时间:"<<sys.wHour<<":"<<sys.wMinute<<":"<<sys.wSecond<<":"<<sys.wMilliseconds<<")"<<endl;
cout<<"请输入线程要写入的内容,以/结尾"<<endl;
char ch='a';
ofstream outfile("f.txt",ios::out|ios::app);
if(!outfile)
{
cerr<<"open file error!"<<endl;
abort();
}
while(ch!='/')
{ cin>>ch;
outfile<<ch;
}
cout<<endl<<"*" <<"写者"<<writc<<"正在进行写操作.." << endl;
outfile.close();
count++;
//wrIn++;//V操作改变信号量
//empty++;//V操作改变信号量
}//////////////////////////////////////////////////////////////写者函数结尾
void codeIn()/////////////////////////////////////////////////////////////////输入界面
{
int ru;
cout<<"请输入你要创建多少个线程:"<<endl;
cin>>max;
for(int i=0;i<max;i++)
{
cout<<"你所要创建的第"<<i+1<<"个线程类型为:1.读者 2.写者"<<endl;
cin>>ru;///////////////
if(ru==1||ru==2)
temp[i]=ru;
else
cout<<"输入有误!请重新输入"<<endl;/////////////判断错误
if(i==max-1)//录入最后一个线程类型操作
{
//system("cls");//清屏
HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE); //设置输出字体颜色red
SetConsoleTextAttribute(hOut,
FOREGROUND_RED |
FOREGROUND_INTENSITY);
cout<<endl<<"输入完毕!"<<endl;
hOut = GetStdHandle(STD_OUTPUT_HANDLE); //设置输出字体背景
SetConsoleTextAttribute(hOut,
BACKGROUND_GREEN |
BACKGROUND_INTENSITY);
for(int j=0;j<max;j++)//////////////////输出录入结果
{cout<<"线程"<<j+1<<"为:";
if(temp[j]==1)cout<<"读者"<<endl;
else cout<<"写者"<<endl;
}///////////////////////////////输出录入结果
cout<<"下面将进行动态演示:"<<endl<<endl;
system("pause");
cout<<endl;
}//录入最后一个线程类型操作
}//录入界面结束}
}/////////////////////////////////////////////////////////codeIn()
int main()/////////////////////////////////////////////////主函数
{
codeIn();//录入界面
wait=&temp[0];
while(wait<&temp[max]&&count<max)
{
waitSemapore=1;
next=wait;
while(*next==1||*next==2)//线程调用算法
{
if(*next==1)//reader
{if(empty==1||(empty==0&&wrIn==1&&*wait!=2))//体现写者优先
{reader();//调用读者函数
type=1;}
else
{cout<<"["<<"读者此时正在等待资源"<<"]"<<endl;
//Sleep(5000);////////////////////////////////////////////////////////////////////////////////////////////sleep()
if(waitSemapore==1)
{wait=next;//////////this reader is waiting,locate it.
waitSemapore--;
}
}
}
else//writer
{
if(empty==1)
{writer();//调用写者函数
type=2;}
else
{
cout<<"["<<"写者此时正在等待资源"<<"]"<<endl;
//Sleep(5000);/////////////////////////////////////////////////////////////////////////////////////////////sleep()
if(waitSemapore==1)///////////this writer is waiting,find it's location.
{wait=next;
waitSemapore--;
}
}
}//writer is end
next++;
}//1while()
if(type==1)
{cout<<"~"<<"读者"<<readc<<"读完毕,释放资源"<<endl;
readc++;
empty++;//读者用的,但是为了在输出时演示出运行状态,不得不将其放在此处
Sleep(3000);/////////////////////////////////////////////////////////////////////////////////////////////sleep()
cout<<endl;
}
else
if(type==2)
{
cout<<"~"<<"写者"<<writc<<"写完毕,释放资源" << endl;
writc++;
wrIn++;//V操作改变信号量,写者用,但是为了在输出时演示出运行状态,不得不将其放在此处
empty++;//V操作改变信号量,写者用,但是为了在输出时演示出运行状态,不得不将其放在此处
Sleep(3000);/////////////////////////////////////////////////////////////////////////////////////////////sleep()
cout<<endl;
}
}//2while()
return 0;
}/////////////////////////////////////////////////////主函数结束
展开阅读全文