1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| #include <stdio.h> typedef struct process_FCFS{ int num; int arrival; int brust; int waits; int start; }FCFS; //输入 void input(FCFS *pro,int pro_num) { int i; for(i = 0; i < pro_num; i ++) { printf("%d:到达时间,cpu处理时间:",i); scanf("%d %d",&pro[i].arrival,&pro[i].brust); pro[i].waits = 0; pro[i].num = i; } } //输出 void output(FCFS *pro, int pro_num) { int i; for (i = 0; i < pro_num; i++) { printf("进程%d:等待时间%d,开始时间%d,处理时间%d\n",pro[i].num,pro[i].waits,pro[i].start,pro[i].brust); } } //排序 void sort(FCFS *pro, int n) { int i, j; FCFS temp; for (j = 0; j < n - 1; j++) for (i = 0; i < n - 1 - j; i++) { if(pro[i].arrival > pro[i + 1].arrival) { temp = pro[i]; pro[i] = pro[i + 1]; pro[i + 1] = temp; } } } //fcfs void runfcfs() { FCFS pro[100]; int time_wait = 0, pro_num, i,time = 0; float sum_wait = 0; printf("请输入进程数量:"); scanf("%d",&pro_num); input(pro,pro_num); sort(pro, pro_num); for (i=0; i < pro_num; i ++) { if (i == 0) { time_wait = pro[i].arrival; pro[i].waits = time_wait; pro[i].start = time_wait; time = pro[i].arrival + pro[i].brust; sum_wait = time_wait; } else{ if (time >= pro[i].arrival) { time_wait = time - pro[i].arrival; pro[i].waits = time_wait; pro[i].start = time; time += pro[i].brust; } else{ time = pro[i].arrival; pro[i].start = time; pro[i].waits = 0; time += pro[i].brust; } sum_wait += pro[i].waits; } } output(pro, pro_num); printf("处理总时间:%d\n",time); printf("平均等待时间:%f\n",sum_wait/pro_num); } int main() { runfcfs(); return 0; }
|