⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reader_writer.cpp

📁 操作系统读者写者问题源程序代码
💻 CPP
字号:
#include<fstream.h>
#include<windows.h>

const char* test_file="thread.dat";
const int max=100;
long readcount=0,writecount=0;

//线程信息结构
struct ThreadInfo{
	unsigned long id;
	char RorW;
	double createtime;
	double runtime;
}thrdinfo[max];

//读写单元
void rwunit(ThreadInfo* p)
{
	if(p->RorW=='R'){
		cout<<"线程 "<<p->id<<" 开始读操作."<<endl;
		Sleep((DWORD)p->runtime*1000);
		cout<<"线程 "<<p->id<<" 结束读操作."<<endl;
	}
	else{
		cout<<"线程 "<<p->id<<" 开始写操作."<<endl;
		Sleep((DWORD)p->runtime*1000);
		cout<<"线程 "<<p->id<<" 结束写操作."<<endl;
	}

}

//读优先写操作
void RP_WriterThread(LPVOID lpParam)
{
	ThreadInfo* p=(ThreadInfo*)lpParam;
	Sleep((DWORD)p->createtime*1000);
	cout<<"线程 "<<p->id<<" 申请写操作."<<endl;
	HANDLE wsem=OpenSemaphore(SEMAPHORE_ALL_ACCESS,false,"wsem");
	WaitForSingleObject(wsem,INFINITE);
	rwunit(p);
	ReleaseSemaphore(wsem,1,NULL);
	CloseHandle(wsem);
}

//读优先读操作
void RP_ReadThread(LPVOID lpParam)
{
	ThreadInfo* p=(ThreadInfo*)lpParam;
	Sleep((DWORD)p->createtime*1000);
	cout<<"线程 "<<p->id<<" 申请读操作."<<endl;
	HANDLE x=OpenMutex(MUTEX_ALL_ACCESS,false,"x");
	HANDLE wsem=OpenSemaphore(SEMAPHORE_ALL_ACCESS,false,"wsem");
	WaitForSingleObject(x,INFINITE);
	readcount++;
	if(readcount==1)WaitForSingleObject(wsem,INFINITE);
	ReleaseMutex(x);
	rwunit(p);
	WaitForSingleObject(x,INFINITE);
	readcount--;
	if(readcount==0)ReleaseSemaphore(wsem,1,NULL);
	ReleaseMutex(x);
	CloseHandle(x);
	CloseHandle(wsem);
}

//写优先写控制
void WP_WriterThread(LPVOID lpParam)
{
	ThreadInfo* p=(ThreadInfo*)lpParam;
	Sleep((DWORD)p->createtime*1000);
	cout<<"线程 "<<p->id<<" 申请写操作."<<endl;
	HANDLE y=OpenMutex(MUTEX_ALL_ACCESS,false,"y");
	HANDLE wsem=OpenSemaphore(SEMAPHORE_ALL_ACCESS,false,"wsem");
	HANDLE rsem=OpenSemaphore(SEMAPHORE_ALL_ACCESS,false,"rsem");
	WaitForSingleObject(y,INFINITE);
	writecount++;
	if(writecount==1)	WaitForSingleObject(rsem,INFINITE);
	ReleaseMutex(y);
	WaitForSingleObject(wsem,INFINITE);
	rwunit(p);
	ReleaseSemaphore(wsem,1,NULL);
	WaitForSingleObject(y,INFINITE);
	writecount--;
	if(writecount==0)	ReleaseSemaphore(rsem,1,NULL);
	ReleaseMutex(y);
	CloseHandle(y);
	CloseHandle(rsem);
	CloseHandle(wsem);
}

//写优先读操作
void WP_ReadThread(LPVOID lpParam)
{
	ThreadInfo* p=(ThreadInfo*)lpParam;
	Sleep((DWORD)p->createtime*1000);
	cout<<"线程 "<<p->id<<" 申请读操作."<<endl;
	HANDLE z=OpenMutex(MUTEX_ALL_ACCESS,false,"z");
	HANDLE x=OpenMutex(MUTEX_ALL_ACCESS,false,"x");
	HANDLE wsem=OpenSemaphore(SEMAPHORE_ALL_ACCESS,false,"wsem");
	HANDLE rsem=OpenSemaphore(SEMAPHORE_ALL_ACCESS,false,"rsem");
	WaitForSingleObject(z,INFINITE);
	WaitForSingleObject(rsem,INFINITE);
	WaitForSingleObject(x,INFINITE);
	readcount++;
	if(readcount==1)	WaitForSingleObject(wsem,INFINITE);
	ReleaseMutex(x);
	ReleaseSemaphore(rsem,1,NULL);
	ReleaseMutex(z);
	rwunit(p);
	WaitForSingleObject(x,INFINITE);
	readcount--;
	if(readcount==0)	ReleaseSemaphore(wsem,1,NULL);
	ReleaseMutex(x);
	CloseHandle(z);
	CloseHandle(x);
	CloseHandle(wsem);
	CloseHandle(rsem);
}

int main()
{
	long i=0,maxthrd;
	char choice;

	ifstream input_file;
	//以只读方式打开文件
	input_file.open(test_file);
	if(!input_file){
		cout<<"Can not open file thread.dat!"<<endl;
	}	
	//读thread.dat
	while(input_file>>thrdinfo[i].id>>thrdinfo[i].RorW>>thrdinfo[i].createtime>>thrdinfo[i].runtime)
		i++;
	input_file.close();
	maxthrd=i;//最大线程数
	
	cout<<"读优先(R)/写优先(W)?:";
	cin>>choice;

	CreateSemaphore(NULL,1,1,"wsem");
	CreateSemaphore(NULL,1,1,"rsem");
	DWORD* thread_id=new DWORD[maxthrd];
	HANDLE* h_thread=new HANDLE[maxthrd];
	//创建线程
	if(choice=='R'||choice=='r'){	//创建读优先
		for(i=0;i<=maxthrd-1;i++){
			if(thrdinfo[i].RorW=='W')
				h_thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_WriterThread),
										&(thrdinfo[i]),0,&thread_id[i]);
			else	
				h_thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_ReadThread),
										&(thrdinfo[i]),0,&thread_id[i]);
			if(h_thread[i]==NULL)	CloseHandle(h_thread[i]);
			cout<<"线程 "<<thrdinfo[i].id<<" 已创建.("<<thrdinfo[i].RorW<<" "<<thrdinfo[i].createtime
				<<" "<<thrdinfo[i].runtime<<")"<<endl;
		}	
	}
	else{	//创建写优先
		for(i=0;i<=maxthrd-1;i++){
			if(thrdinfo[i].RorW=='W')
				h_thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_WriterThread),
										&(thrdinfo[i]),0,&thread_id[i]);
			else	h_thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_ReadThread),
										&(thrdinfo[i]),0,&thread_id[i]);
			if(h_thread[i]==NULL)	CloseHandle(h_thread[i]);
			cout<<"线程 "<<thrdinfo[i].id<<" 已创建.("<<thrdinfo[i].RorW<<" "<<thrdinfo[i].createtime
				<<" "<<thrdinfo[i].runtime<<")"<<endl;
		}
	}
	for(i=0;i<maxthrd;i++)
		ResumeThread(h_thread[i]);

	//结束所有线程
	for(i=0;i<maxthrd;i++){
		ExitThread(thread_id[i]);
		CloseHandle(h_thread[i]);
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -