zoukankan      html  css  js  c++  java
  • 优先数调度:按最高优先级算法

    #include <stdio.h>

    #include <stdlib.h>

    #include <conio.h>

    #define getpch(type) (type*)malloc(sizeof(type))  

    struct pcb{           

     char name[5];    

     char state;       

     int super;        

     int needtime; 

     int rtime;

     int tijiaotime;  

        int starttime;  

        int ftime; 

        int zztime;

        int dzztime;

     struct pcb* link;    

    }*ready=NULL, *p;

    typedef struct pcb PCB;   

    float pzztime=0;

    float pdzztime=0;

    int n;

    int time=20;

    void sort()           

    {

     PCB *first, *second;   

     int insert=0;                                     

     if((ready==NULL)||((p->super)<(ready->super)))    

     {     

      p->link=ready;                                

      ready=p;

     }

     else   

     {

      first=ready;          

      second=first->link;    

      while(second!=NULL)

      {

       if((p->super)<(second->super)) 

       {

        p->link=second;

        first->link=p;

        second=NULL;

        insert=1;        

       }

       else

       {

        first=first->link;     

        second=second->link;  

       }

      }

      if(insert==0) first->link=p; 

     }

    }

    void input()              

    {

     int i;

     PCB *q;

     printf("请输入进程数:");

     scanf("%d",&n);

     for(i=0;i<n;i++)

     {

      printf(" 第%d个进程: ",i);

      p=getpch(struct pcb);

      printf("  进程名:");

       scanf("%s",&p->name);

      printf("  优先级:");

      scanf("%d",&p->super);

      printf("  运行时间:");

      scanf("%d",&p->needtime);

      printf(" 提交时间:");

      scanf("%d",&p->tijiaotime);

      p->rtime=0;

      p->state='R';     

      p->link=NULL;  

      

     sort();

     }

     q=ready;

    }

    int space() 

    {

     int l=0;

     PCB *pr=ready;    

     while(pr!=NULL)  

     {

      l++;

      pr=pr->link;

     }

     return(l);

    }

    void disp(PCB *p2)    

    {

    time = p2->tijiaotime > time? p2->tijiaotime:time;

     p2->starttime=time;

     time+=p2->needtime;

     p2->state='R'; 

     p2->ftime=time;

     p2->zztime=p2->ftime-p2->tijiaotime;

    p2->dzztime=p2->zztime/p2->needtime;

    pzztime=p2->zztime+pzztime;

    pdzztime=p2->dzztime+pdzztime;

     printf("周转时间:%d ",p->zztime);

      printf("带权周转时间为 %d ",p->dzztime); 

    }

    void destroy()    

     free(p);

    }

    void running()    

    {

     p->rtime=p->rtime+1;

     if(p->rtime==p->needtime)   

     { p->state='C';

     printf("%s",p->name);

     printf("运行完毕 ");

        disp(p);

      destroy();

     }

     else                    

     {

      (p->super)++;   

      sort(); 

      printf("正在运行进程%s ",p->name);

     }

    }

    void main()   

    {

     int len,h=0;

     input();       

     len=space();   

     while((len!=0)&&(ready!=NULL))   

     {

      

      h++;

      p=ready; 

      ready=p->link;

      p->link=NULL;   

      running();  

     }

     pzztime=pzztime/n;

        pdzztime=pdzztime/n;

      printf(" 平均周转时间:%f 平均带权周转时间为%f ",pzztime,pdzztime);

    }

  • 相关阅读:
    [bug] MySQL: The user specified as a definer ('root'@'%') does not exist
    [java] Maven安装本地jar包
    [bug] Openresty:content_by_lua_file 404
    [bug] Failed building wheel for xxx
    [bug] TypeError : unsupported operand type(s) for += : 'NoneType' and 'int'
    [Python] Flask从0到1开发轻量级网页
    [bug]Flask:KeyError: 'A secret key is required to use CSRF.'
    [bug] sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1054, "Unknown column 'recevie_name' in 'field list'")
    mac os 解决Error: EMFILE: too many open files错误
    从零开始的react入门教程(一),让我们从hello world开始
  • 原文地址:https://www.cnblogs.com/poonxiujet/p/5599398.html
Copyright © 2011-2022 走看看