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

📄 lin.txt

📁 操作系统
💻 TXT
字号:
#include<iostream>
#include<string>
#include <assert.h>
#include<queue>
using namespace std; 
class dispatch{
	friend std::istream& operator>>(std::istream& input,dispatch& Dispatch);//友元函数dispatch输入流
	friend std::ostream& operator<<(std::ostream& output,dispatch& Dispatch);//友元函数dispatch输出流
private:
	int count;  //定义变量表示进程process的个数
	bool SF_FC;//标志选择的调度算法类型
	double Ti,Tse,Wi;//总周转时间和总带权周转时间
	typedef bool (*compare_type)(process&,process&);//定义指向函数的指针,实现不同调度算法选择的实现
	compare_type compare_1,compare_2;//定义指向函数的指针,实现不同调度算法选择的实现
	priority_queue<process,vector<process>,compare_type> PQueue;//定义按到达时间顺序的队列PQueue
	priority_queue<process,vector<process>,compare_type> PQueue1;//定义已到达并按短进程优先(或称为“就绪队列”)所排列的队列PQueue1
	queue<process> Queue;//定义此队列表示完成的进程
public:
	dispatch(bool sf_fc = true):count(0),SF_FC(sf_fc),Ti(0),Tse(0),Wi(0) {
		compare_1 = &compare_FSFC;
		compare_2 = &compare_SF;
		if (sf_fc == false){//当给定的标志为真时,表示FSFC调度算法
			PQueue = priority_queue<process,vector<process>,compare_type>(compare_1);
			PQueue1 = priority_queue<process,vector<process>,compare_type>(compare_2);
			cout<<"选择的是最短进程优先(抢占式)调度算法"<<endl;
		}
		else{
			PQueue = priority_queue<process,vector<process>,compare_type>(compare_1);
			PQueue1 =priority_queue<process,vector<process>,compare_type>(compare_1);
			cout<<"选择的是先来先服务调度算法"<<endl;
		}
	}
	void FSFC(){//先来先服务的实现...
		int i =0;
		double atime ; //标志当前内存中进程结束的时间点
		process *Pro1 = &PQueue.top();// 取队首元素
		atime = (*Pro1).GETstart_time();
		while(i < count)
		{
			(*Pro1).Set_bvisited();
			cout<<(*Pro1)<<"  ";
			++i;
			atime=atime+(*Pro1).GETrun_time(); 
			(*Pro1).SETend_time(atime);
			if((*Pro1).Get_bvisited()){//如果当前进程已经完成服务功能
				// Ti = Ti  + (*Pro1).GETrun_time();
				Tse = Tse +((*Pro1).GETend_time()-(*Pro1).GETstart_time());
				Wi = Wi + ((*Pro1).GETend_time()-(*Pro1).GETstart_time())/((*Pro1).GETrun_time());
				Queue.push(*Pro1);
			}
			else 
				cout<<"error!"<<endl;
			PQueue.pop();//删除队列中的队首元素
			if(PQueue.empty() == false)
				process *Pro1 = &PQueue.top();// 取队首元素
		}
	}
	void Short_No1(){//抢占式短进程优先的实现...
		int i=count,m=count,j=0;//标志PQueue中的进程数,标志就绪队列PQueue1中的进程数
		double atime=0,btime=0,ctime=0,dtime=0;//前者表示当前进程若不被抢占内存则完成的时间点,后者表示当前进程还需要多少时间长度才可以完成,ctime表示内存中进程开始此次运行的时间
		process *ptr1 = &PQueue.top();
		process *ptr2,*ptr3;
		PQueue1.push(*ptr1);
		PQueue.pop();
		ptr2 = &PQueue.top(); //察看PQueue的队首元素
		ptr1 = &PQueue1.top();
		dtime = (*ptr1).GETrun_time();
	    (*ptr1).SETend_time(dtime);
		ctime = ctime + (*ptr1).GETstart_time();//赋予第一个进程开始执行的时间点ctime
		//取出PQueue1就绪队列的队首元素,即假设此进程在内存中运行
		while(m>0){
			atime = ctime+(*ptr1).GETstart_time();
			if((*ptr2).GETstart_time()<atime){
				btime =atime - (*ptr2).GETstart_time();
				(*ptr1).SETend_time(btime);
				 dtime = (*ptr2).GETrun_time();
				(*ptr2).SETend_time(dtime);
				while((*ptr2).GETstart_time()<atime &&(PQueue.empty() == false) ){//若PQueue队列中有进程要进入就绪队列,即其到达时间在内存中进程执行完时间点之前的情况
					cout<<"内部3问题"<<endl;
					PQueue1.push(*ptr2);//把该进程插入PQueue1就绪队列
					PQueue.pop();//把该满要进入就绪队列的进程从PQueue队列中取出并删除原有信息
					--i;
					if(PQueue.empty() == false){
						ptr2 = &PQueue.top();//增量,控制循环...
						dtime = (*ptr2).GETrun_time();
						(*ptr2).SETend_time(dtime);
					}
					else
						break;
				}
				//ptr1 = &PQueue1.top();
				continue ;
			}
			else{//若没有找到优先级更高的进程,则占内存的进程便执行完全
				btime = 0;
				ctime = atime;    //此进程的结束时间点,可以是其他进程的开始点
				ptr1 = &PQueue1.top();
				cout<<*ptr1<<".....";
				Tse = atime - (*ptr1).GETstart_time() + Tse;
				Wi = Wi + (atime - (*ptr1).GETstart_time() )/((*ptr1).GETrun_time());
				(*ptr1).Set_bvisited(true);  //设置结束标志
				(*ptr1).SETend_time(atime);  //设置结束时间点
				cout<<(*ptr1).GETend_time();
				Queue.push(*ptr1);     //插入完成队列Queue末尾
				--m;
				PQueue1.pop();
				if(PQueue1.empty() == false){//执行完内存中进程后选择新进程
						ptr3 = &PQueue1.top();
						--j;
						ptr1 = ptr3;
						cout<<"&&&&&&&&"<<endl;

				}
				else{
					if(!PQueue.empty()){
						ptr2 = &PQueue.top();
						PQueue1.push(*ptr2);
						PQueue.pop();
						--i;
						ptr1 = &PQueue1.top();
						ptr2 = &PQueue.top();
						cout<<"********"<<endl;
					}
					else{
						return ;   //控制退出调度算
						}
				}
			}
		}
	}
	void print(){
		process *ptr;
		int i = 1;
		//cout<<"进程调度信息如下:"<<endl;
		cout<<" **运行顺序*****进程名*****到达时间*****运行时间*****结束时间*****"<<endl;
		while (Queue.empty() == false){
			ptr = &Queue.front();
			cout<<"**"<<i<<"*****"<<(*ptr).GETname();
			cout<<"*****"<<(*ptr).GETstart_time();
			cout<<"*****"<<(*ptr).GETrun_time();
			cout<<"*****"<<(*ptr).GETend_time()<<"*****"<<endl;
			++i;
			Queue.pop();
		}
	}
};
std::istream& operator>>(std::istream& input,dispatch& Dispatch){
	process Pro1;
	int count1=0;
	while(input>>(Pro1)){
		Dispatch.PQueue.push(Pro1);
		++count1;
	}
	Dispatch.count = count1;
	cout<<"共有进程数为 "<<Dispatch.count;
	return input;
}
std::ostream& operator<<(std::ostream& output,dispatch& Dispatch){
	int i=1;
	output<<"共有的进程数目:"<<Dispatch.count<<endl;
	output<<"进程名     到达时间     运行时间     "<<endl;
	while(i<= Dispatch.count){
		cout<<"...."<<endl;
		output<<Dispatch.PQueue.top()<<endl;
		Dispatch.PQueue.pop();
		++i;
		cout<<"....."<<endl;
	}
	cout<<"*******"<<endl;
	return output;
}
	
	

⌨️ 快捷键说明

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