[C++] 模拟进程调度时间片轮转算法 →→→→→进入此内容的聊天室

来自 , 2021-04-02, 写在 C++, 查看 146 次.
URL http://www.code666.cn/view/7d2b92b6
  1. #include"stdio.h"
  2. #include"stdlib.h"
  3. #include<iostream>
  4. using namespace std;
  5. #define ready 1
  6. #define run 2
  7.  
  8. struct pcb
  9. {
  10.       char name[10];  
  11.       int priority;   /*进程的优先级*/
  12.       int state;      /*进程的状态:可以有运行和就绪*/
  13.           int needtime;   /*进程需要运行的时间*/
  14.           int runtime;
  15.       int  time;    /*进程已运行的时间*/
  16.           struct pcb *next;  /*指向下一个进程PCB的指针*/
  17. };    
  18. typedef struct pcb PCB;
  19. PCB *head=NULL;
  20.  
  21.  void create()      /*此函数用于创建进程队列*/  
  22. {
  23.          PCB *p, *q;
  24.      int n,i;
  25.      printf("请输入要创建的进程的数量:");
  26.      scanf("%d",&n);                          
  27.      head=(PCB *)malloc(sizeof(PCB));        
  28.      p=head;
  29.      for(i=1;i<=n;i++)                        
  30.      {  
  31.                  q=(PCB*)malloc(sizeof(PCB));
  32.              p->next=q;
  33.              p=q;
  34.              printf("\n请输入 NO.%d 个进程的标识符:",i);
  35.          scanf("%s",&p->name);
  36.              printf("请输入进程的优先级:");
  37.              scanf("%d",&p->priority);
  38.          printf("请输入进程所需的时间:");
  39.          scanf("%d",&p->needtime);
  40.              p->state=ready;
  41.          p->runtime=0;
  42.         p->next=NULL;
  43.      }
  44.  }
  45.  
  46. void lunzhuan ()      /*时间片轮转*/
  47. {
  48.         PCB *p,*q,*r;
  49.    int time;
  50.    printf("请输入时间片大小:\n ");
  51.    scanf("%d",&time);
  52.    for(p=head->next;p->next;p=p->next)
  53.       r=p;
  54.    while(head->next)
  55.    {
  56.            p=head->next;       /*选出就绪队列的第一个进程*/
  57.        p->state=run;
  58.        printf("\n正在运行的进程是:\n"); /*输出该进程的信息*/
  59.        printf("%s\t",p->name);
  60.        printf("state: 运行\t");
  61.        printf("needtime: %d\t",p->needtime);
  62.        printf("runtime: %d\n",p->needtime);
  63.        q=head->next;
  64.        if(p->needtime-p->runtime<=p->time)   /*时间片内,进程运行结束否*/
  65.            {
  66.                    p->runtime=p->needtime;
  67.            printf("runtime:%d\n",p->runtime);
  68.            }
  69.        else
  70.            {
  71.                    p->runtime=p->runtime+p->time;
  72.            printf("runtime:%d\n",p->runtime);
  73.        }
  74.        q=p->next;
  75.        if(q!=NULL)                         /*输出就绪队列中的进程信息*/
  76.            printf("就绪队列是:\n");
  77.        else
  78.                    printf("就绪队列为空!\n");
  79.       while(q)
  80.           {
  81.                   printf("%s\t",q->name);
  82.           printf("state:   就绪\t");
  83.           printf("needtime:%d\t",q->needtime);
  84.           printf("runtime:%d\t",q->runtime);
  85.           printf("\n");
  86.           q=q->next;  
  87.           }
  88.      if(p->runtime==p->needtime)
  89.          {
  90.                  delete(head,p);
  91.          }
  92.      else
  93.          {
  94.                  head->next=p->next;
  95.          r->next=p;
  96.          r=p;
  97.          r->state=ready;
  98.          r->next=NULL;
  99.          }
  100.    }
  101. }
  102.  
  103. void main()
  104. {
  105.         cout<<"开始创建进程:"<<endl;
  106.     create();
  107.         if(head==NULL || head->next==NULL)
  108.       printf("就绪队列没有进程,请先创建进程\n");
  109.         else
  110.       lunzhuan();
  111. }
  112.  
  113.  

回复 "模拟进程调度时间片轮转算法"

这儿你可以回复上面这条便签

captcha