实验三 进程调度模拟程序

 

实验三 进程调度模拟程序

1.    目的和要求

1.1.           实验目的

用高级语言完成一个进程调度程序,以加深对进程的概念及进程调度算法的理解。

1.2.           实验要求

1.2.1例题:设计一个有 N个进程并发执行的进程调度模拟程序。

进程调度算法:采用最高优先级优先的调度算法(即把处理机分配给优先级最高的进程)和先来先服务(若优先级相同)算法。

(1).  每个进程有一个进程控制块(PCB)表示。进程控制块包含如下信息:进程名、优先级、到达时间、需要运行时间、已用CPU时间、进程状态等等。

(2).  进程的优先级及需要的运行时间可以事先人为地指定,进程的运行时间以时间片为单位进行计算。

(3).  每个进程的状态可以是就绪 r(ready)、运行R(Running)、或完成F(Finished)三种状态之一。

(4).  就绪进程获得 CPU后都只能运行一个时间片。用已占用CPU时间加1来表示。

(5).  如果运行一个时间片后,进程的已占用 CPU时间已达到所需要的运行时间,则撤消该进程,如果运行一个时间片后进程的已占用CPU时间还未达所需要的运行时间,也就是进程还需要继续运行,此时应将进程的优先数减1(即降低一级),然后把它插入就绪队列等待调度。

(6).  每进行一次调度程序都打印一次运行进程、就绪队列中各个进程的 PCB,以便进行检查。   

(7).  重复以上过程,直到所要进程都完成为止。

思考:作业调度与进程调度的不同?

1.2.2实验题A:编写并调试一个模拟的进程调度程序,采用“最高优先数优先”调度算法对N(N不小于5)个进程进行调度。

“最高优先级优先”调度算法的基本思想是把CPU分配给就绪队列中优先数最高的进程。

(1). 静态优先数是在创建进程时确定的,并在整个进程运行期间不再改变。

(2). 动态优先数是指进程的优先数在创建进程时可以给定一个初始值,并且可以按一定规则修改优先数。例如:在进程获得一次CPU后就将其优先数减少1,并且进程等待的时间超过某一时限(2个时间片时间)时增加其优先数等。

(3). (**)进程的优先数及需要的运行时间可以事先人为地指定,(也可以由随机数产生)。

(4). (**)在进行模拟调度过程可以创建(增加)进程,其到达时间为进程输入的时间。

0.

 

1.2.3实验题B:编写并调试一个模拟的进程调度程序,采用“基于时间片轮转法”调度算法对N(N不小于5)个进程进行调度。 “轮转法”有简单轮转法、多级反馈队列调度算法。

(1). 简单轮转法的基本思想是:所有就绪进程按 FCFS排成一个队列,总是把处理机分配给队首的进程,各进程占用CPU的时间片长度相同。如果运行进程用完它的时间片后还未完成,就把它送回到就绪队列的末尾,把处理机重新分配给队首的进程。直至所有的进程运行完毕。(此调度算法是否有优先级?)

 (2). 多级反馈队列调度算法的基本思想是:

将就绪队列分为N级(N=3~5),每个就绪队列优先数不同并且分配给不同的时间片:队列级别越高,优先数越低,时间片越长;级别越小,优先数越高,时间片越短。

系统从第一级调度,当第一级为空时,系统转向第二级队列,.....当处于运行态的进程用完一个时间片,若未完成则放弃CPU,进入下一级队列。

当进程第一次就绪时,进入第一级队列。

(3). (**)考虑进程的阻塞状态B(Blocked)增加阻塞队列。进程的是否阻塞和阻塞的时间由产生的“随机数”确定(阻塞的频率和时间长度要较为合理)。注意进程只有处于运行状态才可能转换成阻塞状态,进程只有处于就绪状态才可以转换成运行状态。

 

2.    实验内容

根据指定的实验课题:A(1),A(2),B(1)和B(2)

完成设计、编码和调试工作,完成实验报告。

注:带**号的条目表示选做内容。

 

3.    实验环境

可以选用Turbo C作为开发环境。也可以选用Windows下的VB,CB等可视化环境,利用各种控件较为方便。自主选择实验环境。

4.    实验原理及核心算法参考程序段

     动态优先数(优先数只减不加):

        

 

  1 #include<sdio.h>
  2 #include<string.h>
  3 void shuru();
  4 void readfiles();
  5 void ran();
  6 void sort(int choose);//1为到达时间排序,2为服务时间排序,3为HRRF排序,4为优先级排序
  7 void FinishProcess();
  8 typedef struct Process
  9 {
 10     char name[10];
 11     char status;    //状态:开始s,等待d,就绪j
 12     int id;
 13     int arrtime;    //到达时间
 14     int reqtime;    //要求服务时间
 15     int startime;    //开始时间
 16     int finitime;    //完成时间
 17     int dotime;        //已经工作时间
 18 
 19     float TAtime,TAWtime;    //周转时间、带权周转时间
 20     float prio;        //优先级
 21     float HRRF;            //响应比
 22 
 23 }Process;
 24 
 25 
 26 Process Processes[100]; //最多100个作业
 27 int systime=0,bfsum,add,del;
 28 int intarr,infin,intjob,sumProcess;
 29 char T='A';              //T的默认值是A
 30 
 31 void RunProcess()
 32 {
 33     int i=1;
 34     sort(4);//按prio排序
 35     printf("\n————开始运行————\n");
 36     for(i=1;i<=sumProcess;i++)
 37     {
 38         printf("\n\tProcess %s\t%f\n",Processes[i].name,Processes[i].prio);
 39     }
 40     Processes[1].dotime++;
 41     if(Processes[1].dotime==Processes[1].reqtime)
 42     {
 43         FinishProcess();
 44     }
 45     Processes[1].prio--;
 46 }
 47 
 48 void FinishProcess()
 49 {
 50     int i,j;
 51     Process temp;
 52     for(i=1;i<=sumProcess;i++)
 53     {
 54         j=i-1;
 55                     strcpy(temp.name,Processes[i].name);
 56                     temp.status=Processes[i].status;
 57                     temp.arrtime=Processes[i].arrtime;
 58                     temp.reqtime=Processes[i].reqtime;
 59                     temp.startime=Processes[i].startime;
 60                     temp.finitime=Processes[i].finitime;
 61                     temp.TAtime=Processes[i].TAtime;
 62                     temp.TAWtime=Processes[i].TAWtime;
 63                     temp.prio=Processes[i].prio;
 64                     temp.id=Processes[i].id;
 65                     temp.HRRF=Processes[i].HRRF;
 66                     temp.dotime=Processes[i].dotime;
 67 
 68                     strcpy(Processes[i].name,Processes[j].name);
 69                     Processes[i].status=Processes[j].status;
 70                     Processes[i].arrtime=Processes[j].arrtime;
 71                     Processes[i].reqtime=Processes[j].reqtime;
 72                     Processes[i].startime=Processes[j].startime;
 73                     Processes[i].finitime=Processes[j].finitime;
 74                     Processes[i].TAtime=Processes[j].TAtime;
 75                     Processes[i].TAWtime=Processes[j].TAWtime;
 76                     Processes[i].prio=Processes[j].prio;
 77                     Processes[i].id=Processes[j].id;
 78                     Processes[i].HRRF=Processes[j].HRRF;
 79                     Processes[i].dotime=Processes[j].dotime;
 80 
 81                     strcpy(Processes[j].name,temp.name);
 82                     Processes[j].status=temp.status;
 83                     Processes[j].arrtime=temp.arrtime;
 84                     Processes[j].reqtime=temp.reqtime;
 85                     Processes[j].startime=temp.startime;
 86                     Processes[j].finitime=temp.finitime;
 87                     Processes[j].TAtime=temp.TAtime;
 88                     Processes[j].TAWtime=temp.TAWtime;
 89                     Processes[j].prio=temp.prio;
 90                     Processes[j].id=temp.id;
 91                     Processes[j].HRRF=temp.HRRF;
 92                     Processes[j].dotime=temp.dotime;
 93     }
 94     sumProcess--;
 95 }
 96 
 97 
 98 
 99 void sort(int choose)  //排序
100 {
101     int i,j;
102     Process temp;
103     for(i=1;i<=sumProcess;i++)
104     {
105         for(j=i+1;j<=sumProcess;j++)
106         {
107             if(choose==1)
108             {
109                 if(Processes[i].arrtime>Processes[j].arrtime)
110                 {
111                     strcpy(temp.name,Processes[i].name);
112                     temp.status=Processes[i].status;
113                     temp.arrtime=Processes[i].arrtime;
114                     temp.reqtime=Processes[i].reqtime;
115                     temp.startime=Processes[i].startime;
116                     temp.finitime=Processes[i].finitime;
117                     temp.TAtime=Processes[i].TAtime;
118                     temp.TAWtime=Processes[i].TAWtime;
119                     temp.prio=Processes[i].prio;
120                     temp.id=Processes[i].id;
121                     temp.HRRF=Processes[i].HRRF;
122                     temp.dotime=Processes[i].dotime;
123 
124                     strcpy(Processes[i].name,Processes[j].name);
125                     Processes[i].status=Processes[j].status;
126                     Processes[i].arrtime=Processes[j].arrtime;
127                     Processes[i].reqtime=Processes[j].reqtime;
128                     Processes[i].startime=Processes[j].startime;
129                     Processes[i].finitime=Processes[j].finitime;
130                     Processes[i].TAtime=Processes[j].TAtime;
131                     Processes[i].TAWtime=Processes[j].TAWtime;
132                     Processes[i].prio=Processes[j].prio;
133                     Processes[i].id=Processes[j].id;
134                     Processes[i].HRRF=Processes[j].HRRF;
135                     Processes[i].dotime=Processes[j].dotime;
136 
137                     strcpy(Processes[j].name,temp.name);
138                     Processes[j].status=temp.status;
139                     Processes[j].arrtime=temp.arrtime;
140                     Processes[j].reqtime=temp.reqtime;
141                     Processes[j].startime=temp.startime;
142                     Processes[j].finitime=temp.finitime;
143                     Processes[j].TAtime=temp.TAtime;
144                     Processes[j].TAWtime=temp.TAWtime;
145                     Processes[j].prio=temp.prio;
146                     Processes[j].id=temp.id;
147                     Processes[j].HRRF=temp.HRRF;
148                     Processes[j].dotime=temp.dotime;
149                 }
150             }
151             else if(choose==2)
152             {
153                 if(Processes[i].reqtime>Processes[j].reqtime)
154                 {
155                     strcpy(temp.name,Processes[i].name);
156                     temp.status=Processes[i].status;
157                     temp.arrtime=Processes[i].arrtime;
158                     temp.reqtime=Processes[i].reqtime;
159                     temp.startime=Processes[i].startime;
160                     temp.finitime=Processes[i].finitime;
161                     temp.TAtime=Processes[i].TAtime;
162                     temp.TAWtime=Processes[i].TAWtime;
163                     temp.prio=Processes[i].prio;
164                     temp.id=Processes[i].id;
165                     temp.HRRF=Processes[i].HRRF;
166                     temp.dotime=Processes[i].dotime;
167 
168                     strcpy(Processes[i].name,Processes[j].name);
169                     Processes[i].status=Processes[j].status;
170                     Processes[i].arrtime=Processes[j].arrtime;
171                     Processes[i].reqtime=Processes[j].reqtime;
172                     Processes[i].startime=Processes[j].startime;
173                     Processes[i].finitime=Processes[j].finitime;
174                     Processes[i].TAtime=Processes[j].TAtime;
175                     Processes[i].TAWtime=Processes[j].TAWtime;
176                     Processes[i].prio=Processes[j].prio;
177                     Processes[i].id=Processes[j].id;
178                     Processes[i].HRRF=Processes[j].HRRF;
179                     Processes[i].dotime=Processes[j].dotime;
180 
181                     strcpy(Processes[j].name,temp.name);
182                     Processes[j].status=temp.status;
183                     Processes[j].arrtime=temp.arrtime;
184                     Processes[j].reqtime=temp.reqtime;
185                     Processes[j].startime=temp.startime;
186                     Processes[j].finitime=temp.finitime;
187                     Processes[j].TAtime=temp.TAtime;
188                     Processes[j].TAWtime=temp.TAWtime;
189                     Processes[j].prio=temp.prio;
190                     Processes[j].id=temp.id;
191                     Processes[j].HRRF=temp.HRRF;
192                     Processes[j].dotime=temp.dotime;
193                 }
194             }
195             else if(choose==3)
196             {
197                 if(Processes[i].HRRF<Processes[j].HRRF)
198                 {
199                     strcpy(temp.name,Processes[i].name);
200                     temp.status=Processes[i].status;
201                     temp.arrtime=Processes[i].arrtime;
202                     temp.reqtime=Processes[i].reqtime;
203                     temp.startime=Processes[i].startime;
204                     temp.finitime=Processes[i].finitime;
205                     temp.TAtime=Processes[i].TAtime;
206                     temp.TAWtime=Processes[i].TAWtime;
207                     temp.prio=Processes[i].prio;
208                     temp.id=Processes[i].id;
209                     temp.HRRF=Processes[i].HRRF;
210                     temp.dotime=Processes[i].dotime;
211 
212                     strcpy(Processes[i].name,Processes[j].name);
213                     Processes[i].status=Processes[j].status;
214                     Processes[i].arrtime=Processes[j].arrtime;
215                     Processes[i].reqtime=Processes[j].reqtime;
216                     Processes[i].startime=Processes[j].startime;
217                     Processes[i].finitime=Processes[j].finitime;
218                     Processes[i].TAtime=Processes[j].TAtime;
219                     Processes[i].TAWtime=Processes[j].TAWtime;
220                     Processes[i].prio=Processes[j].prio;
221                     Processes[i].id=Processes[j].id;
222                     Processes[i].HRRF=Processes[j].HRRF;
223                     Processes[i].dotime=Processes[j].dotime;
224 
225                     strcpy(Processes[j].name,temp.name);
226                     Processes[j].status=temp.status;
227                     Processes[j].arrtime=temp.arrtime;
228                     Processes[j].reqtime=temp.reqtime;
229                     Processes[j].startime=temp.startime;
230                     Processes[j].finitime=temp.finitime;
231                     Processes[j].TAtime=temp.TAtime;
232                     Processes[j].TAWtime=temp.TAWtime;
233                     Processes[j].prio=temp.prio;
234                     Processes[j].id=temp.id;
235                     Processes[j].HRRF=temp.HRRF;
236                     Processes[j].dotime=temp.dotime;
237                 }
238             }
239             else if(choose==4)
240             {
241                 if(Processes[i].prio<Processes[j].prio)
242                 {
243                     strcpy(temp.name,Processes[i].name);
244                     temp.status=Processes[i].status;
245                     temp.arrtime=Processes[i].arrtime;
246                     temp.reqtime=Processes[i].reqtime;
247                     temp.startime=Processes[i].startime;
248                     temp.finitime=Processes[i].finitime;
249                     temp.TAtime=Processes[i].TAtime;
250                     temp.TAWtime=Processes[i].TAWtime;
251                     temp.prio=Processes[i].prio;
252                     temp.id=Processes[i].id;
253                     temp.HRRF=Processes[i].HRRF;
254                     temp.dotime=Processes[i].dotime;
255 
256                     strcpy(Processes[i].name,Processes[j].name);
257                     Processes[i].status=Processes[j].status;
258                     Processes[i].arrtime=Processes[j].arrtime;
259                     Processes[i].reqtime=Processes[j].reqtime;
260                     Processes[i].startime=Processes[j].startime;
261                     Processes[i].finitime=Processes[j].finitime;
262                     Processes[i].TAtime=Processes[j].TAtime;
263                     Processes[i].TAWtime=Processes[j].TAWtime;
264                     Processes[i].prio=Processes[j].prio;
265                     Processes[i].id=Processes[j].id;
266                     Processes[i].HRRF=Processes[j].HRRF;
267                     Processes[i].dotime=Processes[j].dotime;
268 
269                     strcpy(Processes[j].name,temp.name);
270                     Processes[j].status=temp.status;
271                     Processes[j].arrtime=temp.arrtime;
272                     Processes[j].reqtime=temp.reqtime;
273                     Processes[j].startime=temp.startime;
274                     Processes[j].finitime=temp.finitime;
275                     Processes[j].TAtime=temp.TAtime;
276                     Processes[j].TAWtime=temp.TAWtime;
277                     Processes[j].prio=temp.prio;
278                     Processes[j].id=temp.id;
279                     Processes[j].HRRF=temp.HRRF;
280                     Processes[j].dotime=temp.dotime;
281                 }
282             }
283             else 
284                 printf("Error!\n");
285         }
286     }
287 }
288 void ins()    //插入
289 {
290     int i;
291     while(T!='E'&&T!='e')
292     {
293             printf("'I'nsert or 'D'elete or 'E'xit?\n");
294             getchar();
295             scanf("%c",&T);
296             if(T=='I'||T=='i')
297             {
298                 printf("你想插入多少个作业?\n");
299                 scanf("%d",&add);
300                 bfsum=sumProcess;
301                 sumProcess=sumProcess+add;
302                 for(i=bfsum+1;i<=sumProcess;i++)
303                 {
304                     printf("\n第%d个作业:\n",i);
305                     getchar();
306                     printf("输入作业名:\n");
307                     gets(Processes[i].name);
308                     printf("到达时间:\n");
309                     scanf("%d",&Processes[i].arrtime);
310                     printf("要求服务时间:\n");
311                     scanf("%d",&Processes[i].reqtime);
312                 }
313             }
314             else if(T=='D'||T=='d')
315             {
316                 printf("你想删除第几组?");
317                 scanf("%d",&del);
318                 for(i=del;i<=sumProcess-1;i++)
319                 {
320                     strcpy(Processes[i].name,Processes[i+1].name);
321                     Processes[i].arrtime=Processes[i+1].arrtime;
322                     Processes[i].reqtime=Processes[i+1].reqtime;
323                 }
324                 sumProcess--;
325             }
326     }
327 }
328 
329 
330 void printarr()    //打印
331 {
332     int i;
333     printf("\t\tname\tartime\trqtime\n");
334     for(i=1;i<=sumProcess;i++)
335     {
336         printf("N  %d\t",i);
337         printf("\t%s",Processes[i].name);
338         printf("\t%d",Processes[i].arrtime);
339         printf("\t%d\n",Processes[i].reqtime);
340     }
341     printf("\t\t\t\t\t\t现在系统时间%d\n",systime);
342     
343 }
344 void printz()
345 {
346     int i;
347     float sum1=0,sum2=0;    //计算平均周转时间、平均带权周转时间
348     printf("\t\tname\tartime\trqtime\tstime\tftime\tTAtime\t\tTAWtime\n");
349     for(i=1;i<=sumProcess;i++)
350     {
351         printf("N  %d\t",i);
352         printf("\t%s",Processes[i].name);
353         printf("\t%d",Processes[i].arrtime);
354         printf("\t%d",Processes[i].reqtime);
355         printf("\t%d",Processes[i].startime);
356         printf("\t%d",Processes[i].finitime);
357         printf("\t%f",Processes[i].TAtime);
358         printf("\t%f\n",Processes[i].TAWtime);
359     }
360     for(i=1;i<=sumProcess;i++)
361     {
362         sum1=sum1+Processes[i].TAtime;
363     }
364     for(i=1;i<=sumProcess;i++)
365     {
366         sum2=sum2+Processes[i].TAWtime;
367     }
368     printf("\n平均周转时间=%f\n",sum1/sumProcess);
369     printf("\n平均带权周转时间=%f\n",sum2/sumProcess);
370     printf("\t\t\t\t\t\t现在系统时间%d\n",systime);
371 }
372 void sumofthey()
373 {
374     int i;
375     Processes[1].startime=Processes[1].arrtime;
376     Processes[1].finitime=Processes[1].arrtime+Processes[1].reqtime;
377     Processes[1].TAtime=(float)(Processes[1].finitime-Processes[1].arrtime);
378     Processes[1].TAWtime=(float)(Processes[1].TAtime/Processes[1].reqtime);
379     Processes[1].HRRF=(float)(Processes[1].TAtime/Processes[1].reqtime);
380     for(i=2;i<=sumProcess;i++)
381     {
382         Processes[i].startime=Processes[i-1].finitime;
383         Processes[i].finitime=Processes[i].startime+Processes[i].reqtime;
384         Processes[i].TAtime=(float)(Processes[i].finitime-Processes[i].arrtime);
385         Processes[i].TAWtime=(float)(Processes[i].TAtime/Processes[i].reqtime);
386         Processes[i].HRRF=(float)(Processes[i].TAtime/Processes[i].reqtime);
387     }
388 }
389 void startit()
390 {
391     int n;
392     printf("\n");
393     printf("\t\t\t**************************\n");
394     printf("\t\t\t**1.调用文本写入数据******\n");
395     printf("\t\t\t**2.调用伪随机数产生数据**\n");
396     printf("\t\t\t**3.调用自己输入模拟数据**\n");
397     printf("\t\t\t**************************\n");
398     scanf("%d",&n);
399     switch(n)
400     {
401     case 1:
402         readfiles();
403         sumofthey();
404         break;
405     case 2:
406         ran();
407         sumofthey();
408         break;
409     case 3:
410         shuru();
411         sumofthey();
412         break;
413     default:
414         {
415         printf("输入有误,请重新输入");
416         break;
417         }
418     }
419 }
420 void ran()
421 {
422     int i;
423     srand((unsigned)time(0));  //参数seed是rand()的种子,用来初始化rand()的起始值。
424     sumProcess=rand()%3+5;
425     for(i=1;i<=sumProcess;i++)
426     {
427         itoa(i,Processes[i].name,2);
428      
429         Processes[i].arrtime=rand()%29+1;
430     
431         Processes[i].reqtime=rand()%7+1;
432 
433         Processes[i].prio=rand()%10+1;
434     }
435     printf("\n \tid    \t作业到达时间     \t作业运行所需要时间     \t优先级\n");
436     for(i=1; i<=sumProcess; i++)
437     {
438         printf("\n\t%s\t%12d\t%15d\t%25f",Processes[i].name,Processes[i].arrtime,Processes[i].reqtime,Processes[i].prio);
439     }
440    printf("\n");
441 }
442 
443 void readfiles()
444 {
445     char szTest[1000] = {0};int i=0,n;
446     FILE *fp=fopen("D:\\File1.txt","r");
447     //FILE *fout=fopen("D:\\File2.txt","w");
448     if(fp == NULL)
449     {
450         printf("ERROR!\n");
451         exit(0);
452     }
453     //fscanf(fp,"Processesum=%d\n",%n);
454     while(!feof(fp))  
455     {
456         fscanf(fp,"%d%d%d",&Processes[i].id,&Processes[i].arrtime,&Processes[i].reqtime);  //fscanf()函数将数据读入
457         printf("\n%3d%12d%15d\n",Processes[i].id,Processes[i].arrtime,Processes[i].reqtime);  //输出到屏幕
458         itoa(Processes[i].id,Processes[i].name,10);
459         i++;
460         /*memset(szTest, 0, sizeof(szTest));
461         fscanf(fp,"@id=%d|",&i);
462         fgets(fp,"name=%s|",Processes[i].name);
463         fscanf(fp,"arrtime=%d|reqtime=%d\n",&Processes[i].arrtime,&Processes[i].reqtime);
464         */
465     //fscanf(fp,"%d",)
466     }
467     sumProcess=i;
468     fclose(fp);
469 }
470 void shuru()
471 {
472     int i;
473     printf("作业个数:\n");
474     scanf("%d",&sumProcess);
475     for(i=1;i<=sumProcess;i++)
476     {
477         printf("\n第%d个作业:\n",i);
478         getchar();
479         printf("输入作业名:\n");
480         gets(Processes[i].name);
481         printf("到达时间:\n");
482         scanf("%d",&Processes[i].arrtime);
483         printf("要求服务时间:\n");
484         scanf("%d",&Processes[i].reqtime);
485     }
486     sumofthey();
487 }
488 void choosesort()
489 {
490     int n;
491     printf("\t\t\t*****************************\n");
492     printf("\t\t\t*******1.FCFS算法调度********\n");
493     printf("\t\t\t*******2.SJF算法调度*********\n");
494     printf("\t\t\t*******3.HRRF算法调度********\n");
495     printf("\t\t\t*******4.调用系统清屏********\n");
496     printf("\t\t\t*******5.退出算法调度********\n");
497     printf("\t\t\t*****************************\n");
498     scanf("%d",&n);
499     switch(n)
500     {
501     case 1:
502         printf("运行先来先服务算法FCFS\n");
503         sort(1);
504         printf("\n经按到达时间排序后,未达到队列是\n");
505         printz();
506         break;
507     case 2:
508         printf("运行最短作业优先算法FJS\n");
509         sort(2);
510         printf("\n经按到达时间排序后,未达到队列是\n");
511         printz();
512         break;
513     case 3:
514         printf("运行最高响应比优先算法HRRF\n");
515         sort(3);
516         printf("\n经按到达时间排序后,未达到队列是\n");
517         printz();
518         break;
519     case 4:
520         system("cls");
521         break;
522     case 5:
523         exit(0);
524         break;
525     }
526 }
527 void main()
528 {
529     startit();
530 //    choosesort();
531     while(sumProcess!=0)
532     {
533         RunProcess();
534     }
535 
536 }

 

  1 #include<stdio.h>
  2 #include<string.h>
  3 void shuru();
  4 void readfiles();
  5 void ran();
  6 void sort(int choose);//1为到达时间排序,2为服务时间排序,3为HRRF排序,4为优先级排序
  7 void FinishProcess();
  8 typedef struct Process
  9 {
 10     char name[10];
 11     char status;    //状态:开始s,等待d,就绪j
 12     int id;
 13     int arrtime;    //到达时间
 14     int reqtime;    //要求服务时间
 15     int startime;    //开始时间
 16     int finitime;    //完成时间
 17     int dotime;        //已经工作时间
 18 
 19     float TAtime,TAWtime;    //周转时间、带权周转时间
 20     float prio;        //优先级
 21     float HRRF;            //响应比
 22 
 23 }Process;
 24 
 25 
 26 Process Processes[100]; //最多100个作业
 27 int systime=0,bfsum,add,del;
 28 int intarr,infin,intjob,sumProcess;
 29 char T='A';              //T的默认值是A
 30 
 31 void RunProcess()
 32 {
 33     int timeflag=0;
 34     int timepiece=2;
 35     int T;
 36     int k;
 37     char ch;
 38     int i=1;
 39 
 40     printf("\n请输入时间片:");
 41     scanf("%d",&T);
 42 
 43     sort(1);//按FCFS排序
 44     printf("\n————开始运行————\n");
 45     while(sumProcess>=0)
 46     {
 47         if(timeflag==T)
 48         {
 49             timeflag=0;
 50             if(Processes[0].reqtime==0)
 51             {
 52             printf("————进程%s已完成————\n",Processes[0].name);
 53             
 54 
 55             if(sumProcess!=0){
 56                 sort(1);
 57                 printf("当前正在运行进程是:%s————\n",Processes[0].name);
 58             }
 59             if(sumProcess>=1)
 60                 for(k=1;k<sumProcess;k++)
 61                 printf("————进程%s正在等待————\n",Processes[k].name);
 62             if(sumProcess==0){
 63                 Processes[0].reqtime--;
 64                 sumProcess--;
 65             }
 66             }
 67             else{
 68                 sort(1);
 69                 if(sumProcess!=0){
 70                 //sort();
 71                 printf("当前正在运行进程是:%s————\n",Processes[0].name);
 72                 }
 73                 if(sumProcess>=1)
 74                 for(k=1;k<sumProcess;k++)
 75                 printf("————进程%s正在等待————\n",Processes[k].name);
 76             }              
 77         }
 78         else{
 79             if(Processes[0].reqtime==0)
 80             {
 81             printf("————进程%s已完成————\n",Processes[0].name);
 82             if(sumProcess!=0){
 83                 sort(1);
 84                 printf("————进程%s正在运行————\n",Processes[0].name);
 85             }
 86             if(sumProcess>=1)
 87                 for(k=1;k<sumProcess;k++)
 88                 printf("————进程%s正在等待————\n",Processes[k].name);
 89             }
 90             else{
 91                 Processes[0].reqtime--;
 92                 if(sumProcess!=0)
 93                 printf("————进程%s正在运行————\n",Processes[0].name);
 94             
 95                 if(sumProcess>=1)
 96                 for(k=1;k<sumProcess;k++)
 97                 printf("————进程%s正在等待————\n",Processes[k].name);
 98             }
 99         }
100         timeflag++;
101         printf("\n 按任回车继续......"); 
102         ch=getchar(); 
103         ch=getchar();
104     }
105      printf("\n\n 全部进程已经完成.\n"); 
106 /*
107     for(i=1;i<=sumProcess;i++)
108     {
109         printf("\n\tProcess %s\t%f\n",Processes[i].name,Processes[i].prio);
110     }
111     Processes[1].dotime++;
112     if(Processes[1].dotime==Processes[1].reqtime)
113     {
114         FinishProcess();
115     }
116     Processes[1].prio--;*/
117 }
118 
119 void FinishProcess()
120 {
121     int i,j;
122     Process temp;
123     for(i=1;i<=sumProcess;i++)
124     {
125         j=i-1;
126                     strcpy(temp.name,Processes[i].name);
127                     temp.status=Processes[i].status;
128                     temp.arrtime=Processes[i].arrtime;
129                     temp.reqtime=Processes[i].reqtime;
130                     temp.startime=Processes[i].startime;
131                     temp.finitime=Processes[i].finitime;
132                     temp.TAtime=Processes[i].TAtime;
133                     temp.TAWtime=Processes[i].TAWtime;
134                     temp.prio=Processes[i].prio;
135                     temp.id=Processes[i].id;
136                     temp.HRRF=Processes[i].HRRF;
137                     temp.dotime=Processes[i].dotime;
138 
139                     strcpy(Processes[i].name,Processes[j].name);
140                     Processes[i].status=Processes[j].status;
141                     Processes[i].arrtime=Processes[j].arrtime;
142                     Processes[i].reqtime=Processes[j].reqtime;
143                     Processes[i].startime=Processes[j].startime;
144                     Processes[i].finitime=Processes[j].finitime;
145                     Processes[i].TAtime=Processes[j].TAtime;
146                     Processes[i].TAWtime=Processes[j].TAWtime;
147                     Processes[i].prio=Processes[j].prio;
148                     Processes[i].id=Processes[j].id;
149                     Processes[i].HRRF=Processes[j].HRRF;
150                     Processes[i].dotime=Processes[j].dotime;
151 
152                     strcpy(Processes[j].name,temp.name);
153                     Processes[j].status=temp.status;
154                     Processes[j].arrtime=temp.arrtime;
155                     Processes[j].reqtime=temp.reqtime;
156                     Processes[j].startime=temp.startime;
157                     Processes[j].finitime=temp.finitime;
158                     Processes[j].TAtime=temp.TAtime;
159                     Processes[j].TAWtime=temp.TAWtime;
160                     Processes[j].prio=temp.prio;
161                     Processes[j].id=temp.id;
162                     Processes[j].HRRF=temp.HRRF;
163                     Processes[j].dotime=temp.dotime;
164     }
165     sumProcess--;
166 }
167 
168 
169 
170 void sort(int choose)  //排序
171 {
172     int i,j;
173     Process temp;
174     for(i=1;i<=sumProcess;i++)
175     {
176         for(j=i+1;j<=sumProcess;j++)
177         {
178             if(choose==1)
179             {
180                 if(Processes[i].arrtime>Processes[j].arrtime)
181                 {
182                     strcpy(temp.name,Processes[i].name);
183                     temp.status=Processes[i].status;
184                     temp.arrtime=Processes[i].arrtime;
185                     temp.reqtime=Processes[i].reqtime;
186                     temp.startime=Processes[i].startime;
187                     temp.finitime=Processes[i].finitime;
188                     temp.TAtime=Processes[i].TAtime;
189                     temp.TAWtime=Processes[i].TAWtime;
190                     temp.prio=Processes[i].prio;
191                     temp.id=Processes[i].id;
192                     temp.HRRF=Processes[i].HRRF;
193                     temp.dotime=Processes[i].dotime;
194 
195                     strcpy(Processes[i].name,Processes[j].name);
196                     Processes[i].status=Processes[j].status;
197                     Processes[i].arrtime=Processes[j].arrtime;
198                     Processes[i].reqtime=Processes[j].reqtime;
199                     Processes[i].startime=Processes[j].startime;
200                     Processes[i].finitime=Processes[j].finitime;
201                     Processes[i].TAtime=Processes[j].TAtime;
202                     Processes[i].TAWtime=Processes[j].TAWtime;
203                     Processes[i].prio=Processes[j].prio;
204                     Processes[i].id=Processes[j].id;
205                     Processes[i].HRRF=Processes[j].HRRF;
206                     Processes[i].dotime=Processes[j].dotime;
207 
208                     strcpy(Processes[j].name,temp.name);
209                     Processes[j].status=temp.status;
210                     Processes[j].arrtime=temp.arrtime;
211                     Processes[j].reqtime=temp.reqtime;
212                     Processes[j].startime=temp.startime;
213                     Processes[j].finitime=temp.finitime;
214                     Processes[j].TAtime=temp.TAtime;
215                     Processes[j].TAWtime=temp.TAWtime;
216                     Processes[j].prio=temp.prio;
217                     Processes[j].id=temp.id;
218                     Processes[j].HRRF=temp.HRRF;
219                     Processes[j].dotime=temp.dotime;
220                 }
221             }
222             else if(choose==2)
223             {
224                 if(Processes[i].reqtime>Processes[j].reqtime)
225                 {
226                     strcpy(temp.name,Processes[i].name);
227                     temp.status=Processes[i].status;
228                     temp.arrtime=Processes[i].arrtime;
229                     temp.reqtime=Processes[i].reqtime;
230                     temp.startime=Processes[i].startime;
231                     temp.finitime=Processes[i].finitime;
232                     temp.TAtime=Processes[i].TAtime;
233                     temp.TAWtime=Processes[i].TAWtime;
234                     temp.prio=Processes[i].prio;
235                     temp.id=Processes[i].id;
236                     temp.HRRF=Processes[i].HRRF;
237                     temp.dotime=Processes[i].dotime;
238 
239                     strcpy(Processes[i].name,Processes[j].name);
240                     Processes[i].status=Processes[j].status;
241                     Processes[i].arrtime=Processes[j].arrtime;
242                     Processes[i].reqtime=Processes[j].reqtime;
243                     Processes[i].startime=Processes[j].startime;
244                     Processes[i].finitime=Processes[j].finitime;
245                     Processes[i].TAtime=Processes[j].TAtime;
246                     Processes[i].TAWtime=Processes[j].TAWtime;
247                     Processes[i].prio=Processes[j].prio;
248                     Processes[i].id=Processes[j].id;
249                     Processes[i].HRRF=Processes[j].HRRF;
250                     Processes[i].dotime=Processes[j].dotime;
251 
252                     strcpy(Processes[j].name,temp.name);
253                     Processes[j].status=temp.status;
254                     Processes[j].arrtime=temp.arrtime;
255                     Processes[j].reqtime=temp.reqtime;
256                     Processes[j].startime=temp.startime;
257                     Processes[j].finitime=temp.finitime;
258                     Processes[j].TAtime=temp.TAtime;
259                     Processes[j].TAWtime=temp.TAWtime;
260                     Processes[j].prio=temp.prio;
261                     Processes[j].id=temp.id;
262                     Processes[j].HRRF=temp.HRRF;
263                     Processes[j].dotime=temp.dotime;
264                 }
265             }
266             else if(choose==3)
267             {
268                 if(Processes[i].HRRF<Processes[j].HRRF)
269                 {
270                     strcpy(temp.name,Processes[i].name);
271                     temp.status=Processes[i].status;
272                     temp.arrtime=Processes[i].arrtime;
273                     temp.reqtime=Processes[i].reqtime;
274                     temp.startime=Processes[i].startime;
275                     temp.finitime=Processes[i].finitime;
276                     temp.TAtime=Processes[i].TAtime;
277                     temp.TAWtime=Processes[i].TAWtime;
278                     temp.prio=Processes[i].prio;
279                     temp.id=Processes[i].id;
280                     temp.HRRF=Processes[i].HRRF;
281                     temp.dotime=Processes[i].dotime;
282 
283                     strcpy(Processes[i].name,Processes[j].name);
284                     Processes[i].status=Processes[j].status;
285                     Processes[i].arrtime=Processes[j].arrtime;
286                     Processes[i].reqtime=Processes[j].reqtime;
287                     Processes[i].startime=Processes[j].startime;
288                     Processes[i].finitime=Processes[j].finitime;
289                     Processes[i].TAtime=Processes[j].TAtime;
290                     Processes[i].TAWtime=Processes[j].TAWtime;
291                     Processes[i].prio=Processes[j].prio;
292                     Processes[i].id=Processes[j].id;
293                     Processes[i].HRRF=Processes[j].HRRF;
294                     Processes[i].dotime=Processes[j].dotime;
295 
296                     strcpy(Processes[j].name,temp.name);
297                     Processes[j].status=temp.status;
298                     Processes[j].arrtime=temp.arrtime;
299                     Processes[j].reqtime=temp.reqtime;
300                     Processes[j].startime=temp.startime;
301                     Processes[j].finitime=temp.finitime;
302                     Processes[j].TAtime=temp.TAtime;
303                     Processes[j].TAWtime=temp.TAWtime;
304                     Processes[j].prio=temp.prio;
305                     Processes[j].id=temp.id;
306                     Processes[j].HRRF=temp.HRRF;
307                     Processes[j].dotime=temp.dotime;
308                 }
309             }
310             else if(choose==4)
311             {
312                 if(Processes[i].prio<Processes[j].prio)
313                 {
314                     strcpy(temp.name,Processes[i].name);
315                     temp.status=Processes[i].status;
316                     temp.arrtime=Processes[i].arrtime;
317                     temp.reqtime=Processes[i].reqtime;
318                     temp.startime=Processes[i].startime;
319                     temp.finitime=Processes[i].finitime;
320                     temp.TAtime=Processes[i].TAtime;
321                     temp.TAWtime=Processes[i].TAWtime;
322                     temp.prio=Processes[i].prio;
323                     temp.id=Processes[i].id;
324                     temp.HRRF=Processes[i].HRRF;
325                     temp.dotime=Processes[i].dotime;
326 
327                     strcpy(Processes[i].name,Processes[j].name);
328                     Processes[i].status=Processes[j].status;
329                     Processes[i].arrtime=Processes[j].arrtime;
330                     Processes[i].reqtime=Processes[j].reqtime;
331                     Processes[i].startime=Processes[j].startime;
332                     Processes[i].finitime=Processes[j].finitime;
333                     Processes[i].TAtime=Processes[j].TAtime;
334                     Processes[i].TAWtime=Processes[j].TAWtime;
335                     Processes[i].prio=Processes[j].prio;
336                     Processes[i].id=Processes[j].id;
337                     Processes[i].HRRF=Processes[j].HRRF;
338                     Processes[i].dotime=Processes[j].dotime;
339 
340                     strcpy(Processes[j].name,temp.name);
341                     Processes[j].status=temp.status;
342                     Processes[j].arrtime=temp.arrtime;
343                     Processes[j].reqtime=temp.reqtime;
344                     Processes[j].startime=temp.startime;
345                     Processes[j].finitime=temp.finitime;
346                     Processes[j].TAtime=temp.TAtime;
347                     Processes[j].TAWtime=temp.TAWtime;
348                     Processes[j].prio=temp.prio;
349                     Processes[j].id=temp.id;
350                     Processes[j].HRRF=temp.HRRF;
351                     Processes[j].dotime=temp.dotime;
352                 }
353             }
354             else 
355                 printf("Error!\n");
356         }
357     }
358 }
359 void ins()    //插入
360 {
361     int i;
362     while(T!='E'&&T!='e')
363     {
364             printf("'I'nsert or 'D'elete or 'E'xit?\n");
365             getchar();
366             scanf("%c",&T);
367             if(T=='I'||T=='i')
368             {
369                 printf("你想插入多少个作业?\n");
370                 scanf("%d",&add);
371                 bfsum=sumProcess;
372                 sumProcess=sumProcess+add;
373                 for(i=bfsum+1;i<=sumProcess;i++)
374                 {
375                     printf("\n第%d个作业:\n",i);
376                     getchar();
377                     printf("输入作业名:\n");
378                     gets(Processes[i].name);
379                     printf("到达时间:\n");
380                     scanf("%d",&Processes[i].arrtime);
381                     printf("要求服务时间:\n");
382                     scanf("%d",&Processes[i].reqtime);
383                 }
384             }
385             else if(T=='D'||T=='d')
386             {
387                 printf("你想删除第几组?");
388                 scanf("%d",&del);
389                 for(i=del;i<=sumProcess-1;i++)
390                 {
391                     strcpy(Processes[i].name,Processes[i+1].name);
392                     Processes[i].arrtime=Processes[i+1].arrtime;
393                     Processes[i].reqtime=Processes[i+1].reqtime;
394                 }
395                 sumProcess--;
396             }
397     }
398 }
399 
400 
401 void printarr()    //打印
402 {
403     int i;
404     printf("\t\tname\tartime\trqtime\n");
405     for(i=1;i<=sumProcess;i++)
406     {
407         printf("N  %d\t",i);
408         printf("\t%s",Processes[i].name);
409         printf("\t%d",Processes[i].arrtime);
410         printf("\t%d\n",Processes[i].reqtime);
411     }
412     printf("\t\t\t\t\t\t现在系统时间%d\n",systime);
413     
414 }
415 void printz()
416 {
417     int i;
418     float sum1=0,sum2=0;    //计算平均周转时间、平均带权周转时间
419     printf("\t\tname\tartime\trqtime\tstime\tftime\tTAtime\t\tTAWtime\n");
420     for(i=1;i<=sumProcess;i++)
421     {
422         printf("N  %d\t",i);
423         printf("\t%s",Processes[i].name);
424         printf("\t%d",Processes[i].arrtime);
425         printf("\t%d",Processes[i].reqtime);
426         printf("\t%d",Processes[i].startime);
427         printf("\t%d",Processes[i].finitime);
428         printf("\t%f",Processes[i].TAtime);
429         printf("\t%f\n",Processes[i].TAWtime);
430     }
431     for(i=1;i<=sumProcess;i++)
432     {
433         sum1=sum1+Processes[i].TAtime;
434     }
435     for(i=1;i<=sumProcess;i++)
436     {
437         sum2=sum2+Processes[i].TAWtime;
438     }
439     printf("\n平均周转时间=%f\n",sum1/sumProcess);
440     printf("\n平均带权周转时间=%f\n",sum2/sumProcess);
441     printf("\t\t\t\t\t\t现在系统时间%d\n",systime);
442 }
443 void sumofthey()
444 {
445     int i;
446     Processes[1].startime=Processes[1].arrtime;
447     Processes[1].finitime=Processes[1].arrtime+Processes[1].reqtime;
448     Processes[1].TAtime=(float)(Processes[1].finitime-Processes[1].arrtime);
449     Processes[1].TAWtime=(float)(Processes[1].TAtime/Processes[1].reqtime);
450     Processes[1].HRRF=(float)(Processes[1].TAtime/Processes[1].reqtime);
451     for(i=2;i<=sumProcess;i++)
452     {
453         Processes[i].startime=Processes[i-1].finitime;
454         Processes[i].finitime=Processes[i].startime+Processes[i].reqtime;
455         Processes[i].TAtime=(float)(Processes[i].finitime-Processes[i].arrtime);
456         Processes[i].TAWtime=(float)(Processes[i].TAtime/Processes[i].reqtime);
457         Processes[i].HRRF=(float)(Processes[i].TAtime/Processes[i].reqtime);
458     }
459 }
460 void startit()
461 {
462     int n;
463     printf("\n");
464     printf("\t\t\t**************************\n");
465     printf("\t\t\t**1.调用文本写入数据******\n");
466     printf("\t\t\t**2.调用伪随机数产生数据**\n");
467     printf("\t\t\t**3.调用自己输入模拟数据**\n");
468     printf("\t\t\t**************************\n");
469     scanf("%d",&n);
470     switch(n)
471     {
472     case 1:
473         readfiles();
474         sumofthey();
475         break;
476     case 2:
477         ran();
478         sumofthey();
479         break;
480     case 3:
481         shuru();
482         sumofthey();
483         break;
484     default:
485         {
486         printf("输入有误,请重新输入");
487         break;
488         }
489     }
490 }
491 void ran()
492 {
493     int i;
494     srand((unsigned)time(0));  //参数seed是rand()的种子,用来初始化rand()的起始值。
495     sumProcess=rand()%3+5;
496     for(i=1;i<=sumProcess;i++)
497     {
498         itoa(i,Processes[i].name,2);
499      
500         Processes[i].arrtime=rand()%29+1;
501     
502         Processes[i].reqtime=rand()%7+1;
503 
504         Processes[i].prio=rand()%10+1;
505     }
506     printf("\n \tid    \t作业到达时间     \t作业运行所需要时间     \t优先级\n");
507     for(i=1; i<=sumProcess; i++)
508     {
509         printf("\n\t%s\t%12d\t%15d\t%25f",Processes[i].name,Processes[i].arrtime,Processes[i].reqtime,Processes[i].prio);
510     }
511    printf("\n");
512 }
513 
514 void readfiles()
515 {
516     char szTest[1000] = {0};int i=0,n;
517     FILE *fp=fopen("D:\\File1.txt","r");
518     //FILE *fout=fopen("D:\\File2.txt","w");
519     if(fp == NULL)
520     {
521         printf("ERROR!\n");
522         exit(0);
523     }
524     //fscanf(fp,"Processesum=%d\n",%n);
525     while(!feof(fp))  
526     {
527         fscanf(fp,"%d%d%d",&Processes[i].id,&Processes[i].arrtime,&Processes[i].reqtime);  //fscanf()函数将数据读入
528         printf("\n%3d%12d%15d\n",Processes[i].id,Processes[i].arrtime,Processes[i].reqtime);  //输出到屏幕
529         itoa(Processes[i].id,Processes[i].name,10);
530         i++;
531         /*memset(szTest, 0, sizeof(szTest));
532         fscanf(fp,"@id=%d|",&i);
533         fgets(fp,"name=%s|",Processes[i].name);
534         fscanf(fp,"arrtime=%d|reqtime=%d\n",&Processes[i].arrtime,&Processes[i].reqtime);
535         */
536     //fscanf(fp,"%d",)
537     }
538     sumProcess=i;
539     fclose(fp);
540 }
541 void shuru()
542 {
543     int i;
544     printf("作业个数:\n");
545     scanf("%d",&sumProcess);
546     for(i=1;i<=sumProcess;i++)
547     {
548         printf("\n第%d个作业:\n",i);
549         getchar();
550         printf("输入作业名:\n");
551         gets(Processes[i].name);
552         printf("到达时间:\n");
553         scanf("%d",&Processes[i].arrtime);
554         printf("要求服务时间:\n");
555         scanf("%d",&Processes[i].reqtime);
556     }
557     sumofthey();
558 }
559 void choosesort()
560 {
561     int n;
562     printf("\t\t\t*****************************\n");
563     printf("\t\t\t*******1.FCFS算法调度********\n");
564     printf("\t\t\t*******2.SJF算法调度*********\n");
565     printf("\t\t\t*******3.HRRF算法调度********\n");
566     printf("\t\t\t*******4.调用系统清屏********\n");
567     printf("\t\t\t*******5.退出算法调度********\n");
568     printf("\t\t\t*****************************\n");
569     scanf("%d",&n);
570     switch(n)
571     {
572     case 1:
573         printf("运行先来先服务算法FCFS\n");
574         sort(1);
575         printf("\n经按到达时间排序后,未达到队列是\n");
576         printz();
577         break;
578     case 2:
579         printf("运行最短作业优先算法FJS\n");
580         sort(2);
581         printf("\n经按到达时间排序后,未达到队列是\n");
582         printz();
583         break;
584     case 3:
585         printf("运行最高响应比优先算法HRRF\n");
586         sort(3);
587         printf("\n经按到达时间排序后,未达到队列是\n");
588         printz();
589         break;
590     case 4:
591         system("cls");
592         break;
593     case 5:
594         exit(0);
595         break;
596     }
597 }
598 void main()
599 {
600     startit();
601 //    choosesort();
602     while(sumProcess!=0)
603     {
604         RunProcess();
605     }
606 
607 }

 

posted @ 2016-04-29 15:42  Wicker  阅读(426)  评论(0编辑  收藏  举报