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

📄 proexc.cpp

📁 里面有5个关于操作系统进程调度的算法源码
💻 CPP
字号:
// ProExc.cpp: implementation of the CProExc class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ProExc.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CProExc::CProExc()
{
	first = 0;
	last = 0;
	time = 0;
	proTotal = 0;
	timeTotal = 0;
}

CProExc::~CProExc()
{
	CPCB *ppcb = 0;
	while (first != 0)
	{
		ppcb = first;
		first = ppcb->next;
		delete ppcb;
	}
}

bool CProExc::InitializeByFile(char *path)
{
	FILE *pFile;
	int i;
	CPCB *temp = 0;
	if ((pFile = fopen(path, "r")) == 0)
	{
		return false;
	}
	else
	{
		fscanf(pFile, "%d\n", &proTotal);
		for (i = 0; i < proTotal; i++)
		{
			temp = new CPCB;
			fscanf(pFile,"%s%d%d%d", 
					&temp->name, &temp->ntime, 
					&temp->ptime, &temp->super);
			timeTotal += temp->ntime;
			if (first == 0)
			{
				first = temp;
				last = first;
			}
			else
			{
				last->next = temp;
				last = last->next;
			}
		}
	}
	return true;
}


void CProExc::Start()
{
	if (!InitializeByFile("in.txt"))
	{
		cout << "文件打开错误..." << endl;
		return;
	}
	else
	{
		CPCB *min = 0;
		CPCB *temp = 0;
		int i, iden;
		for (i = 0; i < timeTotal; i++)
		{
			min = first;
			temp = first;
			iden = 1;
			do 
			{
				if (temp->state == 'W' && temp->ptime <= time)
				{
					if (iden)
					{
						min = temp;
						iden = 0;
					}
					else if (temp->super > min->super || (temp->super == min->super && temp->ptime < min->ptime))
					{
						min = temp;
					}
				}
				temp = temp->next;
			} while(temp != 0);
			if (iden)
			{
				i--;
				printf("\ntime=%d:\t无作业提交...wait...\n", time);
				cout << "按任意键继续..." << endl;
				getch();
				time++;
				if(time > 100)
				{
					printf("\n等待时间过长...error...\n");
				}
			}
			else
				running(min);
		}
		cout << endl << "所有进程运行完毕,按任意键继续..." << endl;
		getch();
	}
}

void CProExc::running(CPCB *ppcb)
{
	ppcb->state = 'R';
	displayPro(ppcb);
	time++;
	ppcb->rtime++;
	ppcb->super--;
	if (ppcb->ntime == ppcb->rtime)
	{
		cout << ppcb->name << " 运行完毕..." << endl;
		ppcb->state = 'F';
	}
	else
		ppcb->state = 'W';
	cout << "按任意键继续..." << endl;
	getch();
}

void CProExc::displayPro(CPCB *ppcb)
{
	CPCB *temp = 0;
	cout << endl << "time = " << time << endl;
	cout << "进程名\t状态\t提交\t需执行\t已运行\t优先级" << endl;
	cout << ppcb->name << "\t" << ppcb->state
		 << "\t" << ppcb->ptime << "\t" << ppcb->ntime << "\t"
		 << ppcb->rtime << "\t" << ppcb->super << endl;
	temp = first;
	do 
	{
		if (temp->state == 'W')
		{
			cout << temp->name << "\t" << temp->state
					<< "\t" << temp->ptime << "\t" << temp->ntime << "\t"
					<< temp->rtime << "\t" << temp->super << endl;
		}
		temp = temp->next;
	} while(temp != 0);
}

⌨️ 快捷键说明

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