FCFS,SJF(java实现)
1 import java.io.BufferedReader; 2 3 import java.io.InputStreamReader; 4 5 public class FCFSANDSJF { 6 7 private static int i; 8 9 private int j; 10 11 private int temp; 12 13 private int m[]; 14 15 private int n[]; 16 17 private int process_number;//进程数 18 19 private int arrival_time[];//到达时间 20 21 private int services_time[];//服务时间 22 23 private int start_time[];//开始时间 24 25 private int completion_time[];//完成时间 26 27 private int turn_around_time[];//周转时间 28 29 private double add1; 30 31 private double add2; 32 33 private double add3; 34 35 private double add4; 36 37 private double right_turn_around_time[]; 38 39 private void FCFS(){ 40 41 System.out.println("\n===FCFS先来先服务算法==="); 42 43 System.out.println("\n到达时间是:"); 44 45 for(i=0;i<process_number;i++){ 46 47 System.out.print(arrival_time[i]+"\t"); 48 49 } 50 51 System.out.println("\n服务时间是:"); 52 53 for(i=0;i<process_number;i++){ 54 55 System.out.print(services_time[i]+"\t"); 56 57 } 58 59 System.out.println("\n开始时间是:"); 60 61 for(i=0;i<process_number;i++){ 62 63 n[i]=arrival_time[i]; 64 65 m[i]=i; 66 67 } 68 69 for(i=0;i<process_number-1;i++){ 70 71 for(j=i+1;j<process_number;j++){ 72 73 if(n[i]>n[j]){ 74 75 temp=n[i]; 76 77 n[i]=n[j]; 78 79 n[j]=temp; 80 81 temp=m[i]; 82 83 m[i]=m[j]; 84 85 m[j]=temp; 86 87 } 88 89 } 90 91 } 92 93 start_time[m[0]]=arrival_time[m[0]]; 94 95 for(i=1;i<process_number;i++){ 96 97 if(arrival_time[m[i]]<start_time[m[i-1]]+services_time[m[i-1]]) 98 99 start_time[m[i]]=start_time[m[i-1]]+services_time[m[i-1]]; 100 101 else 102 103 start_time[m[i]]=arrival_time[m[i]]; 104 105 } 106 107 for(i=0;i<process_number;i++){ 108 109 System.out.print(start_time[i]+"\t"); 110 111 } 112 113 System.out.println("\n完成时间是:"); 114 115 for(i=0;i<process_number;i++){ 116 117 completion_time[i]=start_time[i]+services_time[i]; 118 119 } 120 121 for(i=0;i<process_number;i++){ 122 123 System.out.print(completion_time[i]+"\t"); 124 125 } 126 127 System.out.println("\n周转时间是:"); 128 129 for(i=0;i<process_number;i++){ 130 131 turn_around_time[i]=completion_time[i]-arrival_time[i]; 132 133 } 134 135 for(i=0;i<process_number;i++){ 136 137 System.out.print(turn_around_time[i]+"\t"); 138 139 } 140 141 add1=0; 142 143 for(i=0;i<process_number;i++){ 144 145 add1=add1+turn_around_time[i]; 146 147 } 148 149 System.out.println("\n平均周转时间是:"+add1/process_number); 150 151 System.out.println("\n带权周转时间是:"); 152 153 for(i=0;i<process_number;i++){ 154 155 right_turn_around_time[i]=turn_around_time[i]*1.0/services_time[i]; 156 157 } 158 159 for(i=0;i<process_number;i++){ 160 161 System.out.print(right_turn_around_time[i]+"\t"); 162 163 } 164 165 System.out.println("\n平均带权周转时间是:"); 166 167 add2=0; 168 169 for(i=0;i<process_number;i++){ 170 171 add2=add2+right_turn_around_time[i]; 172 173 } 174 175 System.out.println(add2/process_number); 176 177 178 179 } 180 181 private void SJF(){ 182 183 System.out.println("\n===SJF:短作业优先算法==="); 184 185 System.out.println("\n到达时间是:"); 186 187 for(i=0;i<process_number;i++){ 188 189 System.out.print(arrival_time[i]+"\t"); 190 191 } 192 193 System.out.println("\n服务时间是:"); 194 195 for(i=0;i<process_number;i++){ 196 197 System.out.print(services_time[i]+"\t"); 198 199 } 200 201 System.out.println("\n开始时间是:"); 202 203 m[0]=0; 204 205 for(i=0;i<process_number-1;i++){ 206 207 if(arrival_time[i]>arrival_time[i+1]) 208 209 m[0]=i+1; 210 211 } 212 213 for(i=0;i<process_number;i++){ 214 215 n[i]=services_time[i]; 216 217 m[i+1]=i; 218 219 } 220 221 for(i=0;i<process_number-1;i++){ 222 223 for(j=i+1;j<process_number;j++){ 224 225 if(n[i]>n[j]){ 226 227 temp=n[i]; 228 229 n[i]=n[j]; 230 231 n[j]=temp; 232 233 temp=m[i+1]; 234 235 m[i+1]=m[j+1]; 236 237 m[j+1]=temp; 238 239 } 240 241 } 242 243 } 244 245 for(i=1;i<process_number+1;i++){ 246 247 if(m[0]==m[i]){ 248 249 for(j=i;j<process_number;j++){ 250 251 m[j]=m[j+1]; 252 253 } 254 255 } 256 257 } 258 259 start_time[m[0]]=arrival_time[m[0]]; 260 261 for(i=1;i<process_number;i++){ 262 263 if(arrival_time[m[i]]<start_time[m[i-1]]+services_time[m[i-1]]) 264 265 start_time[m[i]]=start_time[m[i-1]]+services_time[m[i-1]]; 266 267 else 268 269 start_time[m[i]]=arrival_time[m[i]]; 270 271 } 272 273 for(i=0;i<process_number;i++){ 274 275 System.out.print(start_time[i]+"\t"); 276 277 } 278 279 System.out.println("\n完成时间是:"); 280 281 for(i=0;i<process_number;i++){ 282 283 completion_time[i]=start_time[i]+services_time[i]; 284 285 } 286 287 for(i=0;i<process_number;i++){ 288 289 System.out.print(completion_time[i]+"\t"); 290 291 } 292 293 System.out.println("\n周转时间是:"); 294 295 for(i=0;i<process_number;i++){ 296 297 turn_around_time[i]=completion_time[i]-arrival_time[i]; 298 299 } 300 301 for(i=0;i<process_number;i++){ 302 303 System.out.print(turn_around_time[i]+"\t"); 304 305 } 306 307 add3=0; 308 309 for(i=0;i<process_number;i++){ 310 311 add3=add3+turn_around_time[i]; 312 313 } 314 315 System.out.println("\n平均周转时间是:"+add3/process_number); 316 317 System.out.println("\n带权周转时间是:"); 318 319 for(i=0;i<process_number;i++){ 320 321 right_turn_around_time[i]=turn_around_time[i]*1.0/services_time[i]; 322 323 } 324 325 for(i=0;i<process_number;i++){ 326 327 System.out.print(right_turn_around_time[i]+"\t"); 328 329 } 330 331 System.out.println("\n平均带权周转时间是:"); 332 333 add4=0; 334 335 for(i=0;i<process_number;i++){ 336 337 add4=add4+right_turn_around_time[i]; 338 339 } 340 341 System.out.println(add4/process_number); 342 343 } 344 345 public static void main(String[] args) throws Exception{ 346 347 System.out.println("请输入进程数:"); 348 349 FCFSANDSJF wo=new FCFSANDSJF(); 350 351 BufferedReader buf=null; 352 353 buf=new BufferedReader(new InputStreamReader(System.in)); 354 355 String str1=null; 356 357 str1=buf.readLine(); 358 359 wo.process_number=Integer.parseInt(str1); 360 361 wo.arrival_time=new int[wo.process_number]; 362 363 wo.services_time=new int[wo.process_number]; 364 365 wo.start_time=new int[wo.process_number+1]; 366 367 wo.completion_time=new int[wo.process_number+1]; 368 369 wo.turn_around_time=new int[wo.process_number+1]; 370 371 wo.right_turn_around_time=new double[wo.process_number+1]; 372 373 wo.m=new int[wo.process_number+1]; 374 375 wo.n=new int[wo.process_number]; 376 377 String str=null; 378 379 String str2=null; 380 381 System.out.println("\n请输入各进程到达时间"); 382 383 for(i=0;i<wo.process_number;i++){ 384 385 str=buf.readLine(); 386 387 wo.arrival_time[i]=Integer.parseInt(str); 388 389 } 390 391 System.out.println("\n请输入各个进程服务时间"); 392 393 for(i=0;i<wo.process_number;i++){ 394 395 str2=buf.readLine(); 396 397 wo.services_time[i]=Integer.parseInt(str2); 398 399 } 400 401 System.out.println("该进程数为"+wo.process_number); 402 403 System.out.println("\n到达时间为"); 404 405 for(i=0;i<wo.process_number;i++){ 406 407 System.out.print(wo.arrival_time[i]+"\t"); 408 409 } 410 411 System.out.println("\n服务时间为:"); 412 413 for(i=0;i<wo.process_number;i++){ 414 415 System.out.print(wo.services_time[i]+"\t"); 416 417 } 418 419 System.out.println("\n======先来先服务【0】========"); 420 421 System.out.println("\n======短作业优先【1】========"); 422 423 System.out.println("\n======退出系统【2】========"); 424 425 String str3=null; 426 427 str3=buf.readLine(); 428 429 i=Integer.parseInt(str3); 430 431 while(i!=2){ 432 433 switch(i){ 434 435 case 0: 436 437 wo.FCFS(); 438 439 break; 440 441 case 1: 442 443 wo.SJF(); 444 445 break; 446 447 default: 448 449 System.out.println("\n输入有误,请重新输入"); 450 451 } 452 453 System.out.println("\n======先来先服务【0】========"); 454 455 System.out.println("\n======短作业优先【1】========"); 456 457 System.out.println("\n======退出系统【2】========"); 458 459 str3=buf.readLine(); 460 461 i=Integer.parseInt(str3); 462 463 } 464 465 System.out.println("系统已退出!"); 466 467 } 468 469 }
—————-转载非原创