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

📄 作业调度.cpp

📁 作业调度算法
💻 CPP
字号:
// 作业调度.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
using namespace std;

#define MEM 64
#define TAPE 4
#define PT 2
typedef struct jcb
{
	char name[4]; /*作业名*/
	int length; /*作业长度,所需主存大小*/
	int printer; /*作业执行所需打印机的数量*/
	int tape; /*作业执行所需磁带机的数量*/
	int runtime; /*作业估计的执行时间*/
	int waittime; /*作业在输入井中的等待时间*/
	struct jcb *next;/*指向下一个作业控制块的指针*/
}JCB; /*作业控制块类型定义*/

typedef struct cu
{
	int printer;//打印机
	int tape;//磁带机
	int mem;
	struct jcb *header;/*后备队列*/
}CU; /*作业控制块类型定义*/

void jinit(cu *jcu)//输入井后备队列初始化
{
	int n;
	jcb tmp,*tp,*head=new jcb();
	//char tt[15];
	//tmp=new pcb();
	
	tp=head;
	tmp.next=NULL;
	tmp.waittime =0;
	cout<<"-----------批处理系统中作业调度 ----------"<<endl;
	cout<<"输入井作业数目:";
	cin>>n;
	cout.flush();	
	for(int i=0;i<n;i++)
	{
		cout<<"作业名:";		
		head->next =new jcb();		
		cin>>tmp.name;		
	
		cout<<"作业大小:";
		cin>>tmp.length;
		
		cout<<"所需打印机数:";
		cin>>tmp.printer;
		cout<<"所需磁带机数:";
		cin>>tmp.tape;
		cout<<"作业估计执行时间:";
		cin>>tmp.runtime;
		cout<<endl;
		cout.flush();
		*head->next =tmp;
		head=head->next;
		//head=new pcb();
	}
	jcu->header=tp->next ;
	return;
}
jcb* jsort(cu *jcu)//选择高响应比作业进入内存
{
	jcb *tmp,*cur,*pre,*head=jcu->header;
	jcu->header=NULL;
	float tp,hp;
	//pre=head;
	if(head!=NULL)
	{
		tmp=head;
		if(head->next ==NULL)
		{
			//head->next =jcu->header;
			//jcu->header=head;
			return tmp;
		}
		head=head->next ;
		pre=head;
		cur=head;
		tmp->next =NULL;
		tp=(tmp->runtime +tmp->waittime )/tmp->runtime ;
		hp=(cur->runtime +cur->waittime )/cur->runtime ;
		if(tp <hp)
		{
			tmp->next =cur->next ;
			cur->next =NULL;
			head=tmp;
			tmp=cur;
			pre=head;
			//cur=pre->next;
		}
		cur=pre->next ;
		while(cur!=NULL)
		{
			tp=(tmp->runtime +tmp->waittime )/tmp->runtime ;
			hp=(cur->runtime +cur->waittime )/cur->runtime ;
			if(tp<hp)
			{
				tmp->next =cur->next ;
				cur->next =NULL;
				pre->next =tmp;
				tmp=cur;
			}
			pre=pre->next ;
			cur=pre->next;
		}
		jcu->header=head;
		return tmp;
	}
	return NULL;
}
void jout(jcb *job,cu *jcu)
{
	if((jcu->mem -job->length)>=0&&(jcu->printer -job->printer)>=0&&(jcu->tape -job->tape)>=0)
	{
		cout<<"调入作业:"<<job->name<<"进入内存."<<endl;
		cout<<"资源剩余(内存:"<<jcu->mem -job->length <<"\t打印机:"<<jcu->printer -job->printer <<"\t磁带机:"<<jcu->tape -job->tape <<endl;
	}
	else 
	{
		cout<<"调入作业:"<<job->name<<"进入内存."<<endl;
		cout<<"资源不足,调度失败,该作业无法完成!"<<endl;
	}
}
void jrun1(cu *jcu)
{
	jcb *cur=jsort(jcu);
	if(cur==NULL){cout<<"作业调度结束."<<endl;return ;}
	int time=cur->runtime ;
	jcb *head=jcu->header;
	while(head!=NULL)
	{
		head->waittime+=time;
		head=head->next ;
	}
	jout(cur,jcu);
}
void jrun(cu *jcu)
{
	jinit(jcu);
	while(jcu->header !=NULL)
		jrun1(jcu);
}
int main(int argc, char* argv[])
{
	cu jcu;
	jcu.header=NULL;
	jcu.printer=PT;
	jcu.tape=TAPE;
	jcu.mem=MEM;
	jrun(&jcu);
	//printf("Hello World!\n");
	return 0;
}

⌨️ 快捷键说明

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