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

📄 ex1.cpp

📁 操作系统 程序实现一个虚拟机模拟多道的运行环境
💻 CPP
字号:
// Ex1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Device.h"
int TimePassed=0;
CPtrArray DeviceList;
CPtrArray ProcessList;
//CPtrArray FinishedProcessList;
CString FileName;
CString ModuleFilePath;
BOOL loadTask();
BOOL ExecuteTask();
void OutputResult();
void Clear();
void GetModuleFilePath();

inline void GetString(fstream& fs,CString&str){	
	char chr[20];
	fs.getline(chr,15,'>');
	str=chr;
	str.TrimLeft("\n");
	str.TrimLeft(" ");
	str.TrimRight("\n");
	str.TrimRight(" ");
	str.MakeLower();
	str+=">";
	if('/'==str[0]) str.Insert(0,'<');
}

//主函数
int main(int argc, char* argv[]){
	int SuccTasks=0;
	int FailTasks=0;
	if(argc<=1){
		cout<<"程序必须带参数运行!"<<endl;
		return -1;
	}
	for(int i=1;i<argc;i++){
		TimePassed=0;
		FileName=argv[i];
		GetModuleFilePath();
		if(!loadTask()){	//装载任务
			cout<<"装载任务失败!"<<endl;
			FailTasks++;
			continue;
		}
		ExecuteTask();	//执行任务
		OutputResult();	//输出结果
		Clear();
		SuccTasks++;
	}
	cout<<endl<<"任务已完成,共:"<<SuccTasks+FailTasks<<"个,成功:"<<SuccTasks<<"个,失败:"<<FailTasks<<"个"<<endl;
	return 0;
}
BOOL loadTask(){
	fstream fin;
	int i=0,stime,etime;
	int LastEndTime=0;
	CString *str=NULL,devType,ProName;
	CStringArray sStack;
	char sep;
	CDevice* dev=NULL;
	CProcess* Pro=NULL;
	BOOL ErrorOccured=FALSE;
	BOOL FormatError=FALSE;
	cout<<endl<<"分析文件:"<<FileName<<endl;
	fin.open(FileName,ios::in|ios::nocreate);
	if (!fin.is_open()){
		cout<<"文件不存在!"<<endl;
		return FALSE;
	}
	do {	//开始分析文件
		str=new CString;
		GetString(fin,*str);
		CString chk;
		if (sStack.GetSize()>0){//取得当前栈顶元素
			chk=sStack.GetAt(0);
			chk.Remove('/');
		}
		if(sStack.GetSize()>0 && sStack.GetAt(0)==*str){//配对	
			str=&sStack.GetAt(0);
			sStack.RemoveAt(0);
			if ("</unit>"==*str){ 
				Pro=NULL;
				LastEndTime=0;
			}
			str=NULL;
		}else if ("<input>"==*str){//输入开始,一直到</input>结束
			str->Replace("<","</");
			sStack.InsertAt(0,*str);//压栈
		}else if ("<unit>"==*str && "<input>"==chk){//独立的新任务,一直到</unit>结束
			str->Replace("<","</");
			sStack.InsertAt(0,*str);//压栈
		}else if ("<program>"==*str && "<unit>"==chk){//新任务名,一直到</program>结束
			fin.getline((char*)(LPCTSTR)ProName,100,'<');
			int		proCount=ProcessList.GetSize();
			BOOL	bExist=FALSE;
			for(i=0;i<proCount;i++){
				Pro=(CProcess*)ProcessList.GetAt(i);
				if (ProName==Pro->GetProcessName()){
					bExist=TRUE;
					break;
				}
			}
			if (bExist){
				cout<<"任务名有重复."<<endl;
				break;
			}else{
				Pro=new CProcess(ProName);
				ProcessList.Add(Pro);
			}
			str->Replace("<","</");
			sStack.InsertAt(0,*str);//压栈
		}else if ("<step>"==*str && "<unit>"==chk && NULL!=Pro){//任务步骤,一直到</step>结束
			fin>>stime>>sep>>etime>>sep;
			fin.getline((char*)(LPCTSTR)devType,100,'<');
			if(stime>=etime || stime!=LastEndTime){
				if(!(stime==etime || stime==LastEndTime))
					break;
			}else{
				//增加设备
				BOOL bExist=FALSE;
				int devCount=DeviceList.GetSize();
				for (i=0;i<devCount;i++){
					dev=(CDevice*)DeviceList.GetAt(i);
					if(devType==dev->GetDeviceType()){
						bExist=TRUE;
						break;
					}
				}
				if (!bExist){
					dev=new CDevice(devType);
					DeviceList.Add(dev);
				}
				//增加step
				CUseDevice udev(Pro->GetProcessName(),devType);
				udev.SetRequestTimeLength(etime-stime);
				Pro->AddStep(udev);
				LastEndTime=etime;
			}
			str->Replace("<","</");
			sStack.InsertAt(0,*str);//压栈
		}else{
			cout<<"分析文件时有错误发生,文件格式不正确!"<<endl;
			ErrorOccured=TRUE;
			FormatError=TRUE;
			break;
		}
	} while(sStack.GetSize()>0 && !fin.eof());	//分析文件结束
	fin.close();
	if (0==sStack.GetSize() && !ErrorOccured){	//检查文件读取状态
		int ProcessCount=ProcessList.GetSize();
		for (i=0;i<ProcessCount;i++)	//将任务装载进设备
			((CProcess*)ProcessList.GetAt(i))->WaitNextDevice();
		return TRUE;
	}else{
		if(!FormatError){	//是否是文件格式错误
			cout<<"分析文件时有错误发生,文件内容不正确."<<endl;

		}
		//删除内存
		while (ProcessList.GetSize()){
			CProcess* pro=(CProcess*)ProcessList.GetAt(0);
			ProcessList.RemoveAt(0);
			delete pro;
			pro=NULL;
		}
		while(DeviceList.GetSize()){
			CDevice* dev=(CDevice*)DeviceList.GetAt(0);
			DeviceList.RemoveAt(0);
			delete dev;
			dev=NULL;
		}
		while (sStack.GetSize()){
			str=&sStack.GetAt(0);
			sStack.RemoveAt(0);
			delete str;
			str=NULL;
		}
		return FALSE;
	}
}
BOOL ExecuteTask(){
	int ProcessCount=ProcessList.GetSize();
	int DeviceCount=DeviceList.GetSize();
	int i;
	while (ProcessCount){
		for(i=0;i<DeviceCount;i++) //每个设备正在运行
			((CDevice*)DeviceList.GetAt(i))->Run();
		for(i=0;i<DeviceCount;i++)	//设备检查是否空闲
			((CDevice*)DeviceList.GetAt(i))->CheckIdlesse();
		BOOL Running=FALSE;
		for(i=0;i<ProcessCount;i++){//检查所有任务是否已完成
			Running+=((CProcess*)ProcessList.GetAt(i))->ProcessIsRunning();
		}
		if(!Running) break;
		TimePassed++;	//增加时间计数
	}
	return TRUE;
}

void GetModuleFilePath(){
	char drive[_MAX_DRIVE],dir[_MAX_DIR],fname[_MAX_FNAME],ext[_MAX_EXT];
	char tmp_path[MAX_PATH];
	::GetModuleFileName(NULL,tmp_path,MAX_PATH);
	_splitpath(tmp_path,drive,dir,fname,ext);
	ModuleFilePath=drive;
	ModuleFilePath+=dir;
}
void OutputResult(){
	CreateDirectory("Result",NULL);
	typedef struct his{
		CString devType;
		int useTime;
	}DevUseRatio;
	fstream fout;
	DevUseRatio* ratio=NULL;
	CPtrArray ratioArray;
	char drive[_MAX_DRIVE],dir[_MAX_DIR],fname[_MAX_FNAME],ext[_MAX_EXT];
	_splitpath(FileName,drive,dir,fname,ext);
	CString OutFileName="Result\\";
	OutFileName+=fname;
	OutFileName.Replace(".","_");
	OutFileName+="_Result.txt";
	fout.open(OutFileName,ios::out);
	BOOL bMultiply=ProcessList.GetSize()-1;
	int DeviceCount=DeviceList.GetSize();
	if(bMultiply) fout<<"<output>"<<endl;
	for(int i=0;i<DeviceCount;i++){
		CDevice* dev=NULL;
		dev=(CDevice*)DeviceList.GetAt(i);
		ratio=new DevUseRatio;
		ratioArray.Add(ratio);
		if(bMultiply) fout<<"<unit>"<<endl;
		if(bMultiply) fout<<"<resource>"<<dev->GetDeviceType()<<"</resource>"<<endl;
		ratio->devType=dev->GetDeviceType();
		ratio->useTime=0;
		int StepCount=dev->GetHistory()->GetSize();
		for(int j=0;j<StepCount;j++){
			if(bMultiply) fout<<"<phase>";
			int stime,etime;
			CString proName;
			stime=(((CUseDevice*)dev->GetHistory()->GetAt(j))->GetStartTime());
			etime=(((CUseDevice*)dev->GetHistory()->GetAt(j))->GetEndTime());
			proName=(((CUseDevice*)dev->GetHistory()->GetAt(j))->GetProcessName());
			if(bMultiply) fout<<stime<<","<<etime<<","<<proName<<"</phase>"<<endl;
			if("null"!=proName) ratio->useTime+=(etime-stime);
		}
		if(bMultiply) fout<<"</unit>"<<endl;
	}
	while(ratioArray.GetSize()){
		fout<<"<result>"<<endl;
		ratio=(DevUseRatio*)ratioArray.GetAt(0);
		ratioArray.RemoveAt(0);
		fout<<"<name>"<<ratio->devType<<"</name>"<<endl;
		fout<<"<ratio>"<<(int)(ratio->useTime*100.0/TimePassed+0.5)<<"</ratio>"<<endl;
		fout<<"</result>"<<endl;
		delete ratio;
		ratio=NULL;
	}
	fout<<"</output>";
	fout.close();
	cout<<"运行成功,结果已输出到: .\\"<<OutFileName<<endl;
}

void Clear(){
	while (ProcessList.GetSize()){
		CProcess* pro;
		pro=(CProcess*)ProcessList.GetAt(0);
		ProcessList.RemoveAt(0);
		delete pro;
	}
	while(DeviceList.GetSize()){
		CDevice* dev;
		dev=(CDevice*)DeviceList.GetAt(0);
		DeviceList.RemoveAt(0);
		delete dev;
	}
}

⌨️ 快捷键说明

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