码迷,mamicode.com
首页 > 编程语言 > 详细

操作系统中的几种调度算法(JAVA版)

时间:2017-01-23 17:05:12      阅读:634      评论:0      收藏:0      [点我收藏+]

标签:方法   png   cep   images   string   cat   div   eth   override   

  1 import java.text.DecimalFormat;
  2 import java.util.Arrays;
  3 import java.util.Scanner;
  4 
  5 /*
  6  * 作者:Chensx1020
  7  * 时间:2016-12-11
  8  * 功能:CPU调度算法
  9  *         1)先到先服务调度算法(FCFS)
 10  *         2)最短作业优先调度算法,非抢占式(SJF)
 11  *         3)优先级调度算法(PSA)
 12  *         4)轮转法调度算法(RR)
 13  *         5)最高响应比调度算法(HRR)
 14  *         6)最短作业优先调度算法,抢占式(SJF)
 15  */
 16 
 17 public class CPU {
 18     //排序方法
 19     public static String whichComp="FCFS";
 20     //时间片
 21     public static int timeSlice=4;
 22 
 23     //进程类...............................................................
 24     public static class MyProcess implements Comparable<MyProcess> {
 25         
 26         private String name;    //进程名字
 27         private int arrive;    //到达时间
 28         private int work;    //区间时间(服务时间)
 29         private int rWork;    //还需时间
 30         private int priority;    //优先级
 31         private int begin;    //开始时间
 32         private double hrr;    //响应比 
 33         private boolean flag;    //标记该进程是否执行
 34         
 35         public MyProcess(String name){
 36             this.name = name;
 37             this.flag = false;
 38         }
 39         
 40         public void setArrive(int arrive){
 41             this.arrive = arrive;
 42         }
 43         public void setWork(int work){
 44             this.work = work;
 45         }
 46         public void setRwork(int rWork){
 47             this.rWork = rWork;
 48         }
 49         public void setPriority(int priority){
 50             this.priority = priority;
 51         }
 52         public void setBegin(int begin){
 53             this.begin = begin;
 54         }
 55         public void setHrr(){
 56             this.hrr = (this.begin-this.arrive)*1.0/this.work + 1;
 57         }
 58         public void setHrr(double hrr){
 59             this.hrr = hrr;
 60         }
 61         public void setFlag(){
 62             this.flag = true;
 63         }
 64         
 65         public String getName(){
 66             return this.name;
 67         }
 68         public int getArrive(){
 69             return this.arrive;
 70         }
 71         public int getWork(){
 72             return this.work;
 73         }
 74         public int getRwork(){
 75             return this.rWork;
 76         }
 77         public int getPriority(){
 78             return this.priority;
 79         }
 80         public int getBegin(){
 81             return this.begin;
 82         }
 83         public double getHrr(){
 84             return this.hrr;
 85         }
 86         public boolean getFlag(){
 87             return this.flag;
 88         }
 89 
 90         @Override
 91         public int compareTo(MyProcess o) {
 92             if(whichComp.equals("FCFS")){
 93                 if(this.arrive>o.arrive)
 94                     return 1;
 95                 else if(this.arrive<o.arrive)
 96                     return -1;
 97                 else
 98                     return this.work-o.work;
 99             }
100             else if(whichComp.equals("SJF")){
101                 if(this.work>o.work)
102                     return 1;
103                 else if(this.work<o.work)
104                     return -1;
105                 else
106                     return this.arrive-o.arrive;
107             }
108             else if(whichComp.equals("SJFS")){
109                 if(this.rWork>o.rWork)
110                     return 1;
111                 else if(this.rWork<o.rWork)
112                     return -1;
113                 else
114                     return this.arrive-o.arrive;
115 
116             }
117             else if(whichComp.equals("PSA")){
118                 if(this.priority>o.priority)
119                     return 1;
120                 else if(this.priority<o.priority)
121                     return -1;
122                 else
123                     return this.arrive-o.arrive;
124             }
125             else if(whichComp.equals("HRR")){
126                 if(this.begin>o.begin)
127                     return 1;
128                 else if(this.begin<o.begin)
129                     return -1;
130                 else{
131                     if(this.hrr < o.hrr)
132                         return 1;
133                     else if(this.hrr > o.hrr)
134                         return -1;
135                     else{
136                         return this.arrive-o.arrive;
137 //                        if(this.arrive>o.arrive)
138 //                            return 1;
139 //                        else if(this.arrive<o.arrive)
140 //                            return -1;
141 //                        else
142 //                            return this.work-o.work;
143                     }
144                 }
145             }
146             else{
147                 return 0;
148             }
149         }
150     }
151     
152     
153     //FCFS类...............................................................
154     public static class FCFS{
155         
156         private int count;
157         private String temp;
158         
159         public FCFS(MyProcess test[]){
160             Arrays.sort(test);
161             
162             count = test[0].getArrive();
163             temp = "";
164             
165             int length = test.length;
166             
167             int wait = 0;    //总等待时间
168             int cycle = 0;    //总周转时间
169             
170             System.out.print("------------------------------------------------------------------");
171             System.out.println("------------------------------------------------------------------");
172             System.out.println("先到先服务调度算法的结果为:");
173             System.out.println("进程名字\t到达时间\t服务时间\t开始时间\t结束时间\t等待时间\t"
174                     + "周转时间\t带权周转时间\t");
175             
176             for(int i=0; i<length-1; ++i){
177                 
178                 System.out.println(test[i].getName()+"\t"
179                         +test[i].getArrive()+"\t"
180                         +test[i].getWork()+"\t"
181                         +count+"\t"
182                         +(count+test[i].getWork())+"\t"
183                         +(count-test[i].getArrive())+"\t"
184                         +(count-test[i].getArrive()+test[i].getWork())+"\t"
185                         +(count-test[i].getArrive()+test[i].getWork())*1.0/test[i].getWork()+"\t"
186                         );
187                 
188                 wait += count-test[i].getArrive();
189                 cycle += count-test[i].getArrive()+test[i].getWork();
190                 
191                 count += test[i].getWork();
192                 
193                 if(count < test[i+1].getArrive())
194                     count = test[i+1].getArrive();
195                 temp += test[i].getName()+"   ";
196                 
197             }
198             
199             System.out.println(test[length-1].getName()+"\t"
200                     +test[length-1].getArrive()+"\t"
201                     +test[length-1].getWork()+"\t"
202                     +count+"\t"
203                     +(count+test[length-1].getWork())+"\t"
204                     +(count-test[length-1].getArrive())+"\t"
205                     +(count-test[length-1].getArrive()+test[length-1].getWork())+"\t"
206                     +(count-test[length-1].getArrive()+test[length-1].getWork())*1.0/test[length-1].getWork()+"\t"
207                     );
208             
209             wait += count-test[length-1].getArrive();
210             cycle += count-test[length-1].getArrive()+test[length-1].getWork();
211             
212             temp += test[test.length-1].getName()+"   ";
213             
214             System.out.println("\n所以最后先到先服务调度算法执行的顺序为:" + temp);
215             System.out.println("平均等待时间为:" + wait*1.0/length + ",平均周转时间为:" + cycle*1.0/length);
216         }
217         
218     }
219     
220     
221     //SJF类非抢占式...............................................................
222     public static class SJF{
223         
224         private int count;
225         private String temp;
226         
227         public SJF(MyProcess test[]){
228             Arrays.sort(test);
229             
230             count = test[0].getArrive();
231             temp = "";
232             
233             int length = test.length;
234             
235             int wait = 0;    //总等待时间
236             int cycle = 0;    //总周转时间
237             
238             System.out.print("------------------------------------------------------------------");
239             System.out.println("------------------------------------------------------------------");
240             System.out.println("最短作业优先调度算法非抢占式的结果为:");
241             System.out.println("进程名字\t到达时间\t服务时间\t开始时间\t结束时间\t等待时间\t"
242                     + "周转时间\t带权周转时间\t");
243             
244             for(int i=0; i<length-1; ++i){
245                 System.out.println(test[i].getName()+"\t"
246                         +test[i].getArrive()+"\t"
247                         +test[i].getWork()+"\t"
248                         +count+"\t"
249                         +(count+test[i].getWork())+"\t"
250                         +(count-test[i].getArrive())+"\t"
251                         +(count-test[i].getArrive()+test[i].getWork())+"\t"
252                         +(count-test[i].getArrive()+test[i].getWork())*1.0/test[i].getWork()+"\t"
253                         );
254                 
255                 wait += count-test[i].getArrive();
256                 cycle += count-test[i].getArrive()+test[i].getWork();
257                 
258                 count += test[i].getWork();
259                 
260                 if(count < test[i+1].getArrive())
261                     count = test[i+1].getArrive();
262                 temp += test[i].getName()+"   ";
263                 
264             }
265             
266             System.out.println(test[length-1].getName()+"\t"
267                     +test[length-1].getArrive()+"\t"
268                     +test[length-1].getWork()+"\t"
269                     +count+"\t"
270                     +(count+test[length-1].getWork())+"\t"
271                     +(count-test[length-1].getArrive())+"\t"
272                     +(count-test[length-1].getArrive()+test[length-1].getWork())+"\t"
273                     +(count-test[length-1].getArrive()+test[length-1].getWork())*1.0/test[length-1].getWork()+"\t"
274                     );
275                                        
276             wait += count-test[length-1].getArrive();
277             cycle += count-test[length-1].getArrive()+test[length-1].getWork();
278                                
279             temp += test[test.length-1].getName()+"   ";
280             
281             System.out.println("\n所以最后最短作业优先调度算法非抢占式执行的顺序为:" + temp);
282             System.out.println("平均等待时间为:" + wait*1.0/length + ",平均周转时间为:" + cycle*1.0/length);
283         }
284         
285     }
286     
287     
288     //SJF类抢占式...............................................................
289     public static class SJFS{
290         
291         private int count;
292         private String temp;    //执行的顺序
293         private int flag = 0;    //计数,用于判断是否所有进程执行完毕
294         
295         public SJFS(MyProcess test[]){
296             
297             int wait = 0;    //总等待时间
298             int cycle = 0;    //总周转时间
299             
300             System.out.print("------------------------------------------------------------------");
301             System.out.println("------------------------------------------------------------------");
302             System.out.println("最短作业优先调度算法抢占式的结果为:");
303             System.out.println("进程名字\t到达时间\t区间时长\t开始时间\t结束时间\t剩余服务时间\t等待时间\t"
304                     + "周转时间\t带权周转时间\t");
305             
306             int length = test.length;    //长度
307             
308             whichComp="FCFS";
309             Arrays.sort(test);    //按照进程的到达时间排序
310             count = test[0].getArrive();    //count的值为程序最先到达的进程的到达时间
311             
312             whichComp="SJFS";
313             Arrays.sort(test);     //所有进程按照剩余服务时间的长短排序
314             
315             String lastName = "";    //上一次执行的进程的名字
316             for(int i=0; i<length; ++i){
317                 if(test[i].getArrive()<=count){
318                     lastName = test[i].getName();
319                     System.out.print(test[i].getName() + "\t"
320                             + test[i].getArrive() + "\t"
321                             + test[i].getWork() + "\t"
322                             + count + "\t");
323                     temp = test[i].getName()+"   ";
324                     break;
325                 }
326             }
327             
328             boolean done = false;
329             
330             while(true){
331                 done = false;
332                 ++count;
333                 
334                 for(int j=0; j<length; ++j){
335                     if(test[j].getName().equals(lastName)){
336                         test[j].setRwork(test[j].getRwork()-1);    //剩余时间-1
337                         if(test[j].getRwork() == 0){
338                             ++flag;
339                             test[j].setRwork(999999999);
340                             System.out.println(count + "\t"
341                                     + "0\t\t"
342                                     + (count-test[j].getWork()-test[j].getArrive()) + "\t"    //等待时间
343                                     + (count-test[j].getArrive()) + "\t"                        //周转时间
344                                     + ((count-test[j].getArrive())*1.0/test[j].getWork()) + "\t" //带权周转
345                                     );
346                             wait += count-test[j].getWork()-test[j].getArrive();
347                             cycle += count-test[j].getArrive();
348                             done = true;
349                         }    
350                         break;
351                     }
352                 }
353                 
354                 if(flag==length){
355                     break;
356                 }
357                 
358                 Arrays.sort(test);     //所有进程按照剩余服务时间的长短重新排序
359                 
360                 for(int i=0; i<length; ++i){
361                     if(test[i].getArrive()<=count){
362                         if(lastName.equals(test[i].getName())){
363 //                            System.out.println(test[i].getName());
364                         }
365                         else{
366                             for(int j=0; j<length; ++j){
367                                 if(test[j].getName().equals(lastName)){
368                                     if(!done){
369                                         System.out.println(count + "\t"
370                                                 + test[j].getRwork()+ "\t\t"
371                                                 + "未执行完\t" + "未执行完\t" + "未执行完\t");
372                                     }
373                                 }
374                             }
375                             
376                             lastName = test[i].getName();
377                             
378                             System.out.print(test[i].getName() + "\t"
379                                     + test[i].getArrive() + "\t"
380                                     + test[i].getWork() + "\t"
381                                     + count + "\t");
382                             temp += test[i].getName()+"   ";
383                         }
384                         break;
385                     }
386                 }
387 
388             }
389             
390             System.out.println("\n所以最后最短作业优先调度算法抢占式执行的顺序为:" + temp);
391             System.out.println("平均等待时间为:" + wait*1.0/length + ",平均周转时间为:" + cycle*1.0/length);
392 
393         }
394     
395     }
396     
397     
398     //PSA类...............................................................
399     public static class PSA{
400         
401         private int count;
402         private String temp;
403         
404         public PSA(MyProcess test[]){
405             Arrays.sort(test);
406             
407             count = test[0].getArrive();
408             temp = "";
409             
410             int length = test.length;
411             
412             int wait = 0;    //总等待时间
413             int cycle = 0;    //总周转时间
414             
415             System.out.print("------------------------------------------------------------------");
416             System.out.println("------------------------------------------------------------------");
417             System.out.println("优先级调度算法的结果为:");
418             System.out.println("进程名字\t到达时间\t服务时间\t优先级\t开始时间\t结束时间\t等待时间\t"
419                     + "周转时间\t带权周转时间\t");
420             
421             for(int i=0; i<length-1; ++i){
422                 System.out.println(test[i].getName()+"\t"
423                         +test[i].getArrive()+"\t"
424                         +test[i].getWork()+"\t"
425                         +test[i].getPriority()+"\t"
426                         +count+"\t"
427                         +(count+test[i].getWork())+"\t"
428                         +(count-test[i].getArrive())+"\t"
429                         +(count-test[i].getArrive()+test[i].getWork())+"\t"
430                         +(count-test[i].getArrive()+test[i].getWork())*1.0/test[i].getWork()+"\t"
431                         );
432                 
433                 wait += count-test[i].getArrive();
434                 cycle += count-test[i].getArrive()+test[i].getWork();
435                 
436                 count += test[i].getWork();
437                 
438                 if(count < test[i+1].getArrive())
439                     count = test[i+1].getArrive();
440                 temp += test[i].getName()+"   ";
441                 
442             }
443             
444             System.out.println(test[length-1].getName()+"\t"
445                     +test[length-1].getArrive()+"\t"
446                     +test[length-1].getWork()+"\t"
447                     +test[length-1].getPriority()+"\t"
448                     +count+"\t"
449                     +(count+test[length-1].getWork())+"\t"
450                     +(count-test[length-1].getArrive())+"\t"
451                     +(count-test[length-1].getArrive()+test[length-1].getWork())+"\t"
452                     +(count-test[length-1].getArrive()+test[length-1].getWork())*1.0/test[length-1].getWork()+"\t"
453                     );
454             
455             wait += count-test[length-1].getArrive();
456             cycle += count-test[length-1].getArrive()+test[length-1].getWork();
457             
458             temp += test[test.length-1].getName()+"   ";
459             
460             System.out.println("\n所以最后优先级调度算法执行的顺序为:" + temp);
461             System.out.println("平均等待时间为:" + wait*1.0/length + ",平均周转时间为:" + cycle*1.0/length);
462         }
463         
464     }
465     
466 
467     //HRR类...............................................................
468     public static class HRR{
469         
470         private int count;
471         private String temp;
472         
473         public HRR(MyProcess test[], MyProcess tests[]){
474             
475             DecimalFormat df = new DecimalFormat("#.###");     //小数保留三位小数  
476             
477             Arrays.sort(tests);
478 
479             count = tests[0].getBegin();
480             temp = "";
481             
482             int length = test.length;
483             
484             int wait = 0;    //总等待时间
485             int cycle = 0;    //总周转时间
486             
487             System.out.print("------------------------------------------------------------------");
488             System.out.println("------------------------------------------------------------------");
489             System.out.println("最高响应比调度算法的结果为:");
490             
491             for(int tur = 1; tur<=length; ++tur){
492                 System.out.println("\n正在执行第"+tur+"个进程:"+tests[0].getName());    
493                 System.out.println("进程名字\t到达时间\t服务时间\t响应比\t开始时间\t结束时间\t等待时间\t"
494                         + "周转时间\t带权周转时间\t");    
495                 
496                 for(int i=0; i<length; ++i) {
497                     
498                     if(test[i].getFlag()){    //已执行
499                         System.out.println(test[i].getName()+"\t"
500                                 +test[i].getArrive()+"\t"
501                                 +test[i].getWork()+"\t"
502                                 +"已执行完\t"
503                                 +test[i].getBegin()+"\t"
504                                 +(test[i].getBegin()+test[i].getWork())+"\t"
505                                 +(test[i].getBegin()-test[i].getArrive())+"\t"
506                                 +(test[i].getBegin()-test[i].getArrive()+test[i].getWork())+"\t"
507                                 +df.format((test[i].getBegin()-test[i].getArrive()+test[i].getWork())*1.0/test[i].getWork())+"\t"
508                                 );
509                         continue;
510                     }
511                     
512                     else if(test[i].getArrive()>count){    //未到达
513                         System.out.println(test[i].getName()+"\t"
514                                 +test[i].getArrive()+"\t"
515                                 +test[i].getWork()+"\t"
516                                 +"未到达\t"
517                                 +"未到达\t"
518                                 +"未到达\t"
519                                 +"未到达\t"
520                                 +"未到达\t"
521                                 +"未到达\t"
522                                 );
523                         continue;
524                     }
525                     
526                     else{
527                         //执行该进程
528                         if(test[i].getName().equals(tests[0].getName())){
529                             System.out.println(test[i].getName()+"\t"
530                                     +test[i].getArrive()+"\t"
531                                     +test[i].getWork()+"\t"
532                                     +df.format(test[i].getHrr())+"\t"
533                                     +test[i].getBegin()+"\t"
534                                     +(test[i].getBegin()+test[i].getWork())+"\t"
535                                     +(test[i].getBegin()-test[i].getArrive())+"\t"
536                                     +(test[i].getBegin()-test[i].getArrive()+test[i].getWork())+"\t"
537                                     +df.format((test[i].getBegin()-test[i].getArrive()+test[i].getWork())*1.0/test[i].getWork())+"\t"
538                                     );
539                             
540                             wait += test[i].getBegin()-test[i].getArrive();
541                             cycle += test[i].getBegin()-test[i].getArrive()+test[i].getWork();
542                             test[i].setFlag();
543                             tests[0].setFlag();
544                         }
545                         
546                         else{
547                             System.out.println(test[i].getName()+"\t"
548                                     +test[i].getArrive()+"\t"
549                                     +test[i].getWork()+"\t"
550                                     +df.format(test[i].getHrr())+"\t"
551                                     +"未开始\t"
552                                     +"未开始\t"
553                                     +"未开始\t"
554                                     +"未开始\t"
555                                     +"未开始\t"
556                                     );
557                         }
558                         
559                     }
560                 }
561                 
562                 int count1=count+tests[0].getWork();
563 
564                 for(int j=tests.length-1; j>=1; --j){
565                     if(tests[j].getFlag()){
566                         ;
567                     }
568                     else{
569                         if(count1 >= tests[j].getArrive()){    //进程已到
570                             tests[j].setBegin(count1);
571                             int x;
572                             for(x=0; x<length; ++x){
573                                 if(tests[j].getName().equals(test[x].getName()))
574                                     break;
575                             }
576                             test[x].setBegin(count1);
577                             tests[j].setHrr();
578                             test[x].setHrr();
579                             count = count1;
580                         }
581                         else{
582                             count = tests[j].getBegin();
583                         }
584                     }
585                 }
586                 
587                 temp += tests[0].getName()+"   ";
588                 
589                 //改变执行过的进程数据,使再次排序时排在未执行数据的后面
590                 tests[0].setArrive(999999999);
591                 tests[0].setBegin(999999999);
592                 tests[0].setHrr(-1.0);
593 
594                 Arrays.sort(tests);    //重新排序
595                 
596             }
597 
598             System.out.println("\n所以最后最高响应比调度算法执行的顺序为:" + temp);
599             System.out.println("平均等待时间为:" + wait*1.0/length + ",平均周转时间为:" + cycle*1.0/length);
600         }
601         
602     }
603     
604     
605     //RR类...............................................................
606     public static class RR{
607         
608         private int count;
609         private String temp;
610         
611         public RR(MyProcess test[]){
612             
613             count = 0;
614             temp = "";
615             
616             int length = test.length;
617             
618             int wait = 0;    //总等待时间
619             int cycle = 0;    //总周转时间
620             
621             int tur = 1;    //第几趟
622             
623             System.out.print("------------------------------------------------------------------");
624             System.out.println("------------------------------------------------------------------");
625             System.out.println("轮转法调度算法的结果为:");
626             System.out.println("第"+tur+"趟结果\n进程名字\t到达时间\t服务时间\t开始时间\t结束时间\t剩余所需服务时间\t"
627                     + "等待时间\t周转时间\t带权周转时间\t");
628             
629             int tempCount=0;    //记录是否还有进程未执行完
630             
631             int i=0;
632             
633             while(true){
634                 
635                 if(test[i].getRwork()>timeSlice){
636                     
637                     test[i].setRwork(test[i].getRwork()-timeSlice);
638                     
639                     System.out.println(test[i].getName()+"\t"
640                             +test[i].getArrive()+"\t"
641                             +test[i].getWork()+"\t"
642                             +count+"\t"
643                             +(count+timeSlice)+"\t"
644                             +test[i].getRwork()+"\t\t"
645                             +"未执行完\t"
646                             +"未执行完\t"
647                             +"未执行完\t"
648                             );
649                     
650                     count += timeSlice;
651                     temp += test[i].getName()+"  ";
652                     
653                     ++i;
654                     if(i==length){
655                         i=0;
656                         ++tur;
657                         System.out.println("第"+tur+"趟结果\n进程名字\t到达时间\t服务时间\t开始时间\t结束时间\t"
658                                 + "剩余所需服务时间\t等待时间\t周转时间\t带权周转时间\t");
659                     }
660                 }
661                 
662                 else if(test[i].getRwork()>0){
663                     
664                     System.out.println(test[i].getName()+"\t"
665                             +test[i].getArrive()+"\t"
666                             +test[i].getWork()+"\t"
667                             +count+"\t"
668                             +(count+test[i].getRwork())+"\t"
669                             +"0\t\t"
670                             +(count+test[i].getRwork()-test[i].getWork()-test[i].getArrive())+"\t"
671                             +(count+test[i].getRwork()-test[i].getArrive())+"\t"
672                             +(count+test[i].getRwork()-test[i].getArrive())*1.0/test[i].getWork()+"\t"
673                             );
674                     
675                     wait += count+test[i].getRwork()-test[i].getWork()-test[i].getArrive();
676                     cycle += count+test[i].getRwork()-test[i].getArrive();
677                     
678                     count += test[i].getRwork();
679                     temp += test[i].getName()+"  ";
680                         
681                     test[i].setRwork(0);
682                         
683                     ++i;
684                     if(i==length){
685                         i=0;
686                         ++tur;
687                         System.out.println("第"+tur+"趟结果\n进程名字\t到达时间\t服务时间\t开始时间\t结束时间\t"
688                                 + "剩余所需服务时间\t等待时间\t周转时间\t带权周转时间\t");
689                     }
690                         
691                     ++tempCount;
692                     if(tempCount == length)
693                         break;
694                 }
695                 else{
696                     ++i;
697                     if(i==length){
698                         i=0;
699                         ++tur;
700                         System.out.println("第"+tur+"趟结果\n进程名字\t到达时间\t服务时间\t开始时间\t结束时间\t"
701                                 + "剩余所需服务时间\t等待时间\t周转时间\t带权周转时间\t");
702                     }
703                     continue;
704                 }
705             }
706             
707             System.out.println("\n所以最后轮转法调度算法执行的顺序为:" + temp);
708             System.out.println("平均等待时间为:" + wait*1.0/length + ",平均周转时间为:" + cycle*1.0/length);
709         }
710         
711     }    
712     
713     
714     //主函数................................................................
715     public static void main(String[] args) {
716         Scanner input = new Scanner(System.in);
717         
718         while(true){
719             int n;    //进程数
720             while(true){
721                 try{
722                     System.out.print("输入进程个数:");
723                     n = input.nextInt();
724                     break;
725                 } catch(Exception e){
726                     System.out.println("输入不合法,请重新输入!");
727                     input.nextLine();
728                 }
729             }
730             MyProcess test[] = new MyProcess[n];    //进程数组
731             MyProcess tests[] = new MyProcess[n];    //临时进程数组(HRR用)
732             
733             System.out.println("您想执行何种操作:");
734             System.out.println("1.FCFS(先到先服务调度算法)");
735             System.out.println("2.SJF(最短作业优先调度算法,非抢占式)");
736             System.out.println("3.PSA(优先级调度算法)");
737             System.out.println("4.RR(轮转法调度算法)");
738             System.out.println("5.HRR(最高响应比调度算法)");
739             System.out.println("6.SJF(最短作业优先调度算法,抢占式)");
740             System.out.println("7.退出");
741             int which;
742             
743             try{
744                 which = input.nextInt();
745             } catch(Exception e){
746                 System.out.println("输入不合法,请重新输入!");
747                 input.nextLine();
748                 continue;
749             }
750             
751             //FCFS
752             if(which == 1){
753                 for(int i=0; i<n; ++i){
754                     test[i] = new MyProcess("P"+(i+1));
755                     System.out.print("请输入P" + (i+1) + "的到达时间和服务时间:");
756                     test[i].setArrive(input.nextInt());
757                     test[i].setWork(input.nextInt());
758                 }
759                 whichComp = "FCFS";
760                 new FCFS(test);
761             }
762             //SJF,非抢占式
763             else if(which == 2){
764                 for(int i=0; i<n; ++i){
765                     test[i] = new MyProcess("P"+(i+1));
766                     System.out.print("请输入P" + (i+1) + "的到达时间和服务时间:");
767                     test[i].setArrive(input.nextInt());
768                     test[i].setWork(input.nextInt());
769                 }
770                 whichComp = "SJF";
771                 new SJF(test);
772             }
773             //PSA
774             else if(which == 3){
775                 for(int i=0; i<n; ++i){
776                     test[i] = new MyProcess("P"+(i+1));
777                     System.out.print("请输入P" + (i+1) + "的到达时间和服务时间和优先级:");
778                     test[i].setArrive(input.nextInt());
779                     test[i].setWork(input.nextInt());
780                     test[i].setPriority(input.nextInt());
781                 }
782                 whichComp = "PSA";
783                 new PSA(test);
784             }
785             //RR
786             else if(which == 4){
787                 System.out.print("请输入时间片的大小:");
788                 timeSlice = input.nextInt();
789                 
790                 for(int i=0; i<n; ++i){
791                     test[i] = new MyProcess("P"+(i+1));
792                     System.out.print("请输入P" + (i+1) + "的服务时间(剩余时间):");
793                     test[i].setWork(input.nextInt());
794                     test[i].setRwork(test[i].getWork());
795                 }
796                 whichComp = "RR";
797                 new RR(test);
798             }
799             //HRR
800             else if(which == 5){
801                 for(int i=0; i<n; ++i){
802                     test[i] = new MyProcess("P"+(i+1));
803                     tests[i] = new MyProcess("P"+(i+1));
804                     System.out.print("请输入P" + (i+1) + "的到达时间和服务时间:");
805                     int a = input.nextInt();
806                     test[i].setArrive(a);
807                     tests[i].setArrive(a);
808                     int b = input.nextInt();
809                     test[i].setWork(b);
810                     tests[i].setWork(b);
811                     //设置开始时间和响应比
812                     test[i].setBegin(a);
813                     test[i].setHrr();
814                     tests[i].setBegin(a);
815                     tests[i].setHrr();
816                 }
817                 whichComp = "HRR";
818                 new HRR(test,tests);
819             }
820             //SJF,抢占式
821             else if(which == 6){
822                 for(int i=0; i<n; ++i){
823                     test[i] = new MyProcess("P"+(i+1));
824                     System.out.print("请输入P" + (i+1) + "的到达时间和服务时间:");
825                     test[i].setArrive(input.nextInt());
826                     test[i].setWork(input.nextInt());
827                     test[i].setRwork(test[i].getWork());    //设置剩余时间
828                 }
829                 whichComp = "SJFS";
830                 new SJFS(test);
831             }
832             else if(which == 7){
833                 System.out.println("程序结束!");
834                 break;
835             }
836             else {
837                 System.out.println("输入不合法,请重新输入!");
838             }
839             
840             System.out.print("------------------------------------------------------------------");
841             System.out.println("------------------------------------------------------------------");
842             System.out.print("------------------------------------------------------------------");
843             System.out.println("------------------------------------------------------------------\n\n");
844         }
845         
846         input.close();
847 
848     }
849 
850 }

部分运行结果:

技术分享

相同数据的最高响应比结果为

技术分享

 

操作系统中的几种调度算法(JAVA版)

标签:方法   png   cep   images   string   cat   div   eth   override   

原文地址:http://www.cnblogs.com/Chensx1020/p/6344298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!