📄 批处理系统的作业调度.cpp
字号:
//vc++ 6.0
#include <iostream>
#include <vector>
using namespace std;
int sysmemory=64*1024, sysprinter=4, systape=2;
typedef struct jcb
{
char name[5]; //作业名
int length; //作业长度,所需主存大小
int printer; //作业执行所需打印机的数量
int tape; //作业执行所需磁带机的数量
int runtime; //作业估计执行时间
int waittime; //作业在系统中的等待时间
int next; //指向下一个作业控制块的指针
} JCB ;
//#define n 10 //假定系统中可容纳的作业数量为n
JCB jobtable[10]; //作业表
//int jobcount;
int head; //现在后备作业头;
int runhead; //现在的运行作业;
struct Avaliable
{
int mememory;
int printer;
int tape;
} cur;
void Done();
int Select();
void Init();
void FinishJob();
void FinishAll();
int main (void)
{
Init();
while(1)
{
Select();
}
return 0;
}
void Init()
{
head = 0;
runhead = -1;
int jobcount = 0;
cur.mememory = sysmemory;
cur.printer =sysprinter;
cur.tape = systape;
cout << "Enter the number of jobs you want to put in( 2~10):\n";
cin >> jobcount;
while (jobcount >10 || jobcount <2)
{
cout << "Invaliad number of jobs.Please enter a new one (2~10):\n";
cin >> jobcount;
}
for (int i =0;i<jobcount;i++)
{
cout<<"Enter the JOBNAME of NO."<<i+1<<endl;
cin>>jobtable[i].name;
jobtable[i].name[4]='\0';
cout<<"Enter the job's LENGTH(1~65536)"<<endl;
cin>>jobtable[i].length;
while(jobtable[i].length<=0||jobtable[i].length>sysmemory){
cout<<"Enter error!"<<"\nInvaliad length.Please enter a new one (1~65536):\n";
cin>>jobtable[i].length;
}
cout<<"Enter how many PRINTERS the job need(<=4)"<<endl;
cin>>jobtable[i].printer;
while(jobtable[i].printer>4|| jobtable[i].printer<0){
cout<<"Enter error!"<<"\nInvaliad number of needed printers.Please enter a new one (0~4):\n";
cin>>jobtable[i].printer;
}
cout<<"Enter how many TAPES the job need(<=2)"<<endl;
cin>>jobtable[i].tape;
while(jobtable[i].tape>2 || jobtable[i].tape<0){
cout<<"Enter error!"<<"\nInvaliad number of needed tapes.Please enter a new one (0~2):\n";
cin>>jobtable[i].tape;
}
cout<<"Enter the RUNTIME that the job may run(>0):\n";
cin>>jobtable[i].runtime;
while(jobtable[i].runtime<1){
cout<<"Enter error!"<<"\nInvaliad runtime.Please enter a new one:\n";
cin>>jobtable[i].runtime;
}
cout<<"Enter the time the job has waited(>=0):\n";
cin>>jobtable[i].waittime;
while(jobtable[i].waittime<0){
cout<<"Enter error!"<<"\nInvaliad waitingtime.Please enter a new one:\n";
cin>>jobtable[i].waittime;
}
jobtable[i].next = i+1;
}
jobtable[jobcount-1].next = -1;
}
int Select()
{
int now;
vector<int> selected;
if (head <0)
{
Done();
}
//selected.clear();
for (now = head;now >=0;now = jobtable[now].next)
{
if (jobtable[now].length<=cur.mememory && jobtable[now].printer <=cur.printer && jobtable[now].tape <=cur.tape)
{
selected.push_back(now);
}
}
if (selected.size()==0)
{
FinishJob();
return 0;
}
int best=selected[0];
float bestv = jobtable[best].waittime / jobtable[best].runtime;
for (int i =1;i<selected.size();i++)
{
int tmp = selected[i];
if (jobtable[tmp].waittime / jobtable[tmp].runtime > bestv)
{
bestv = jobtable[tmp].waittime / jobtable[tmp].runtime;
best = tmp;
}
}
cout << "Start doing job No." << best+1
<< " job name:" << jobtable[best].name
<< " used " << jobtable[best].length << " of memeory ,"
<<jobtable[best].printer <<" printers, "
<<jobtable[best].tape << " tapes."
<<endl;
if (runhead < 0)
{
runhead = best;
if (head == best)
{
head = jobtable[best].next;
}else{
int i;
for(i= head;jobtable[i].next!=best;i=jobtable[i].next)
;
jobtable[i].next=jobtable[best].next;
}
jobtable[best].next = -1;
}else{
int i;
for(i= runhead;jobtable[i].next>=0;i=jobtable[i].next)
;
jobtable[i].next=best;
if (head == best)
{
head = jobtable[best].next;
}else{
for(i= head;jobtable[i].next!=best;i=jobtable[i].next)
;
jobtable[i].next=jobtable[best].next;
}
jobtable[best].next = -1;
}
cur.mememory -= jobtable[best].length;
cur.printer -= jobtable[best].printer;
cur.tape -= jobtable[best].tape;
return 1;
}
void Done()
{
FinishAll();
cout << "All job has been done\n";
exit(0);
}
void FinishJob()
{
if (runhead <0)
{
cout << "Error!! There is a job can never be selected as limits to the resourse\n";
Done();
}
int selected=runhead;
int lesttime=jobtable[runhead].runtime;
for (int i=runhead;i>=0;i=jobtable[i].next)
{
if (lesttime < jobtable[i].runtime)
{
lesttime = jobtable[i].runtime;
selected = i;
}
}
for (int m=runhead;m>=0;m=jobtable[m].next)
{
jobtable[m].runtime -= lesttime;
jobtable[m].waittime += lesttime;
}
if (selected==runhead)
{
runhead = jobtable[runhead].next;
}else
{
int m;
for (m=runhead;jobtable[m].next!=selected;m=jobtable[m].next)
;
jobtable[m].next = jobtable[selected].next;
}
cur.mememory += jobtable[selected].length;
cur.printer +=jobtable[selected].printer;
cur.tape += jobtable[selected].tape;
cout << "Finied job No."<<selected+1
<< "Get "<<jobtable[selected].length << " memeory,"
<<jobtable[selected].printer << " printers, "
<<jobtable[selected].tape << " tapes."
<<endl;
}
void FinishAll()
{
for (int m=runhead;m>=0;m=jobtable[m].next)
{
cout << "Finied job No."<<m+1
<< "Get "<<jobtable[m].length << " memeory,"
<<jobtable[m].printer << " printers, "
<<jobtable[m].tape << " tapes."
<<endl;
}
cout << "请输入e结束程序:";
char e;
cin>>e;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -