📄 reader_writrer.cpp
字号:
#include"windows.h"
#include<conio.h>
#include<stdlib.h>
#include<fstream.h>
#include<io.h>
#include<string.h>
#include<stdio.h>
#define READER 'R'
#define WRITER 'W'
#define INTE_PER_SEC 1000
#define MAX_THREAD_NUM 64
#define MAX_STR_LEN 32
#define MAX_FILE_LEN 32
int readcount =0;
int writecount =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);
m_persist = (DWORD)(((ThreadInfo *)(p))->persist *INTE_PER_SEC);
Sleep(m_delay);//延迟等待
printf("Reader thread %d sents the reading require.\n",m_serial);
wait_for_mutex =WaitForSingleObject(h_Mutex,-1);
readcount++;
if(readcount==1)
{
EnterCriticalSection(&RP_Write);
}
ReleaseMutex(h_Mutex);
printf("Reader thread %d begin 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);
}
///////////////////////////////////////////
//读者优先处理函数
void ReaderPriority(char * file)
{
DWORD n_thread =0;
DWORD thread_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;
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')
{
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 finishing operating .\n");
}
///////////////////////////////////////////////
void WP_ReaderThread(void * p)
{
HANDLE h_Mutex1;
h_Mutex1 = OpenMutex(MUTEX_ALL_ACCESS,false,"mutex1");
HANDLE h_Mutex2;
h_Mutex2 = OpenMutex(MUTEX_ALL_ACCESS,false,"mutex2");
DWORD wait_for_mutex1;
DWORD wait_for_mutex2;
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("Reader thread %d sents the reading require.\n",m_serial);
wait_for_mutex1 = WaitForSingleObject(h_Mutex1,-1);
EnterCriticalSection(&cs_Read);
wait_for_mutex2 =WaitForSingleObject(h_Mutex2,-1);
readcount++;
if(readcount ==1)
{
EnterCriticalSection(&cs_Write);
}
ReleaseMutex(h_Mutex2);
LeaveCriticalSection(&cs_Read);
ReleaseMutex(h_Mutex1);
printf("Reader thread %d begin to read file.\n",m_serial);
Sleep(m_persist);
printf("Reader thread %d finished reading file .\n",m_serial);
wait_for_mutex2 = WaitForSingleObject(h_Mutex2,-1);
readcount--;
if(readcount==0)
{
LeaveCriticalSection(& cs_Write);
}
ReleaseMutex(h_Mutex2);
}
//page 84写者优先——写者进程
void WP_WriterThread(void *p)
{
DWORD m_delay;
DWORD m_persist;
int m_serial;
DWORD wait_for_mutex3;
HANDLE h_Mutex3;
h_Mutex3 =OpenMutex(MUTEX_ALL_ACCESS,false,"mutex3");
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);
//阻塞互斥对象
wait_for_mutex3 =WaitForSingleObject(h_Mutex3,-1);
writecount++;
if(writecount ==1)
{
EnterCriticalSection( &cs_Read);
}
ReleaseMutex(h_Mutex3);
EnterCriticalSection(&cs_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(&cs_Write);
wait_for_mutex3 =WaitForSingleObject(h_Mutex3,-1);
writecount--;
if(writecount ==0)
{
LeaveCriticalSection(&cs_Read);
}
ReleaseMutex(h_Mutex3);
}
//////////////////////////////////////////////
//写者优先处理
void WriterPriority( char *file)
{
DWORD n_thread =0;
DWORD thread_ID;
DWORD wait_for_all;
HANDLE h_Mutex1;
h_Mutex1 =CreateMutex(NULL,FALSE,"mutex1");
HANDLE h_Mutex2;
h_Mutex2 =CreateMutex(NULL,FALSE,"mutex2");
HANDLE h_Mutex3;
h_Mutex3 =CreateMutex(NULL,FALSE,"mutex3");
//
HANDLE h_Thread[MAX_THREAD_NUM];
ThreadInfo thread_info[MAX_THREAD_NUM];
readcount =0;
writecount =0;
InitializeCriticalSection(&cs_Write);
InitializeCriticalSection(&cs_Read);
ifstream inFile;
inFile.open(file);
printf("Writer 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')
{
h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_ReaderThread),
&thread_info[i],0,&thread_ID);
}
else
{
//
h_Thread[i] =CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)(WP_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("**********************************************\n");
printf(" 1:Reader Priority\n");
printf(" 2:Writer Priority\n");
printf(" 3:Exit to Windows\n");
printf("**********************************************\n");
printf("Enter your choice(1,2 or,3): ");
do
{
ch =(char)_getch();
}while(ch !='1'&& ch !='2' && ch !='3');
system("cls");
//3
if(ch == '3')
return 0;
//1
else if(ch == '1')
ReaderPriority("thread.dat");
//2
else
WriterPriority("thread.dat");
//结束
printf("\nPress Any Key To Continue:");
_getch();
system("cls");
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -