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 97 98 99 100 101 102 103 104 105 106
| void RR(int n, struct ReadyQueue rq, ReadyQueue* rq1, struct ReadyQueue* rq2) { int i = 0,j=1; PCB* p = rq.head;
while (rq1->head != NULL || p) { if (!rq1->head && p->arrivetime - i < n&& p->arrivetime-i>0) i = p->arrivetime; if (!rq1->head && p->arrivetime >= i + n) { i += n; continue; j++; } Processin(&p, rq1, i); PCB* temp; rq1->head->state = 'R'; int t = rq1->head->needtime; printf("第%d个时间片\n", j); Print(*rq1); printf("******************************\n"); if (rq1->head->needtime > n) { rq1->head->needtime -= n; rq1->head->state = 'W'; rq1->head->servicetime += n; } else { rq1->head->finishtime = i + t; rq1->head->servicetime += t; rq1->head->needtime = 0; rq1->head->state = 'F'; } Dequeue(rq1, &temp); if (temp->needtime) { i += n; Processin(&p, rq1, i); EnQueue(rq1, temp); } else { i += t; EnQueue(rq2, temp); } j++; } } void Preemptive(struct ReadyQueue rq, ReadyQueue* rq1, struct ReadyQueue* rq2){ int i = 0; PCB* p = rq.head; while (rq1->head != NULL||p) { if (!rq1->head) i = p->arrivetime; if(p)Processin(&p, rq1, i); PCB* p1 = Getprior(*rq1); p1->state = 'R'; printf("第%d个时刻\n", i); Print(*rq1); printf("******************************\n"); if (p1->needtime>1) { p1->state = 'W'; p1->priority += 1;} else { p1->finishtime =i+1; p1->state = 'F'; } p1->needtime -= 1; p1->servicetime += 1; if (!p1->needtime) { Delete(rq1, p1); EnQueue(rq2, p1); } i++; } } void NonPreemptive(struct ReadyQueue rq, ReadyQueue* rq1, struct ReadyQueue* rq2) { int i = 0; PCB* p = rq.head; while (rq1->head != NULL || p) { if (!rq1->head) i = p->arrivetime; if (p)Processin(&p, rq1, i); PCB* p1 = Getprior(*rq1); while (p1->needtime) { p1->state = 'R'; printf("第%d个时刻\n", i); Print(*rq1); printf("******************************\n"); i++; p1->servicetime += 1; p1->state = 'W'; if (p)Processin(&p, rq1, i); p1->needtime -= 1; } p1->finishtime = i; p1->state = 'F'; Delete(rq1, p1); EnQueue(rq2, p1); } } void Show(struct ReadyQueue rq) { PCB* p = rq.head; int i = 0; float t = 0, w = 0,m; while (p) { m = (p->finishtime - p->arrivetime)*1.0 / (p->servicetime); t += p->finishtime - p->arrivetime; w += m; printf("进程名:%c\t完成时间:%d\t周转时间:%d\t带权周转时间:%f\t\n", p->name, p->finishtime, p->finishtime - p->arrivetime,m); p = p->next; i++; } printf("%d个进程的平均周转时间为%f,平均带权周转时间为%f", i, t/i , w/ i); }
int main() { ReadyQueue rq, rq2; ReadyQueue rq1; initQueue(&rq1); initQueue(&rq); initQueue(&rq2); int n1, n2; scanf("%d", &n1); Creat(n1, &rq); NonPreemptive(rq, &rq1, &rq2); Show(rq2); }
|