• fd


    #include<iostream>
    #include<queue>
    #include<algorithm>
    using namespace std;
    struct Job
    {
     char name[11];
     int come_t,need_t,p;
     friend bool operator<(Job n1,Job n2)//大的在上面
        {
            if(n1.come_t>n2.come_t) return true;
      else if(n1.come_t==n2.come_t)
      {
       if(n1.need_t>n2.need_t)return true;
       if(n1.need_t<n2.need_t)return false;
       return n1.p>n2.p;
       
      }else return false;
        }
    }Job_list[111];
    struct PCB
    {
     char name[111],s;
     int need_t,p,r;
     PCB *next;
    }*p_ready,*end_p,*pre_p,*now_p,*finish;
    int n_list=10,all_p=3,now_np,clock,sum_job,com_job;
    priority_queue<Job> q,qq;
    void out_list()//输出调度表信息
    {
     printf("下面输出作业调度表中的信息 --作业名字--到达时间--需要运行时间--优先级 ");
     for(int i=com_job;i<n_list;i++)
       printf("------%s--------%d----------%d----------%d ",Job_list[i].name,Job_list[i].come_t,Job_list[i].need_t,Job_list[i].p);
    }
    void out_q()//输出后备队列信息
    {
     Job out;
     printf("下面输出后备队列中的信息 --作业名字--到达时间--需要运行时间--优先级 ");
     while(!q.empty())
     {
      out=q.top();q.pop();
      printf("------%s--------%d----------%d----------%d ",out.name,out.come_t,out.need_t,out.p);
      qq.push(out);
     }
     while(!qq.empty())
     {
      out=qq.top();qq.pop();
      q.push(out);
     }
    }
    void out_readyq()//输出就绪队列的信息
    {
     printf("下面输出就绪队列的信息 进程名字--状态--还需运行时间--优先级--分得时间片 ");
     for(PCB *l=p_ready->next;l!=p_ready;l=l->next)
      printf("----%s------%c--------%d----------%d------%d------ ",l->name,l->s,l->need_t,l->p,l->r);
    }
    void out_run(PCB *l)
    {
     printf("下面输出运行队列的信息 进程名字--状态--还需运行时间--优先级--分得时间片 ");
     printf("----%s------%c--------%d----------%d------%d------ ",l->name,l->s,l->need_t,l->p,l->r);
    }
    void out_finish()//输出完成队列的信息
    {
     printf("下面输出完成队列的信息 进程名字--状态--还需运行时间--优先级-- ");
     for(PCB *l=finish->next;l;l=l->next)
      printf("--%s------f--------%d----------%d------- ",l->name,l->need_t);
    }
    bool cmp(Job x,Job y)
    {
     if(x.come_t<y.come_t) return true;
     else return false;
    }
    void init()
    {
     com_job=0;p_ready=new(PCB);finish=new(PCB);finish->next=NULL;
     p_ready->next=p_ready;
     end_p=p_ready,now_p=p_ready,pre_p=p_ready;
     now_np=0;
     while(!q.empty()) q.pop(); clock=0;
     for(int i=0;i<n_list;i++)
      Job_list[i].come_t=rand()%10,Job_list[i].name[0]='a'+i,Job_list[i].need_t=rand()%10,Job_list[i].p=rand()%100,Job_list[i].name[1]='';
     sort(Job_list,Job_list+n_list,cmp);
    }
    void job_esca()
    {
     Job top_job;
     out_list();
     while(com_job<n_list&&Job_list[com_job].come_t==clock) q.push(Job_list[com_job++]);//作业的建立
     out_q();
     while(!q.empty()&&all_p>now_np++)  //作业的调用
     {
      PCB *p=new(PCB);
      top_job=q.top();q.pop();
      strcpy(p->name,top_job.name);
      p->need_t=top_job.need_t;
      p->p=top_job.p;
      p->r=1;
      p->s='w';
      p->next=end_p->next;
      end_p->next=p;
      end_p=p;
     }
    }
    void time_turn()
    {
     out_readyq();
     if(p_ready->next==p_ready) return ;
      pre_p=now_p;
      now_p=now_p->next;
     if(now_p==p_ready)
     {
      now_p=p_ready->next;
      pre_p=p_ready;
     }
     now_p->need_t--;
     now_p->s='r';
        out_run(now_p);
     now_p->s='s';
     if(now_p->need_t<=0)  //这个进程已经运行完毕
     {
      now_np--;
      pre_p->next=now_p->next;
      now_p->s='f';
      now_p->next=finish->next;//该进程进入完成队列
      finish->next=now_p;
      now_p=pre_p;
     }
     out_finish();
     getchar();
    }
    int main()
    {
     init();
     while(com_job<n_list||now_np)
     {
      printf("系统的当前时间为%d ",clock);
      job_esca();//作业建立&调度
      time_turn();//时间片轮转法进程调度
      clock++;
      printf(" ******************************************** ");
     }
     return 0;
    }

  • 相关阅读:
    iOS中循环引用的解除
    Block的循环引用详解
    Mac OS X下面 Node.js环境的搭建
    swift中闭包和OC的block的对比
    STL priority_queue
    优先使用map(或者unordered_map)的find函数而非algorithm里的find函数
    Insert Interval
    Integer Break
    Unique Binary Search Trees
    腾讯2016实习生笔试
  • 原文地址:https://www.cnblogs.com/zhangdashuai/p/4596003.html
Copyright © 2020-2023  润新知