📄 读写者.txt
字号:
#include "windows.h"
#include <conio.h> //屏幕操作函数的头文件,即调用DOS控制台I/O子程序的各个函数
#include<stdlib.h> //说明一些常用的子程序:转换子程序、搜索排序程序等
#include<fstream.h> //c++输入输出流iostream的派生类,用于磁盘文件I/O
#include<string.h> //说明一些字符串和内存操作函数
#include<stdio.h> //定义UNIX中标准和扩展的类型和宏,定义标准I/O预定义流,说明
//I/O流子程序
#define READER 'R' //读者
#define WRITER 'W' //写者
#define INTE PER SEC 100 //每秒时钟中断数目
#define MAX THREAD NUM 64 //最大线程数目
#define MAX FILE NUM 32 //最大数据文件数目
#define MAX STR LEN 32 //字符串长度
int readcount=0; //读者数目
int writecunt=0; //写者数目
CRITICAL SECTION RP Write; //临界区
CRITICAL SECTION cs Write; //临界区
CRITICAL SECTION cs Read; //临界区
struct ThreadInfo
{
int serial; //线程序号
char entity; //线程类别
double delay; //线程延迟
double persist; //线程读写操作的持续时间
};
/////////////////////////////////////////////////////////////////////////////////////读者优先-读者线程
//p:读者线程信息
void RP 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 persist; //读文件持续时间
int m serial; //线程序号
//从参数中获得信息
m serial =((ThreadInfo*)(p))->serial;
m delay =(DWORD)(((ThreadInfo*)(p))->delay*INTE PER SEC);
Sleep(m delay); //延迟等待
printf("Reader thread %d sents the reading require.\n", m serial);
//等待互斥信号,保证对readcount的访问、修改互斥
wait for mutex =WaitForSingleObject(h Mutex , -1);
if(readcount ==0)
EnterCriticalSection(&RP Write);
readcount++;
ReleaseMutex(h Mutex); //释放互斥信号
//读文件
printf("Reader thread %d begins to read file,\n",m serial);
Sleep(m persist); //延迟一下,满足读文件的持续时间
printf("Reader thread %d finished reading file.\n",m serial);
wait for mutex =WaitForSingleObject(h Mutex,-1);
readcount--;
if (readcount==0)
LeaveCriticalSection(&RP Write);
ReleaseMutex(h Mutex);
}
/////////////////////////////////////////////////////////////////////////////////////读者优先---写者线程
//p: 写者线程信息
void RP WriterThread(void* p)
{
DWORD m delay;
DWORD m persist;
int m serial;
m serial=((ThreadInfo*)(p))->serial;
m delay=(DWORD)(((ThreadInfo*)(p))->delay*INTE PER SEC);
m persist=(DWORD)(((ThreadInfo*)(p))->persist*INTE PER SEC);
Sleep(m delay);
printf("writer thread %d sents the writing require.\n",m serial);
EnterCriticalSection(&RP Write);
//写文件
printf("writer thread %d begins to write to the file .\n",m serial);
Sleep(m persist);
printf("writer thread %d finishing writing to the file.\n",m serial);
LeaveCriticalSection(&RP Write);
}
/////////////////////////////////////////////////////////////////////////////////////读者优先处理函数
//file:文件名
void ReaderPriority(char* file)
{
DWORD n thread=0; //线程数目
DWORD thread ID; //线程ID
DWORD wait for all; //等待所有线程结束
HANDLE h Mutex;
h Mutex=CreateMutex(NULL,FALSE,"mutex for readcount");
//线程对象数组
HANDLE h Thread[MAX THREAD NUM];
ThreadInfo thread info[MAX THREAD NUM];
readcount=0; //初始化readcount
InitializeCriticalSection(&RP Write); //初始化临界区
ifstream inFile;
inFile.open(file); //打开文件
printf("reader priority:\n\n");
while(inFile)
{
//读入每一个读者、写者信息
inFile>>thread info[n thread].serial;
inFile>>thread info[n thread].entity;
inFile>>thread info[n thread].delay;
inFile>>thread info[n thread++].persist;
inFile.get();
}
for (int i=0;i<(int)(n thread);i++)
{
if(thread info[i].entity==READER||thread info[i].entity=='R')
{ //创建读者线程,&thread ID是输出参数接受线程ID
h Thread[i]=CreateThread(NULL,0,(LPTHREAD START ROUTINE)(RP ReaderThread),&thread info[i],0,&thread ID);
}
else{
h Thread[i]=CreateThread(NULL,0,(LPTHREAD START ROUTINE)(RP WriterThread),&thread info[i],0,&thread ID);
}
}
//等待所有线程结束
wait for all=WaitForMultipleObjects(n thread,h Thread,TRUE,-1);
printf("All reader and writer have finished operating ,\n");
}
//主函数
int main(int argc,char*argv[])
{
char ch;
while(true)
{
printf(" 1:Reader priority\n");
printf(" 2:Writer priority\n");
printf(" 3:Exit to windows\n");
do{
ch=(char) getch();
}while(ch!='1'&&ch!='2'&&ch!='3');
system("cls");
if(ch=='3')
return 0;
else if(ch=='1')
ReaderPriority("thread.dat");
printf("\npress any key to continue:");
getch();
system("cls");
return 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -