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

📄 ex4.cpp

📁 计算机操作系统的实验作业
💻 CPP
字号:
#include <windows.h>
#include <conio.h>
#include <fstream.h>
#include <stdio.h>

#define MAX_THREAD_NUM  64

struct ThreadInfo
{
	int	serial;
	char entity;
	int  from;
	int  to;
};
int prime[9];
// Mutex Object
HANDLE h_Full;
HANDLE h_Empty;
// Tread Object Array
HANDLE h_Thread[MAX_THREAD_NUM];
ThreadInfo  thread_info[MAX_THREAD_NUM];

void Control_Thread( char* file );
void RP_ReaderThread(void* p);
void RP_WriterThread(void* p);
////////////////////////////////////////////////////////
// main fuction
////////////////////////////////////////////////////////
int main( int agrc, char* argv[] )
{
	char ch;

	for(int i =0;i < 9;i ++)
		prime[i] = 0;
	while ( TRUE )
	{
		// Cleare screen
		system( "cls" );

		// display prompt info
		printf("*********************************************\n");
		printf("       1.Start test\n");
		printf("       2.Exit to Windows\n");
		printf("*********************************************\n");
		printf("Input your choice(1or2): ");
		// if the number inputed is error, retry!
		do{
			ch = (char)_getch();
		}while( ch != '1' && ch != '2');

		system ( "cls" );
		if ( ch == '1')
			Control_Thread("ex4.dat");
		else if ( ch == '2')
			return 0;
		printf("\nPress any key to finish this Program. \nThank you test this Proggram!\n");
		_getch();
	} //end while
} //end main

///////////////////////////////////////////////////////////////
// Reader Priority fuction
// file: filename
//////////////////////////////////////////////////////////////

void Control_Thread( char* file )
{
	DWORD n_thread = 0;
	DWORD thread_ID;
	DWORD wait_for_all;
	h_Full = CreateMutex( NULL,FALSE,"mutex_for_full" );
	h_Empty = CreateMutex( NULL,FALSE,"mutex_for_empty" );
	ifstream  inFile;
	inFile.open(file);		//open file
	printf( "Control Thread:\n\n" );
	while ( inFile )
	{
		// read every reader/writer info
		inFile>>thread_info[n_thread].serial;
		inFile>>thread_info[n_thread].entity;
		inFile>>thread_info[n_thread].from;
		inFile>>thread_info[n_thread++].to;
		inFile.get();
	} //end while
	for( int i = 0; i < (int)(n_thread); i++)
	{
		if(thread_info[i].entity == 'D' || thread_info[1].entity == 'd')
		{
			// Create Reader thread
			h_Thread[i] = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)(RP_ReaderThread), &thread_info[i], 0, &thread_ID);
		}	
		else {
			// Create Writer thread
			h_Thread[i] = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)(RP_WriterThread), &thread_info[i], 0, &thread_ID);
		}
	} //end for
	// waiting all thread will been finished
	wait_for_all = WaitForMultipleObjects(n_thread,h_Thread,TRUE, -1);
	printf("All reader and writer thread have finished Operating.\n");
}// end Control Thread

////////////////////////////////////
// p: reader thread info
///////////////////////////////////
void RP_ReaderThread(void* p)
{
	DWORD wait_for_full;
	DWORD m_from;
	DWORD m_to;
	int m_serial;
	int i = 0;
	int k = 0;

	//get info froam para
	m_serial = ((ThreadInfo*) (p)) -> serial;
	m_from  = (DWORD) (((ThreadInfo*)(p)) -> from);
	m_to = (DWORD) (((ThreadInfo*)(p)) -> to);
	do
	{
		printf("Reader thread %d sents the reading require .\n",m_serial);
		// wait mutex
		wait_for_full = WaitForSingleObject( h_Full,-1 );
		k ++;
		while(!prime[i]) 
		{
			i = (i + 1)%9;
		}
		if(prime[i]>(int)m_from && prime[i]<(int)m_to)
		{
			cout<<"read: "<<prime[i]<<" from "<<i<<endl;
			prime[i] = 0;
		}
		else
			i = ++i%9;
		ReleaseMutex( h_Full );
		Sleep(50);
	}while(k<23);
}

////////////////////////////////////
// p: writer thread info
///////////////////////////////////

void RP_WriterThread(void* p)
{
	h_Full = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_full");
	DWORD wait_for_empty;
	DWORD m_from;
	DWORD m_to;
	int m_serial;
	//get info froam para
	m_serial = ((ThreadInfo*) (p)) -> serial;
	m_from  = (DWORD) (((ThreadInfo*)(p)) -> from);
	m_to = (DWORD) (((ThreadInfo*)(p)) -> to);
	int i =0;
	for(int j = (int)m_from;j < (int)m_to;j ++)
	{
		printf("Writer thread %d sents the writing require .\n",m_serial);
		if(j == 1) continue;
		for(int k = 2;k <= j/2;k ++)
		{
			if( j%k == 0)
				break;
		}
		if(k < j/2 + 1)
			continue;
		wait_for_empty = WaitForSingleObject( h_Empty,-1);
		while(prime[i])
		{
			i = (i + 1)%9;
		}
		prime[i] = j;
		cout<<"write: "<<j<<" to "<<i<<endl;
		ReleaseMutex( h_Empty );
		Sleep(20);
	}
}

⌨️ 快捷键说明

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