| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 754 人关注过本帖
标题:哪位朋友能够把下面的程序(模拟作业调度)补充完整呢?
只看楼主 加入收藏
huangdan
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2009-10-24
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
哪位朋友能够把下面的程序(模拟作业调度)补充完整呢?
#include<iostream.h>
#include<process.h>
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void why_fifs();
void why_sjf();
void why_hrn();
void why_priority();
void why_setup();
void why_readjob();
struct job
{    int jobnumber;
     int waittimes;                  //等待时间
     int needtimes;                  //需要运行时间
     int priority;                   //静态优先级
     job*prior;
     job*next;
};
job*head;
void main()
{    char w_choice;
     head=NULL;
     while(1)
    {     system("cls");
          cout<<endl<<endl<<endl;
          cout<<"                操作系统(实验一)--------作业调度模拟"<<endl;
          cout<<endl<<"             设 置 作 业 队 列     -------------->1"<<endl;
          cout<<endl<<"             先 来 先 服 务(FCFS)-------------->2"<<endl;
          cout<<endl<<"             短 作 业 优 先(SJF) -------------->3"<<endl;
          cout<<endl<<"             相 应 比 高 者 优 先(HRN)--------->4"<<endl;
          cout<<endl<<"             优 先 级 法           -------------->5"<<endl;                    
          cout<<endl<<"             退                  -------------->6";
          cout<<endl ;
          cout<<endl<< "            请选择演示方式(1-5):";
          cin>>w_choice;
          cin.ignore(300,'\n');
          switch(w_choice)
          {   
               case'1':why_setup();break;
               case'2':why_fifs();break;
               case'3':why_sjf();break;
               case'4':why_hrn();break;
               case'5':why_priority();break;                       
               case'6':exit(0);
               default:break;
          }
    }
}

void why_readjob()
{    job*tp,*p;
     FILE*fp;
     system("cls");
     fp=fopen("job","rb");
     if(fp==NULL)
     {    cout<<endl<<"open file error or file null"<<endl;
          getchar();
          return;   }
     else
     {    if(tp=(job*)malloc(sizeof(job)))
          {    fread(tp,sizeof(struct job),1,fp);
               head=tp;
               tp->prior=NULL;
               tp->next =NULL;   }
          else
          {    cout<<endl<<"out of memory"<<endl;
               getch();
               return;           }
          while(1)
          {    if(feof(fp))
               {    fclose(fp);
                    break;    }
               else
               {    if(p=(job*)malloc(sizeof(job)))
                    {    fread(p,sizeof(struct job),1,fp);
                         tp->next=p;
                         p->prior=tp;
                         tp=tp->next;
                         tp->next=NULL; }
                     else
                     {   system("cls");
                         cout<<endl<<"out of memory! anykey exit"<<endl;
                         getch();
                         return;        }
               }  
          }
          p=tp;
          tp=tp->prior;
          tp->next=NULL;
          free(p);
     }
}


void why_setup()
{    job *tp,*p;
     FILE *fp;
     int jobnumber,needtimes,priority;//作业号、运做时间、优先级
     jobnumber=0;                     
     system("cls");
     cout<<endl<< " 该功能将重新生成作业队列文件,原来的数据将丢失(若不修改,直接按0退出)。"<<endl;
     while(1)
     {    cout<<endl<<" 请输入模拟的作业信息(运行时间为0保存退出)"<<endl;
          cout<<endl<<"         "<<++jobnumber;  
          cout<<endl<<"   所运行时间:";               
          cin>>needtimes;              //输入运行时间needtimes
          cin.ignore(300,'\n');
          //当needtimes=0时,结束作业的设置
          //**********************************************************
          if(needtimes==0)              
          {    if(head==NULL)return;
               fp=fopen("job","wb");  //写文件
               if(fp==NULL)
               {   cout<<endl<<"error!!!can't open file 'job' with writen,please check it's attrib"<<endl;
                   return;            
               }
               else
               {   tp=head;
                   while(tp)
                   {    fwrite(tp,sizeof(struct job),1,fp);
                        tp=tp->next;   }
                   fclose(fp);
                   return;
               }               
          }

          //设置作业的属性
          //**********************************************************
          cout<<"          级:";
          cin>>priority;            //输入优先级
          cin.ignore(300,'\n');
          p=( job *)malloc(sizeof(job));
          if(p)
          {   p->jobnumber =jobnumber;
              p->waittimes =0;
              p->needtimes =needtimes;
              p->priority =priority;
              if(jobnumber==1)        //双链表表头结点
              {  head=p;
                 tp=p;
                 tp->prior =NULL;
                 tp->next  =NULL;  
              }
              else                  //指针p指向新建立的结点,tp记录前一个结点
              {  p->prior =tp;
                 tp->next =p;
                 tp=tp->next;
                 tp->next =NULL;
              }
          }
          else
          {   
              cout<<endl<<"out of memory! anykey exit"<<endl;
              getch();
              return;           
          }
         //**********************************************************
     }
}

//先来先服务(CFSF)
void why_fifs()
{  
     job *p,*tp;
     float T,W;
     int w_runtimes,w_jobs;//w_jobs为参与模拟的作业数
     why_readjob();        //读入之前设置的作业及相关属性
     system("cls");
     cout<<endl<<"先来先服务(FIFS)算法按作业进入队列的先后次序进行调度"<<endl<<"(该程序为单道操作系统模拟):"<<endl;
     cout<<endl<<"调度序列:"<<endl<<endl;
     T=0;
     W=0;
     w_runtimes=0;
     w_jobs=0;
     p=head;                //p指向1号作业
     while(p)                        
     {     T=T+p->needtimes+p->waittimes;                        //计算累加时间=操作时间+等待时间
           W=W+(p->needtimes +p->waittimes)*1.0/p->needtimes;    //带权周转时间
           w_runtimes=p->needtimes;  //作业操作时间=作业欲设需要时间
           w_jobs++;        //作业数量
           cout<<" 作业 "<<p->jobnumber;    //作业+编号
           p=p->next;
           tp=p;
           while(tp)
           {   
               tp->waittimes =tp->waittimes +w_runtimes;
               tp=tp->next;     
           }
     }
     cout<<endl<<endl;
     cout<<"作业平均周转时间为(取整数部分):"<<int(T/w_jobs)<<endl<<endl;
     cout<<"作业带权周转时间为(取整数部分):"<<int(W/w_jobs)<<endl;
     cout<<endl<<"anykey return"<<endl;
     getch();
     return;
}


void why_priority()
{   
    //优先级法
}

void why_hrn()
{   
    //响应比高者优先        
}
void why_sjf()
{
     //短作业优先算法
}
搜索更多相关主题的帖子: 作业 朋友 模拟 
2009-10-24 16:55
caolihui1008
Rank: 2
来 自:石家庄
等 级:论坛游民
帖 子:33
专家分:75
注 册:2009-10-19
收藏
得分:20 
作业最好还是自己做把呵呵!
2009-10-28 18:36
快速回复:哪位朋友能够把下面的程序(模拟作业调度)补充完整呢?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019934 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved