?? process.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 //每秒時鐘中斷數(shù)目
#define MAX_THREAD_NUM 64//最大線程數(shù)目
#define MAX_FILE_NUM 32//最大數(shù)據(jù)文件數(shù)目
#define MAX_STR_LEN 32//字符串長度
int readcount = 0;//讀者數(shù)目
int writecount=0;//寫者數(shù)目
CRITICAL_SECTION RP_Write;//臨界區(qū)
CRITICAL_SECTION cs_Write;
CRITICAL_SECTION cs_Read;
struct ThreadInfo//線程結(jié)構(gòu)
{
int serial;//線程序號
char entity;//線程類別(判斷是讀者線程還是寫者線程)
double delay;//線程延遲
double persist;//線程讀寫操作持續(xù)時間
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//讀者優(yōu)先-讀者線程
//p:讀者線程信息
void RP_ReaderThread(void* p)
{
//互斥變量
HANDLE h_Mutex;
h_Mutex =OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readcount");
DWORD wait_for_mutex;//等待互斥變量所有權(quán)
DWORD m_delay;//延遲時間
DWORD m_persist;//讀文件持續(xù)時間
int m_serial;//線程序號
//從參數(shù)中獲得信息
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);
//等待互斥信號,保證對readcount的訪問、修改和互斥
wait_for_mutex=WaitForSingleObject(h_Mutex,-1);//P操作
//讀者數(shù)目增加
readcount++;
if(readcount==1)
{
//讀第一個讀者,等待資源
EnterCriticalSection(&RP_Write);//P操作
}
ReleaseMutex(h_Mutex);//V操作
printf("Reader thread %d begins to read file.\n", m_serial);
Sleep(m_persist);
//退出線程
printf("Reader thread %d finished reading file.\n", m_serial);
//等待互斥信號,保證對readcount的訪問、修改互斥
wait_for_mutex=WaitForSingleObject(h_Mutex,-1);//P操作
//讀者數(shù)目減少
readcount--;
if(readcount==0)
{
//如果讀者全部讀完,喚醒寫者
LeaveCriticalSection(&RP_Write);//V操作
}
ReleaseMutex(h_Mutex);//V操作
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//讀者優(yōu)先-寫者線程
//寫者線程信息
void RP_WriterThread(void* p)
{
DWORD m_delay;//延遲時間
DWORD m_persist;//寫文件持續(xù)時間
int m_serial;//線程序號
//從參數(shù)中獲得信息
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);//P操作
//寫文件
printf("Writer thread %d begins to write to the file.\n", m_serial);
Sleep(m_persist);
//退出線程
printf("Writer thread %d finished writing to the file.\n", m_serial);
//釋放資源
LeaveCriticalSection(&RP_Write);//V操作
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//讀者優(yōu)先處理函數(shù)
//file:文件名
void ReaderPriority(char* file)
{
DWORD n_thread=0;//線程數(shù)目
DWORD thread_ID;//線程ID
DWORD wait_for_all;//等待所有線程結(jié)束
//互斥對象
HANDLE h_Mutex;
h_Mutex =CreateMutex(NULL,FALSE,"mutex_for_readcount");
//線程對象的數(shù)組
HANDLE h_Thread[MAX_THREAD_NUM];
ThreadInfo thread_info[MAX_THREAD_NUM];
readcount=0;//初始化readcount
InitializeCriticalSection(&RP_Write);//初始化臨界區(qū)
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')
{
//創(chuàng)建讀者進(jìn)程
h_Thread[i]=CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)(RP_ReaderThread),
&thread_info[i],
0,&thread_ID);
}
else{
//創(chuàng)建寫者進(jìn)程
h_Thread[i]=CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)(RP_WriterThread),
&thread_info[i],
0,&thread_ID);
}
}
//等待所有線程結(jié)束
wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1);
printf("All reader and writer have finished operating.\n");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//寫者優(yōu)先--讀者進(jìn)程
//p:讀者線程信息
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;//等待互斥變量所有權(quán)
DWORD wait_for_mutex2;
DWORD m_delay;//延遲時間
DWORD m_persist;//讀文件持續(xù)時間
int m_serial;//線程序號
//從參數(shù)中獲得信息
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);//P操作
//進(jìn)入讀者臨界區(qū)
EnterCriticalSection(&cs_Read);//P操作
//阻塞互斥對象mutex2,保證對readcount的訪問、修改互斥
wait_for_mutex2=WaitForSingleObject(h_mutex2,-1);//P操作
//修改讀者數(shù)目
readcount++;
if(readcount==1)
{
//如果是第一個讀者,等待寫者寫完
EnterCriticalSection(&cs_Write);//P操作
}
ReleaseMutex(h_mutex2);//V操作
//讓其他讀者進(jìn)入臨界區(qū)
LeaveCriticalSection(&cs_Read);//V操作
ReleaseMutex(h_mutex1);//V操作
//讀文件
printf("Reader thread %d begins to read file.\n", m_serial);
Sleep(m_persist);
//退出線程
printf("Reader thread %d finished reading file.\n", m_serial);
//阻塞互斥對象mutex2,保證對readcount的訪問、修改互斥
wait_for_mutex2=WaitForSingleObject(h_mutex2,-1);//P操作
readcount--;
if(readcount==0)
{
//如果所有讀者讀完,喚醒寫者
LeaveCriticalSection(&cs_Write);//V操作
}
ReleaseMutex(h_mutex2);//V操作
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//寫者優(yōu)先--寫者線程
//p:寫者線程信息
void WP_WriterThread(void* p)
{
DWORD wait_for_mutex3;
DWORD m_delay;//延遲時間
DWORD m_persist;//寫文件持續(xù)時間
int m_serial;//線程序號
//互斥對象
HANDLE h_mutex3;
h_mutex3=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex3");
//從參數(shù)中獲得信息
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);
//阻塞互斥對象mutex3,保證對writecount的訪問、修改互斥
wait_for_mutex3=WaitForSingleObject(h_mutex3,-1);//P操作
//修改寫者數(shù)目
writecount++;
if(writecount==1)
{
//第一個寫者,等待讀者讀完
EnterCriticalSection(&cs_Read);//P操作
}
ReleaseMutex(h_mutex3);//V操作
//進(jìn)入寫者臨界區(qū)
EnterCriticalSection(&cs_Write);//P操作
//寫文件
printf("Writer thread %d begins to write to the file.\n", m_serial);
Sleep(m_persist);
//退出線程
printf("Writer thread %d finished writing to the file.\n", m_serial);
//離開臨界區(qū)
LeaveCriticalSection(&cs_Write);//V操作
//阻塞互斥對象mutex3,保證對writecount的訪問、修改互斥
wait_for_mutex3=WaitForSingleObject(h_mutex3,-1);//P操作
writecount--;
if(writecount==0)
{
//寫者寫完,讀者可以讀
LeaveCriticalSection(&cs_Read);//V操作
}
ReleaseMutex(h_mutex3);//V操作
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//寫者優(yōu)先處理函數(shù)
//file:文件名
void WriterPriority(char* file)
{
DWORD n_thread=0;//線程數(shù)目
DWORD thread_ID;//線程ID
DWORD wait_for_all;//等待所有線程結(jié)束
//互斥對象
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;//初始化readcount
writecount=0;//初始化writecount
InitializeCriticalSection(&cs_Write);//初始化臨界區(qū)
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')
{
//創(chuàng)建讀者進(jìn)程
h_Thread[i]=CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)(RP_ReaderThread),
&thread_info[i],
0,&thread_ID);
}
else{
//創(chuàng)建寫者進(jìn)程
h_Thread[i]=CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)(RP_WriterThread),
&thread_info[i],
0,&thread_ID);
}
}
//等待所有線程結(jié)束
wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1);
printf("All reader and writer have finished operating.\n");
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//主函數(shù)
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):");
//如果信息不正確,繼續(xù)輸入
do{
ch=(char)_getch();
}while(ch!='1'&&ch!='2'&&ch!='3');
system("cls");
//選擇3,返回
if(ch=='3')
return 0;
//選擇1,讀者優(yōu)先
else if (ch=='1')
ReaderPriority("thread.dat");
//選擇2,寫者優(yōu)先
else
WriterPriority("thread.dat");
//結(jié)束
printf("\nPress Any Key To Continue:");
_getch();
system("cls");
}
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -