进程调度算法

<p>设计一个按先来先服务,算法时间片轮转法,优先数调度算法实现处理器调度的程序。</p>
#include<iostream>//main.cpp
#include<windows.h>
#include"process.h"
#include"fcfs.h"
#include"timeSlice.h"
#include"priority.h"
using namespace std;
void pause()
{
	system("pause");getchar();system("cls");
}
int main()
{
	int n=2;
	while(1)
	{
		cout<<endl<<endl<<endl<<endl;
		cout<<"\t\t****************************************"<<endl;
		cout<<"\t\t*\tFunction List:                 *"<<endl;
		cout<<"\t\t*                                      *"<<endl;
		cout<<"\t\t*\t1.First Come Firse Service     *"<<endl;
		cout<<"\t\t*\t2.TimeSlice Circulation        *"<<endl;
		cout<<"\t\t*\t3.Priority                     *"<<endl;
		cout<<"\t\t*\t0.Quit                         *"<<endl;
		cout<<"\t\t*                                      *"<<endl;
		cout<<"\t\t****************************************"<<endl<<endl;
		cout<<"\t\tMake a selection:"<<endl;
		cin>>n;
		if(!n)return 0;
		system("cls");
		getchar();
		switch(n)
		{
		case 1:fcfs();pause();break;
		case 2:TimeSlice();pause();break;
		case 3:choice();pause();break;
		default:
			cout<<"Wrong Input,Please try again"<<endl;
			system("pause");getchar();system("cls");break;
		}
	}
	return 0;
}
class Process//process.h
{
private:<pre name="code" class="cpp">#include<list>//fcfs.h
#include<iostream>
#include<iomanip>
using namespace std;
list<Process>process;
list<Process>::iterator it,it2;
void fcfsInput()
{
	char name[20];
	float at,st;
	int n=0;
	cout<<"How many Processes?"<<endl;
	cin>>n;
	while(n--)
	{
		cout<<"Input name,arrivalTime&serviceTime"<<endl;
		cin>>name>>at>>st;
		process.push_back(Process(name,at,st));
	}
}
void fcfsFunction()
{
	it=process.begin();
	it->setBT(it->getAT());
	it->setACT(it->getAT(),it->getST());
	it++;
	for(it2=process.begin();it!=process.end();it++,it2++)
	{
		if(it->getAT()>it2->getACT())
			it->setBT(it->getAT());
		else 
			it->setBT(it2->getACT());
		it->setACT(it->getBT(),it->getST());
	}
	for(it=process.begin();it!=process.end();it++)
	{
		it->setRT(it->getACT(),it->getAT());
		it->setRTWW(it->getRT(),it->getST());
	}
}
void fcfsPrint()
{
	cout<<"Execution Sequence:"<<endl;
	cout<<"Direction:smaller -> bigger"<<endl;
	for(it=process.begin();it!=process.end();it++)
		cout<<it->getName()<<"  ";
	cout<<endl<<"Process Information :"<<endl<<setw(10)<<"Name"<<setw(10)<<"Arrival"<<setw(10)<<"Service"
		<<setw(8)<<"Begin"<<setw(13)<<"Accomplish"<<setw(6)<<"Run"<<setw(16)<<"RunWithWeight"<<endl;
	for(it=process.begin();it!=process.end();it++)
		cout<<setw(10)<<it->getName()<<setw(10)<<it->getAT()<<setw(10)<<it->getST()<<setw(8)<<it->getBT()
		<<setw(13)<<it->getACT()<<setw(6)<<it->getRT()<<setw(16)<<it->getRTWW()<<endl;
}
void fcfs()
{
	fcfsInput();
	process.sort(cmp);
	fcfsFunction();
	fcfsPrint();
}

#include<iostream>//priority.h
#include<vector>
#include<list>
using namespace std;
vector<Process>process2;
vector<int>vi;
int nTask1=0;
void input()
{
	int n,pri,at,st;
	char name[20];
	cout<<"How many process?"<<endl;
	cin>>n;
	nTask1=n;
	while(n--)	
	{
		cout<<"Input process name,priority,arrivalTime&serviceTime"<<endl;
		cin>>name>>pri>>at>>st;
		process2.push_back(Process(name,pri,at,st));
	}
}
void priShow()
{
	cout<<endl<<setw(10)<<"Name"<<setw(20)<<"serviceTime lefted"<<endl;
	for(int i=0;i<nTask1;i++)
		cout<<setw(10)<<process2[i].getName()<<setw(20)<<process2[i].getST()<<endl;
}
bool judge1()
{
	int i=0,j=0;
	for(;i<nTask1;i++)
		if(process2[i].getST()==0)
			j++;
	if(nTask1==j)
		return false;
	return true;
}
void enque(int i,int flag1[])
{
	int j;
	for(j=0;j<nTask1;j++)
		if(process2[j].getAT()==i&&flag1[j]==0)
			vi.push_back(j),flag1[j]=1;
	sort(vi.begin(),vi.end());
}
void func_preempt()
{
	int i,runningTask1=-1;
	int *flag1=new int[nTask1];
	for(i=0;i<nTask1;i++)
		flag1[i]=0;
	for(i=process2[0].getAT();judge1();i++)
	{
		enque(i,flag1);
		if(runningTask1==-1&&vi.size()!=0)
		{
			runningTask1=vi.back();
			vi.pop_back();
		}
		if(runningTask1!=-1)
		{
			process2[runningTask1].setST(1);
			priShow();
			if(process2[runningTask1].getST()==0)
				runningTask1=-1;
			else
			{
				vi.push_back(runningTask1);
				runningTask1=-1;
				flag1[runningTask1]=0;
			}
		}
	}
}
void func_nonpreempt()
{
	int i,runningTask1=-1;
	int *flag1=new int[nTask1];
	for(i=0;i<nTask1;i++)
		flag1[i]=0;
	for(i=process2[0].getAT();judge1();i++)
	{
		enque(i,flag1);
		if(runningTask1==-1&&vi.size()!=0)
		{
			runningTask1=vi.back();
			vi.pop_back();
		}
		if(runningTask1!=-1)
		{
			process2[runningTask1].setST(1);
			priShow();
			if(process2[runningTask1].getST()==0)
				runningTask1=-1;
		}
	}
}
void non_preempt()
{
	input();
	sort(process2.begin(),process2.end(),cmp);
	func_nonpreempt();
}
void preempt()
{
	input();
	sort(process2.begin(),process2.end(),cmp);
	func_preempt();
}
void choice()
{
	int choice;
	cout<<endl<<endl<<endl<<endl<<endl<<"\t\t\t*****************************"<<endl;
	cout<<"\t\t\t*     make a choice         *"<<endl;
	cout<<"\t\t\t*    1.Preempt way          *"<<endl;
	cout<<"\t\t\t*    2.Non-preempt way      *"<<endl;
	cout<<"\t\t\t*****************************"<<endl;
	cin>>choice;
	system("cls");
	switch(choice)
	{
		case 1:preempt();break;
		case 2:non_preempt();break;
		default:cout<<"wrong input"<<endl;break;
	}
}

#include<iostream>//timeSlice.h
#include<vector>
#include<queue>
#include<iomanip>
using namespace std;
vector<Process>process1;
queue<int>que;
int nTask=0,timeSlice=0;
bool judge()
{
	int i=0,j=0;
	for(;i<nTask;i++)
		if(process1[i].getST()==0)
			j++;
	if(nTask==j)
		return false;
	return true;
}
void timesliceInput()
{
	char name[20];
	float at,st;
	int n=0;
	cout<<"How many Processes?"<<endl;
	cin>>n;
	cout<<"What about the timeSlice?"<<endl;
	cin>>timeSlice;
	nTask=n;
	while(n--)
	{
		cout<<"Input name,arrivalTime&serviceTime"<<endl;
		cin>>name>>at>>st;
		process1.push_back(Process(name,at,st));
	}
}
void timesliceShow()
{
	cout<<endl<<setw(10)<<"Name"<<setw(20)<<"serviceTime lefted"<<endl;
	for(int i=0;i<nTask;i++)
		cout<<setw(10)<<process1[i].getName()<<setw(20)<<process1[i].getST()<<endl;
}

void enqueue(int i,int flag[])
{
	for(int j=0;j<nTask;j++)
		if(i==process1[j].getAT()&&flag[j]==0)
			que.push(j),flag[j]=1;
}
void timesliceFunction()
{
	int i,ts=timeSlice,runningTask=-1;
	int *flag=new int[nTask];
	for(i=0;i<nTask;i++)
		flag[i]=0;
	for(i=process1.begin()->getAT();judge();i++)
	{
		enqueue(i,flag);
		if(runningTask==-1&&que.size()!=0)
		{
			runningTask=que.front();
			que.pop();
			ts=timeSlice;
		}
		if(runningTask!=-1)
		{
			process1[runningTask].setST(1);
			if(process1[runningTask].getST()==0&&ts)
			{
				ts=timeSlice;
				runningTask=-1;
				timesliceShow();
				continue;
			}
			ts--;
		}	
		timesliceShow();
		if(!ts)
		{
			if(runningTask!=-1&&process1[runningTask].getST()>0)
			{
				enqueue(i+1,flag);
				que.push(runningTask);
			}
			ts=timeSlice;
			runningTask=-1;
		}
	}
}
void TimeSlice()
{
	timesliceInput();
	sort(process1.begin(),process1.end(),cmp);
	timesliceFunction();
}

char name[20];float arrivalTime;float serviceTime;float beginTime;float accomplishTime;float runTime;float runTimeWithWeight;float priority;public:Process(char *n,float at,float st){strncpy(name,n,20);arrivalTime=at;serviceTime=st;}Process(char *n,float pri,float at,float st){strncpy(name,n,20);priority=pri;arrivalTime=at;serviceTime=st;}void setST(){serviceTime=0;}void setST(int ts){serviceTime-=ts;}void setBT(float at){beginTime=at;}void setACT(float at,float st){accomplishTime=at+st;}void setRT(float act,float at){runTime=act-at;}void setRTWW(float rt,float st){runTimeWithWeight=rt/st;}char *getName(){return name;}float getAT(){return arrivalTime;}float getST(){return serviceTime;}float getBT(){return beginTime;}float getACT(){return accomplishTime;}float getRT(){return runTime;}float getRTWW(){return runTimeWithWeight;}float getPri(){return priority;}};bool cmp(Process &a,Process &b)
 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值