pcwin.cpp

来自「生产消费者问题 是操作系统程序设计的课程作业」· C++ 代码 · 共 191 行

CPP
191
字号
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include<iostream>
#include<time.h>

#include<string>

using namespace std;

int const MAXCUS = 3;
int const MAXPRO = 2;
int const NUMBER = 6;
int const MAXNUM = 3;
const LPCTSTR MUTEMNAME="mutex.suicide";
const LPCTSTR FILEMAPNAME="file.map";
const LPCTSTR FULLNAME="sem.full";
const LPCTSTR EMPTYNAME="sem.empty";
#define MILLIONS 1000

void outtime()
{
	time_t timep; 
	struct tm *p; 
	time(&timep); 
	p=localtime(&timep); /*取得当地时间 */ 
	printf ("<%d/%d/%d ", (1900+p->tm_year),(1+p->tm_mon), p->tm_mday); 
	printf(" %d:%.2d:%.2d>",p->tm_hour, p->tm_min, p->tm_sec);
}

HANDLE  MakeSharedFile()
{	
	int i;
	//创建一个临时文件映射对象并将其初始化为0
	HANDLE  hMapping=CreateFileMapping(INVALID_HANDLE_VALUE, NULL,PAGE_READWRITE,0,sizeof(int)*(MAXNUM+2),FILEMAPNAME);
	//把文件映射到当前地址空间,返回文件映射的起始地址
	LPVOID  pData=MapViewOfFile( hMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0); 
	//分配内存空间,并将其清0
	ZeroMemory(pData, sizeof(int)*(MAXNUM+2));
	int *ptr=reinterpret_cast<int*>(pData);
	//将三个缓冲区清为-1
	ptr+=2;
	for (i=0;i<MAXNUM;i++)
	{
		(*ptr)=-1;
		ptr++;
	}
	UnmapViewOfFile(pData); 
	return(hMapping);
}	
 
int *getqueue()
{
	HANDLE  hMapping=OpenFileMapping (FILE_MAP_ALL_ACCESS,FALSE,FILEMAPNAME);
	LPVOID  pData=MapViewOfFile( hMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0); 
	int *ptr=reinterpret_cast<int*>(pData);
	return ptr;
}

void outstate(int *ptr)
{
	int i;
	int *p;
	printf("Buffer state \n");
	printf("The writer ptr = %d\n",*ptr);
	printf("The reader ptr = %d\n",*(ptr+1));
	p=ptr+2;
	for (i=0;i<MAXNUM;i++)
		printf("Buffer <%d> = %d\n",i,*(p+i));
}

void customer()
{
//	printf("Customer %d\n",GetCurrentProcessId());
	 HANDLE mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,MUTEMNAME);
	 HANDLE empty=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,EMPTYNAME);
	 HANDLE full=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,FULLNAME);
	 int *read;
	 int x,i,pid=GetCurrentProcessId();
	 int *ptr;
	 printf("Customer %d is created!\n",pid);
	 for (i=0;i<NUMBER*MAXPRO/MAXCUS;i++)
	 {
	 	WaitForSingleObject(full,INFINITE);
		WaitForSingleObject(mutex,INFINITE);
		ptr=getqueue();
		outstate(ptr);
		read=ptr+2;
		ptr++;
		read+=(*ptr);
		x=(*read);
		outtime();
		(*ptr) = ((*ptr) + 1 ) % MAXNUM;
		printf("Customer %d customed  a number %d\n",pid,x);
		ReleaseMutex(mutex);
		ReleaseSemaphore(empty,1,NULL);
		Sleep(MILLIONS);
	 }
}

void produce()
{
//	printf("Producer %d\n",GetCurrentProcessId());
	 HANDLE mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,MUTEMNAME);
	 HANDLE empty=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,EMPTYNAME);
	 HANDLE full=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,FULLNAME);
	 int *write;
	 int x,i,pid=GetCurrentProcessId();
	 int *ptr;
	 	printf("Producer %d is created!\n",pid);
	 for (i=0;i<NUMBER;i++)
	 {
		WaitForSingleObject(empty,INFINITE);
		WaitForSingleObject(mutex,INFINITE);
		ptr=getqueue();
		outstate(ptr);
		write=ptr + 2;
		write+=(*ptr);
		x=i+pid*10;
		(*write)=x;
		outtime();
		(*ptr) = ((*ptr) + 1) % MAXNUM;
		printf("Producer %d Produced a number %d\n",pid,x);
		ReleaseMutex(mutex);
		ReleaseSemaphore(full,1,NULL);
		Sleep(MILLIONS);
	 }
}

void parent()
{
	HANDLE  hMapping=MakeSharedFile();
	HANDLE  hMutex=CreateMutex(NULL, FALSE,MUTEMNAME);  
	HANDLE  full=CreateSemaphore(NULL,0,MAXNUM,FULLNAME); 
	HANDLE  empty=CreateSemaphore(NULL,MAXNUM,MAXNUM,EMPTYNAME); 
	STARTUPINFO si[MAXCUS+MAXPRO]={sizeof(STARTUPINFO)};
	PROCESS_INFORMATION pi[MAXCUS+MAXPRO];
	int i,j,k=0;
	char szCmdLine[MAX_PATH],szFilename[MAX_PATH];
	GetModuleFileName(NULL, szFilename, MAX_PATH);     
	string cmd1(szFilename);
	cmd1+=string(" customer");
	for (j=0;j<cmd1.size();j++)
		szCmdLine[j]=cmd1[j];
	szCmdLine[j]='\0';
	for (i=0;i<MAXCUS;i++)
	{
		bool bCreateOK=CreateProcess(NULL,szCmdLine,NULL,NULL,FALSE,0,NULL,NULL,&si[k],&pi[k]);
		k++;
	}
	
	string cmd2(szFilename);
	cmd2+=string(" produce");
	for (j=0;j<cmd2.size();j++)
		szCmdLine[j]=cmd2[j];
	szCmdLine[j]='\0';	

	for (i=0;i<MAXPRO;i++)
	{
		bool bCreateOK=CreateProcess(NULL,szCmdLine,NULL,NULL,FALSE,0,NULL,NULL,&si[k],&pi[k]);
		k++;
	}

	for (i=0;i<MAXCUS+MAXPRO;i++)
			WaitForSingleObject(pi[i].hProcess,INFINITE);

	for (i=0;i<MAXCUS+MAXPRO;i++)
	{
		CloseHandle(pi[i].hProcess);
		CloseHandle(pi[i].hThread);
	}

}

 main(int argc,char **argv)
{   
                  
	if (argc==1) {
		parent();
	}
	if (argc==2&&!strcmp(argv[1],"customer")) {
		customer();
	}
	if (argc==2&&!strcmp(argv[1],"produce")) {
		produce();
	}
    getchar();
	
}

⌨️ 快捷键说明

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