Скачиваний:
35
Добавлен:
01.05.2014
Размер:
3.47 Кб
Скачать


#include <iostream>

using namespace std;


int globaltime = 0;
int n;



class oneprocess
{

public:
	int stime;
	int burst;
	int history[150];
	int priority;

	oneprocess()
	{
		this->burst=0;
		this->stime=0;
		this->priority=0;
	}

	oneprocess(int burst,int stime = 0, int priority=0)
		{
			this->stime=stime;
			this->burst=burst;
			this->priority=priority;
			for (int i = 0; i<150; i++)
			{
				this->history[i]=0;
			}
		}
	~oneprocess() {};

	void run(int gtime){
		this->burst--;
		this->history[gtime]=2;
		}

	void wait(int gtime){
		if ((this->burst>0)&&(this->stime<gtime))
			{this->history[gtime]=1;}
		}

};




int fifo(oneprocess processes[], int gtime, int pri=10)
{
	oneprocess *cur;
	for(int i=0;i<n;i++)
	{
		cur=&processes[i];
		if ((cur->burst>0)&&(cur->stime<gtime)&&(cur->priority<=pri))
			{

			return i;
			}

	}

	return -1;
}

int rr(oneprocess processes[],int gtime, int pri=10)
{
	oneprocess *cur;
	int last;
	last=-1;

	for(int k=0;k<n;k++)
	{
		cur=&processes[k];
		if(cur->history[gtime-1]==2)
		{
			last=k;
		}
	}

	for(int i=0;i<n;i++)
	{
		cur=&processes[i];
		if ((cur->burst>0)&&(cur->stime<gtime)&&(last<i)&&(cur->priority<=pri))
		{
			return i;

		}

	}
	for(int j=0;j<n;j++)
	{
		cur=&processes[j];
		if ((cur->burst>0)&&(cur->stime<gtime)&&(cur->priority<=pri))
		{
			return j;
		}

	}
	return -1;
}

int mq(oneprocess processes[], int gtime)
	{

	oneprocess *cur;
	int min_pri;
	min_pri=3;

	for(int i=0;i<n;i++)
	{

		cur=&processes[i];
		if ((cur->priority < min_pri)&&(cur->burst>0)&&(cur->stime<gtime))
			{
			min_pri = cur->priority;

			}

	}

	int res =-1;

		if(min_pri==0)
		{res = fifo(processes,gtime,0);
		if (res != -1) {return res;}}
		if (min_pri==1)
		{res =  rr(processes,gtime,1);
		if (res != -1) {return res;}}
		if(min_pri==2)
		{res =  fifo(processes,gtime,2);
		if (res != -1) {return res;}}
		if (min_pri>=3)
		{res =  rr(processes,gtime,3);
		if (res != -1) {return res;}}
	return -1;
	}


void run(int time, int alg, oneprocess processes[])
{
	int cur_proc;
	for(int gtime=0;gtime<time;gtime++)
	{
		if (alg==0)
		{
			cur_proc=fifo(processes,gtime);}
		else if (alg==1)
		{cur_proc=rr(processes,gtime);}
		else {cur_proc=mq(processes,gtime);}


		for(int c=0;c<n;c++)
		{if (c==cur_proc)
			{processes[c].run(gtime);}
		else {processes[c].wait(gtime);}

		}
	}
}

void show_history(oneprocess processes[])
{
for(int i=0;i<n;i++)
	{
		cout<<"Process "<<i+1<<" Priority "<<processes[i].priority<<"  ";
		for(int j=2;j<50;j++)
		{cout<<processes[i].history[j];}
		cout<<endl;
	}
}


int main()
{

	n=5;
	oneprocess procs[5];
	for(int i=0;i<n;i++)
	{
			int boost, starttime, prior;
			cout << "Enter value "<<i+1<<" of "<< n<<" processes:"<<endl;
			cout << "Cpu_burst:";
			cin >> boost;
			cout << "Start_time:";
			cin >> starttime;
			cout << "Prioritet:";
			cin >> prior;

			if (prior<0) {
				prior = 0;
			}
			if (prior>3){
				prior = 3;
			}
			oneprocess np(boost,starttime,prior);
			procs[i]=np;
		}
	cout << "Enter name algorithm"<<endl;
	cout << "0- FIFO; 1-RobinRound; 2- Multilevel Queue"<<endl;
	int name, tmp;
	cin >> name;

	run(50,name,procs);
	cout<<"Results:"<<endl;
	show_history(procs);
	cout << "press any key" << endl;
	cin >> tmp ;
	return 0;
}
Соседние файлы в папке Планирование процессов