📄 readwrite.cpp
字号:
//#include "stdafx.h"
#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 100 //每秒时钟中断数目
#define MAX_THREAD_NUM 64 //最大线程数目
#define MAX_FILE_NUM 32 //最大数据文件数目
#define MAX_STR_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; //线程读写持续时间
};
void ReaderPriority(char* file);
void RP_ReaderThread(void *p);
void RP_WriterThread(void *p);
void WriterPriority(char* file);
void WP_ReaderThread(void *p);
void WP_WriterThread(void *p);
int main()
{
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)\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");
else
WriterPriority("thread.dat");
printf("\nPress Any key to continue.\n");
_getch();
system("cls");
}
return 0;
}
//读者优先处理函数
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;
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;
if (-1 == inFile.get())
break;
n_thread++;
}
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 finished operating.\n");
}
//读者优先-----读者线程
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的访问、
//修改互斥
readcount++; //读者数目增加
if (readcount==1)
EnterCriticalSection(&RP_Write);
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的访问、修改互斥
readcount--; //读者数目减少
if (readcount==0)
LeaveCriticalSection(&RP_Write); //如果所有读者读完,唤醒写者
ReleaseMutex(h_Mutex);
}
//读者优先-----写者线程
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 file.\n",m_serial);
Sleep(m_persist);
//退出线程
printf("Writer thread %d finished writing file.\n",m_serial);
LeaveCriticalSection(&RP_Write); //如果所有读者读完,唤醒写者
}
//写者优先处理函数
void WriterPriority(char* file)
{
DWORD n_thread=0; //线程数目
DWORD thread_ID; //线程ID
DWORD wait_for_all; //等待所有线程结束
//互斥对象
HANDLE h_Mutex1, h_Mutex2, h_Mutex3;
h_Mutex1 = CreateMutex(NULL, FALSE, "mutex_for_writecount");
h_Mutex2 = CreateMutex(NULL, FALSE, "mutex_for_readcount");
h_Mutex3 = CreateMutex(NULL, FALSE, "mutex_for_read");
//线程对象数组
HANDLE h_Thread[MAX_THREAD_NUM];
ThreadInfo thread_info[MAX_THREAD_NUM];
readcount=0; //初始化readcount
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;
if(-1 == inFile.get())
break;
n_thread++;
}
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");
}
//写者优先-----写者线程
void WP_WriterThread(void *p)
{
//互斥变量
HANDLE h_Mutex1;
h_Mutex1 = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_writecount");
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("Writer thread %d sents the writing require.\n",m_serial);
wait_for_mutex=WaitForSingleObject(h_Mutex1,-1);
writecount++;
if (writecount==1)
EnterCriticalSection(&cs_Read);
ReleaseMutex(h_Mutex1);
EnterCriticalSection(&cs_Write);
printf("Writer thread %d begins to write file.\n",m_serial);
Sleep(m_persist);
printf("Writer thread %d finished writing file.\n",m_serial);
LeaveCriticalSection(&cs_Write);
wait_for_mutex=WaitForSingleObject(h_Mutex1,-1);
writecount--;
if(writecount == 0)
LeaveCriticalSection(&cs_Read);
ReleaseMutex(h_Mutex1);
}
//写者优先-----读者线程
void WP_ReaderThread(void *p)
{
HANDLE h_Mutex2, h_Mutex3;
h_Mutex2 = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readcount");
h_Mutex3 = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_read");
DWORD wait_for_mutex, wait_for_mutex1; //等待互斥变量所有权
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_Mutex3,-1);
EnterCriticalSection(&cs_Read);
/*wait_for_mutex=WaitForSingleObject(h_Mutex2,-1);
readcount++;
if (readcount == 1)
EnterCriticalSection(&cs_Write);
ReleaseMutex(h_Mutex2);*/
LeaveCriticalSection(&cs_Read);
ReleaseMutex(h_Mutex3);
wait_for_mutex=WaitForSingleObject(h_Mutex2,-1);
readcount++;
if (readcount == 1)
EnterCriticalSection(&cs_Write);
ReleaseMutex(h_Mutex2);
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_Mutex2,-1);
readcount--;
if (readcount == 0)
LeaveCriticalSection(&cs_Write);
ReleaseMutex(h_Mutex2);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -