短作业优先调度算法(C++代码)

来源:互联网 发布:centos 6.5mysql 编辑:程序博客网 时间:2024/05/02 02:27

#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#define m 10
typedef struct jcb
{
   char name[4];
   int length ;
   int printer;
   int tape;
   int runtime;
   int waittime;
   int next;
}JCB;
int head;
int tape,printer;
long memory;
JCB jobtable[m];
int jobcount=0;
 
void shedule()
 {
 
 float xk,k;
    int p,q,s,t;
    do
 {
  p=head;
  q=s=-1;
  k=1000;
  while (p!=-1)
  {
   if (jobtable[p].length<=memory&&jobtable[p].tape<=tape&&jobtable[p].printer<=printer)
   {
    xk=float(jobtable[p].runtime);
    if(q==0||xk<k)
    {
     k=xk;
     q=p;
     t=s;
    }
   }
   s=p;
   p=jobtable[p].next;
  }
  if (q!=-1)
  {
   if(t==-1)
    head=jobtable[head].next;
   else
    jobtable[t].next=jobtable[q].next;
   memory=memory-jobtable[q].length;
   tape=tape-jobtable[q].tape;
   printer=printer-jobtable[q].printer;
   cout<<"选中作业的作业名:"<<jobtable[q].name<<endl;

  }
 }while (q!=-1);
 if (head!=0)
    cout<<"选中作业的:"<<jobtable[q].name<<endl;
 }

void main()
   {char name[4];
   int size=0,tcount,pcount,wtime,rtime;
   int p;
   size=0;
   tcount=0;
   pcount=0;
   wtime=0;
   rtime=0;
   memory=65536;
   tape=4;
   printer=2;
   head=-1;
   cout<<"输入作业相关数据(以作业大小为负数停止输入):"<<endl;
   cout<<"输入作业名,作业大小,磁带机数,打印机数,等待时间,估计执行时间"<<endl;
   cin>>name>>size>>tcount>>pcount>>wtime>>rtime;
   while(size!=-1)
   {  if (jobcount<m)
        p=jobcount;
   else {cout<<"无法创建进程"<<endl;
   break;
   }
   jobcount++;
   strcpy(jobtable[p].name,name);
   jobtable[p].length=size;
   jobtable[p].printer=pcount;
   jobtable[p].tape=tcount;
   jobtable[p].runtime=rtime;
   jobtable[p].waittime=wtime;
   jobtable[p].next=head;
   head=p;

   cout<<"输入作业名,作业大小,磁带机数,打印机数,等待时间,估计执行时间,"<<endl;

   cin>>name>>size>>tcount>>pcount>>wtime>>rtime;

   }
   shedule();
   }
 

原创粉丝点击