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

📄 os2.cpp

📁 对于给定的一组进程
💻 CPP
字号:
//os2.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include<windows.h>
#include <iostream>
#include<string>
using namespace std;
class Process;
Process *p;
int CurrentTime;
int n;
void Swap();
void Display();
class Process
{
public:
	string Name;
	int prior;
	int submittime;
	int runtime;
	bool run;
	Process()
	{
		run = false;
	}
	bool Execute(int timeslice)
	{
		if(runtime <= 0)
		{
			return true;
		}
		else
		{
			cout << Name << " is running !" << endl;
			run = true;
			if(runtime <= timeslice)
			{
				Sleep(runtime*1000);
				runtime = 0;
				CurrentTime += runtime;
				Display();
				cout << Name << " has finished ! "<<endl;
			}
			else
			{
				Sleep(timeslice*1000);	
				runtime = runtime-timeslice;
				CurrentTime += timeslice;
				Display();
				cout << "running time : " << timeslice << endl;
			}
			run = false;
			prior += 3;
			cout << "Now timeslice is coming!" << endl;
			Swap();
			return false;
		}
	}
};
void Swap()
{	
	for(int i = 0; i < n-1 ; i++)
	{
		for(int j = i+1; j < n; j++)
		{
			if(p[i].prior > p[j].prior)
			{
				Process temp;
				temp = p[j];
				p[j] = p[i];
				p[i] = temp;
			}

		}
	}
}
void Display()
{
	cout << "Stack's process :" << endl;
	cout << "Name" << "            "<<"runtime" <<"            "<< "prior"<<endl;
	for(int i = 0; i < n ; i++)
	{
		if((p[i].run == false)&& (p[i].submittime <= CurrentTime) && p[i].runtime >0)
		{
			cout << p[i].Name << "      "<<p[i].runtime <<"      "<<p[i].prior <<endl;
		}
	}
}
int main(int argc, char* argv[])
{
	
	cout << "Input process number:\n";
	cin>> n;
	p = new Process[n];
	cout << "Input process name and submittime and runtime and prior:\n";
	for(int i = 0; i < n; i++)
	{
		cin >> p[i].Name >> p[i].submittime >> p[i].runtime>> p[i].prior;
	}
	Swap();
	CurrentTime = 0;
	for(i = 0; i < n; i++)
	{
		if(p[i].submittime <= CurrentTime && p[i].runtime > 0)
		{
			p[i].Execute(3);
			i = -1;
		}
	}
	cout << "All has finished !" << endl;
	return 0;
}

⌨️ 快捷键说明

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