进程调度算法--时间片轮转调度算法

avatar 2017年04月16日22:08:58 3 5438 views
博主分享免费Java教学视频,B站账号:Java刘哥 ,长期提供技术问题解决、项目定制:本站商品点此
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define time 2
  5. typedef int datatype;
  6. typedef struct link_node {
  7.     char name[20];
  8.     int arrive;
  9.     int start;
  10.     int finish;
  11.     int zhouzhuan;
  12.     float daizhou;
  13.     int service;
  14.     int count;
  15.     int t;
  16.     struct link_node *next;
  17. } node;
  18. typedef node *linklist;
  19. linklist creatbyqueue() {
  20.     linklist head,r,s;
  21.     datatype arrive, server;
  22.     char str[20];
  23.     head = r = (linklist)malloc(sizeof(node));
  24.     head->next = NULL;
  25.     printf("当前时间片为两个单位时间,所有进程拥有相间的时间片\n");
  26.     printf("输入进程名,到达时间,服务时间:\n");
  27.     scanf("%s",str);
  28.     scanf("%d",&arrive);
  29.     scanf("%d",&server);
  30.     s = (linklist)malloc(sizeof(node));
  31.     s->t = 0;
  32.     s->arrive = arrive;
  33.     s->service = server;
  34.     strcpy(s->name, str);
  35.     s->count = s->service;
  36.     head->next = s;
  37.     s->next = NULL;
  38.     return head;
  39. }
  40. void print(linklist head, int i) {
  41.     linklist p=head->next,q;
  42.     while(p) {
  43.         if(p->arrive == i) {
  44.             printf("进程%s加入就绪队列\n",p->name);
  45.         }
  46.         p = p->next;
  47.     }
  48.     p=head->next;
  49.     printf("当前运行的进程是%s\n",head->next->name);
  50.     printf("就绪队列中有进程:");
  51.     while(p->next) {
  52.         p = p->next;
  53.         if(p->arrive <= i)
  54.             printf("    %s",p->name);
  55.     }
  56.     printf("\n");
  57. }
  58. void delete(linklist head, int i) {
  59.     linklist p,q;
  60.     p = head->next;
  61.     q = p->next;
  62.     head->next = q;
  63.     free(p);
  64. }
  65. void insert(linklist head) {
  66.     linklist p;
  67.     char str[20];
  68.     datatype arrive, server;
  69.     linklist r = (linklist)malloc(sizeof(node));
  70.     p = head->next;
  71.     while(p->next) {
  72.         p = p->next;
  73.     }
  74.     scanf("%s",str);
  75.     scanf("%d",&arrive);
  76.     scanf("%d",&server);
  77.     strcpy(r->name,str);
  78.     r->arrive = arrive;
  79.     r->t = 0;
  80.     p->next = r;
  81.     r->service = server;
  82.     r->count = r->service;
  83.     r->next = NULL;
  84. }
  85. void sort(linklist head) {
  86.     linklist p,pre,r,m;
  87.     m = head;
  88.     r = pre = head->next;
  89.     p = pre->next;
  90.     while(r) {
  91.         while(p) {
  92.             if(pre->arrive > p->arrive) {
  93.                 m->next = p;
  94.                 pre->next = p->next;
  95.                 p->next = pre;
  96.                 p = pre;
  97.                 pre = m->next;
  98.             }
  99.             m = m->next;
  100.             pre = pre->next;
  101.             p = p->next;
  102.         }
  103.         r = r->next;
  104.     }
  105. }
  106. void statistic(linklist head, int i) {
  107.     linklist p = head->next;
  108.     p->finish = i;
  109.     p->zhouzhuan = p->finish - p->arrive;
  110.     p->daizhou = p->zhouzhuan * 1.0/ p->service;
  111. }
  112. void finish(linklist run, int i) {
  113.     printf("进程%s任务完成\n",run->next->name);
  114.     statistic(run, i);
  115.     printf("到达时间\t开始时间\t完成时间\t周转时间\t带权周转时间\n");
  116.     printf("%5d\t%12d\t%12d\t%12d\t\t%.3f\t\n",run->next->arrive, run->next->start, run->next->finish, run->next->zhouzhuan, run->next->daizhou);
  117.     delete(run,i);
  118.     if(run->next!=NULL)
  119.         if(run->next->service == run->next->count)
  120.             run->next->start = i;
  121.     if(run->next != NULL)
  122.         print(run,i);
  123. }
  124. int main() {
  125.     linklist head,p;
  126.     int i = 0;
  127.     head = creatbyqueue();
  128.     int flag = 1;
  129.     while(flag) {
  130.         printf("是否继续输入(1或0):");
  131.         scanf("%d",&flag);
  132.         if(flag) {
  133.             insert(head);
  134.         }
  135.     }
  136.     sort(head);
  137.     linklist run =(linklist)malloc(sizeof(node));
  138.     run->next = NULL;
  139.     linklist r = run;
  140.     while(head->next!=NULL ) {
  141.         printf("已经过单位时间%d\n",i);
  142.         if(head->next->arrive > i && run->next == NULL) {
  143.             printf("当前无进程执行或就绪\n");
  144.         }
  145.         else  if(head->next->arrive == i) {
  146.             while(head->next) {
  147.                 if(head->next->arrive == i) {
  148.                     r->next = head->next;
  149.                     head->next = head->next->next;
  150.                     r = r->next;
  151.                     r->next = NULL;
  152.                 } else break;
  153.             }
  154.             if(run->next->count != 0) {
  155.                 if(run->next->service==run->next->count)
  156.                     run->next->start = i;
  157.                 if(run->next->t != time) {
  158.                     run->next->count--;
  159.                     run->next->t++;
  160.                 } else {
  161.                     printf("进程%s的时间片用完\n",run->next->name);
  162.                     run->next->t = 0;
  163.                     if(run->next->next != NULL) {
  164.                         r->next = run->next;
  165.                         run->next = run->next->next;
  166.                         r = r->next;
  167.                         r->next = NULL;
  168.                         if(run->next->service==run->next->count)
  169.                             run->next->start = i;
  170.                         run->next->count--;
  171.                         run->next->t++;
  172.                     }
  173.                 }
  174.                 print(run,i);
  175.             } else {
  176.                 finish(run,i);
  177.                 if(run->next) {
  178.                 run->next->count--;
  179.                 run->next->t++;
  180.             }
  181.             }
  182.         } else {
  183.             if(run->next->count != 0) {
  184.                 if(run->next->t != time) {
  185.                     run->next->count--;
  186.                     run->next->t++;
  187.                 } else {
  188.                     printf("进程%s的时间片用完\n",run->next->name);
  189.                     run->next->t = 0;
  190.                     if(run->next->next != NULL) {
  191.                         r->next = run->next;
  192.                         run->next = run->next->next;
  193.                         r = r->next;
  194.                         r->next = NULL;
  195.                         if(run->next->service==run->next->count)
  196.                             run->next->start = i;
  197.                         run->next->count--;
  198.                         run->next->t++;
  199.                     }   else {
  200.                         run->next->count--;
  201.                     }
  202.                 }
  203.                 print(run,i);
  204.             } else {
  205.                 finish(run,i);
  206.                 if(run->next) {
  207.                     run->next->count--;
  208.                     run->next->t++;
  209.                 }
  210.             }
  211.         }
  212.         i++;
  213.     }
  214.     while(run->next != NULL) {
  215.         printf("已经过单位时间%d\n",i);
  216.         if(run->next->service==run->next->count)
  217.             run->next->start = i;
  218.         if(run->next->count != 0) {
  219.             if(run->next->t != time) {
  220.                 run->next->count--;
  221.                 run->next->t++;
  222.             } else {
  223.                 printf("进程%s的时间片用完\n",run->next->name);
  224.                 run->next->t = 0;
  225.                 if(run->next->next != NULL) {
  226.                     r->next = run->next;
  227.                     run->next = run->next->next;
  228.                     r = r->next;
  229.                     r->next = NULL;
  230.                     if(run->next->service==run->next->count)
  231.                         run->next->start = i;
  232.                     run->next->count--;
  233.                     run->next->t++;
  234.                 } else {
  235.                     run->next->count--;
  236.                 }
  237.             }
  238.             print(run,i);
  239.         } else {
  240.             finish(run,i);
  241.             if(run->next) {
  242.                 run->next->count--;
  243.                 run->next->t++;
  244.             }
  245.         }
  246.         i++;
  247.     }
  248. }

本文地址:http://liuyanzhao.com/3143.html

转载请注明
  • 微信
  • 交流学习,资料分享
  • weinxin
  • 个人淘宝
  • 店铺名:言曌博客咨询部

  • (部分商品未及时上架淘宝)
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:2   待审核评论数:0
  1. avatar 狂放

    大神学习了

  2. avatar 战略

    谢谢分享