博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【操作系统】实验三 进程调度模拟程序
阅读量:7103 次
发布时间:2019-06-28

本文共 4067 字,大约阅读时间需要 13 分钟。

1 #include 
2 #include
3 #include
4 #define getpch(type) (type*)malloc(sizeof(type)) 5 #define N 3 6 struct pcb { 7 char name[10]; 8 char status; 9 int prio; 10 int ntime; 11 int rtime; 12 struct pcb* link; 13 }*ready=NULL,*p; 14 typedef struct pcb PCB; 15 16 sort() 17 { 18 PCB *first, *second; 19 int insert=0; 20 if((ready==NULL)||((p->prio)>(ready->prio))) 21 { 22 p->link=ready; 23 ready=p; 24 } 25 else 26 { 27 first=ready; 28 second=first->link; 29 while(second!=NULL) 30 { 31 if((p->prio)>(second->prio)) 32 { 33 p->link=second; 34 first->link=p; 35 second=NULL; 36 insert=1; 37 } 38 else 39 { 40 first=first->link; 41 second=second->link; 42 } 43 } 44 if(insert==0) first->link=p; 45 } 46 } 47 48 input() 49 { 50 int i,num; 51 printf("\n 请输入进程数:"); 52 scanf("%d",&num); 53 for(i=0;i
name); 59 p->prio=N; 60 printf("\n 输入进程运行时间:"); 61 scanf("%d",&p->ntime); 62 printf("\n"); 63 p->rtime=0;p->status='r'; 64 p->link=NULL; 65 sort(); 66 } 67 68 } 69 70 71 int space() 72 { 73 int l=0; PCB* pr=ready; 74 while(pr!=NULL) 75 { 76 l++; 77 pr=pr->link; 78 } 79 return(l); 80 } 81 82 83 disp(PCB * pr) 84 { 85 86 printf("|%s\t",pr->name); 87 printf("|%c\t",pr->status); 88 printf("|%d\t",pr->prio); 89 printf("|%d\t",pr->ntime); 90 printf("|%d\t",pr->rtime); 91 printf("\n"); 92 } 93 94 void printbyprio(int prio) 95 { 96 PCB* pr; 97 pr=ready; 98 printf("\n ****当前第%d级队列(优先数为%d)的就绪进程有:\n",(N+1)-prio,prio); 99 printf("\n qname \tstatus\t prio \tndtime\t runtime \n"); 100 while(pr!=NULL) 101 { 102 if (pr->prio==prio) disp(pr); 103 pr=pr->link; 104 } 105 }106 107 check()108 { 109 PCB* pr; 110 int i;111 printf("\n /\\/\\/\\/\\当前正在运行的进程是:%s",p->name); 112 printf("\n qname \tstatus\t prio \tndtime\t runtime \n"); 113 disp(p); 114 115 printf("\n 当前就绪队列状态为:\n"); 116 for(i=N;i>=1;i--)117 printbyprio(i);118 } 119 120 121 destroy() 122 { 123 printf("\n 进程 [%s] 已完成.\n",p->name); 124 free(p); 125 } 126 127 128 running() 129 { 130 int slice,i;131 slice=1;132 for(i=1;i<((N+1)-p->prio);i++)133 slice=slice*2;134 135 for(i=1;i<=slice;i++)136 {137 (p->rtime)++; 138 if (p->rtime==p->ntime)139 break;140 141 }142 if(p->rtime==p->ntime) 143 destroy();144 else 145 { 146 if(p->prio>1) (p->prio)--; 147 p->status='r'; 148 sort(); 149 } 150 } 151 void cteatpdisp()152 { 153 int i;154 printf("\n 当增加新进程后,所有就绪队列中的进程(此时无运行进程):\n");155 for(i=N;i>=1;i--)156 printbyprio(i);157 }158 159 void creatp()160 {161 char temp;162 printf("\nCreat one more process?type Y (yes)");163 scanf("%c",&temp);164 if (temp=='y'||temp=='Y')165 {166 input();167 cteatpdisp();168 }169 170 } 171 172 main() 173 { 174 int len,h=0; 175 char ch; 176 input(); 177 len=space(); 178 while((len!=0)&&(ready!=NULL)) 179 { 180 ch=getchar(); 181 h++; 182 printf("\n The execute number:%d \n",h); 183 p=ready; 184 ready=p->link; 185 p->link=NULL; 186 p->status='R'; 187 check(); 188 running(); 189 creatp();190 printf("\n 按任一键继续......"); 191 ch=getchar(); 192 } 193 printf("\n\n 进程已经完成.\n"); 194 ch=getchar(); 195 ch=getchar();196 }

 

转载于:https://www.cnblogs.com/qq1942589447/p/5489485.html

你可能感兴趣的文章
详解JAVA实现支付宝接口编程
查看>>
HTTP 请求返回代码含义
查看>>
python+soket实现UDP协议的客户/服务端中文聊天程序
查看>>
android:使用BaseExpandableListAdapter实现可折叠的列表
查看>>
什么时候用存储过程
查看>>
【MongoDB for Java】Java操作MongoDB
查看>>
0c-42-ARC模式下如何兼容非ARC的类
查看>>
程序员进阶之路—如何独当一面
查看>>
闲话WPF之十一(Dependency属性 [3] )
查看>>
JS组件系列——基于Bootstrap Ace模板的菜单Tab页效果优化
查看>>
eclipse中tomcat快速启动关闭参数设置
查看>>
C++ extern "c "的作用
查看>>
实践:几十亿条数据分布在几十个节点上的毫秒级实时排序方法
查看>>
PMWiki安装教程
查看>>
JAVA多线程之volatile 与 synchronized 的比较
查看>>
一个经典编程面试题的“隐退”
查看>>
POJ2109
查看>>
显示创建一个表的SQL语句
查看>>
光流和KLT
查看>>
Linux c括号作用域【原创笔记】
查看>>