当前位置:   article > 正文

操作系统课程设计之处理机调度_编写并实现对n个进程使用动态优先权算法的进程调度c语言

编写并实现对n个进程使用动态优先权算法的进程调度c语言

用C语言编写实现对n个进程采用动态优先权算法的进程调度模拟程序。

1.首先设计定义变量

  1. 进程标识号:PID
  2. 进程名:PNAME
  3. 进程优先数SUPER,并规定优先数越大的进程,其优先权越高;假定在调度过程中,进程每运行一个时间片,其优先数减2;进程每在就绪队列中待一个时间片,则其优先数加1。(可自行设定动态优先数的变化规律)
  4. 进程已占用的CPU时间RUNTIME(rtime)。 
  5. 进程最大需占用的CPU时间NEEDTIME(ntime)。当RUNTIME等于NEEDTIME时,进程运行完毕。

        进程状态STATE。假设实验中的进程只有三种状态:就绪(Wait)、运行(Running)和完成(Finished)。

2.设计所需结构体和函数

定义进程控制块PCB

  1. struct pcb /*定义进程控制块PCB*/
  2. { char name[10]; /*进程名*/
  3. char state; /*状态:R-运行,W-就绪,F-完成*/
  4. int super; /*优先数*/
  5. int ntime; /*所需运行时间 need time*/
  6. int rtime; /*已经运行时间 run time*/
  7. struct pcb * next;
  8. }*ready=NULL,*p; /* ready指向就绪队列,P指向当前节点 */
  9. typedef struct pcb PCB;

对进程进行优先级排列函数

  1. void sort(){
  2. PCB *first, *second;
  3. int insert = 0;
  4. if ((ready == NULL) || ((p->super) > (ready->super))) //优先级最大者,插入就绪队列队首
  5. {
  6. p->next =ready;//插入的数据作为链表头结点
  7. ready = p;//头结点指针指向插入数
  8. }
  9. else //进程比较优先级,插入适当的位置中
  10. {
  11. first = ready;
  12. second = first->next;
  13. while (second != NULL)
  14. {
  15. if ((p->super) > (second->super)) //若插入进程比当前进程优先数大
  16. { //插入到当前进程前面
  17. p->next = second;
  18. first->next = p;
  19. second = NULL;
  20. insert = 1;
  21. }
  22. else//插入进程优先数最低,则插入到队尾
  23. {
  24. first = first->next;
  25. second = second->next;
  26. }
  27. }
  28. if (insert == 0)
  29. first->next = p;
  30. }
  31. }

输入进程控制块函数

  1. void input()
  2. {
  3. int n;
  4. printf("Please Input the Number of Process:");
  5. scanf("%d",&n);
  6. for(int i=0;i<n;i++){
  7. printf("process id No.%d\n",i+1);
  8. p=getpch(PCB);
  9. printf("process name:");
  10. scanf("%s", p->name);
  11. printf("process priority:");
  12. scanf("%d", &p->super);
  13. printf("process need running time:");
  14. scanf("%d", &p->ntime);
  15. p->rtime=0;
  16. p->state='W';
  17. p->next=NULL;
  18. sort();
  19. }
  20. }

获取就绪状态的进程数

  1. int space()
  2. {
  3. int len=0;
  4. PCB* pr=ready;
  5. while (pr!=NULL)
  6. {
  7. len++;
  8. pr = pr->next;
  9. }
  10. return(len);
  11. }

展示进程各个变量的具体内容

  1. void disp(PCB * pr)
  2. {
  3. printf("\n name state super ntime rtime\n");
  4. printf(" %s\t", pr->name);
  5. printf(" %c\t", pr->state);
  6. printf(" %d\t", pr->super);
  7. printf(" %d\t", pr->ntime);
  8. printf(" %d\t", pr->rtime);
  9. printf(" \n");
  10. }

查看进程信息,显示当前处于运行态的进程和处于就绪队列的进程

  1. void look()
  2. {
  3. PCB* pr;
  4. printf("\n**** the running process is:%s", p->name);
  5. disp(p);
  6. pr = ready;
  7. printf("\n**** the ready process is:\n");
  8. while (pr != NULL)
  9. {
  10. disp(pr);
  11. pr = pr->next;
  12. }
  13. }

建立进程就绪函数(进程运行时间到,置就绪状态)

  1. void running()
  2. {
  3. PCB *pr;
  4. (p->rtime)++;
  5. pr=ready;
  6. while(ready!=NULL){
  7. (ready->super)++;
  8. ready=ready->next;
  9. }ready=pr;
  10. if (p->rtime == p->ntime)
  11. {
  12. printf("\n process[%s] is finished.\n", p->name);
  13. free(p);
  14. }
  15. else
  16. {
  17. (p->super)=(p->super)-2;
  18. p->state = 'W';
  19. sort();
  20. }
  21. }

下面为程序源代码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #define getpch(type) (type *)malloc(sizeof(type))
  5. struct pcb /*定义进程控制块PCB*/
  6. { char name[10]; /*进程名*/
  7. char state; /*状态:R-运行,W-就绪,F-完成*/
  8. int super; /*优先数*/
  9. int ntime; /*所需运行时间 need time*/
  10. int rtime; /*已经运行时间 run time*/
  11. struct pcb * next;
  12. }*ready=NULL,*p; /* ready指向就绪队列,P指向当前节点 */
  13. typedef struct pcb PCB;
  14. //对进程进行优先级排列函数
  15. void sort(){
  16. PCB *first, *second;
  17. int insert = 0;
  18. if ((ready == NULL) || ((p->super) > (ready->super))) //优先级最大者,插入就绪队列队首
  19. {
  20. p->next =ready;//插入的数据作为链表头结点
  21. ready = p;//头结点指针指向插入数
  22. }
  23. else //进程比较优先级,插入适当的位置中
  24. {
  25. first = ready;
  26. second = first->next;
  27. while (second != NULL)
  28. {
  29. if ((p->super) > (second->super)) //若插入进程比当前进程优先数大
  30. { //插入到当前进程前面
  31. p->next = second;
  32. first->next = p;
  33. second = NULL;
  34. insert = 1;
  35. }
  36. else//插入进程优先数最低,则插入到队尾
  37. {
  38. first = first->next;
  39. second = second->next;
  40. }
  41. }
  42. if (insert == 0)
  43. first->next = p;
  44. }
  45. }
  46. void input()
  47. {
  48. int n;
  49. printf("Please Input the Number of Process:");
  50. scanf("%d",&n);
  51. for(int i=0;i<n;i++){
  52. printf("process id No.%d\n",i+1);
  53. p=getpch(PCB);
  54. printf("process name:");
  55. scanf("%s", p->name);
  56. printf("process priority:");
  57. scanf("%d", &p->super);
  58. printf("process need running time:");
  59. scanf("%d", &p->ntime);
  60. p->rtime=0;
  61. p->state='W';
  62. p->next=NULL;
  63. sort();
  64. }
  65. }
  66. int space()
  67. {
  68. int len=0;
  69. PCB* pr=ready;
  70. while (pr!=NULL)
  71. {
  72. len++;
  73. pr = pr->next;
  74. }
  75. return(len);
  76. }
  77. void disp(PCB * pr)
  78. {
  79. printf("\n name state super ntime rtime\n");
  80. printf(" %s\t", pr->name);
  81. printf(" %c\t", pr->state);
  82. printf(" %d\t", pr->super);
  83. printf(" %d\t", pr->ntime);
  84. printf(" %d\t", pr->rtime);
  85. printf(" \n");
  86. }
  87. void look()
  88. {
  89. PCB* pr;
  90. printf("\n**** the running process is:%s", p->name);
  91. disp(p);
  92. pr = ready;
  93. printf("\n**** the ready process is:\n");
  94. while (pr != NULL)
  95. {
  96. disp(pr);
  97. pr = pr->next;
  98. }
  99. }
  100. void running()
  101. {
  102. PCB *pr;
  103. (p->rtime)++;
  104. pr=ready;
  105. while(ready!=NULL){
  106. (ready->super)++;
  107. ready=ready->next;
  108. }ready=pr;
  109. if (p->rtime == p->ntime)
  110. {
  111. printf("\n process[%s] is finished.\n", p->name);
  112. free(p);
  113. }
  114. else
  115. {
  116. (p->super)=(p->super)-2;
  117. p->state = 'W';
  118. sort();
  119. }
  120. }
  121. main( )
  122. { int len, h=0; /*len存放就绪进程的数量,h为运行的时间片序号*/
  123. char ch;
  124. input( ); /*接收用户输入,建立ready指向的进程链表*/
  125. len=space( ); /*获取就绪状态的进程数*/
  126. while((len!=0)&&(ready!=NULL))
  127. { ch=getchar( );
  128. h++;
  129. printf("\n The execute number:%d",h);
  130. p=ready; /*运行就绪队列的队首进程*/
  131. ready=p->next; /*更改就绪队列指针*/
  132. p->next=NULL; /*摘下队首进程*/
  133. p->state='R'; /*置为运行状态*/
  134. look( ); /*查看并显示运行和就绪的进程信息*/
  135. running( );
  136. printf("\n please press any key to continue......");
  137. ch=getchar( );
  138. }
  139. printf("\n\n All processes are finished.\n");
  140. ch=getchar( );
  141. }

运行结果:

d8a72995b3044e39be0da91b1fa42def.png

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/460150
推荐阅读
相关标签
  

闽ICP备14008679号