程序排程
阿新 • • 發佈:2020-12-29
#include <stdio.h> #include <stdlib.h> struct PCB { int pid;//程序識別符號 int rr;//已執行時間 int time;//程序要求執行時間 char sta;//程序的狀態 struct PCB *next;//連結指標 }; struct PCB pcb1,pcb2,pcb3,pcb4,pcb5,*tail,*head,*rp; void init()//初始化各個程序的執行時間 { int time; pcb1.pid = 1; pcb2.pid = 2; pcb3.pid = 3; pcb4.pid = 4; pcb5.pid = 5; pcb1.rr =pcb2.rr =pcb3.rr =pcb4.rr =pcb5.rr = 0; pcb1.sta = pcb2.sta = pcb3.sta = pcb4.sta = pcb5.sta = 'w'; printf("請輸入程序p1需要執行的時間:"); scanf("%d",&time); pcb1.time =time; printf("請輸入程序p2需要執行的時間:"); scanf("%d",&time); pcb2.time=time; printf("請輸入程序p3需要執行的時間:"); scanf("%d",&time); pcb3.time=time; printf("請輸入程序p4需要執行的時間:"); scanf("%d",&time); pcb4.time =time; printf("請輸入程序p5需要執行的時間:"); scanf("%d",&time); pcb5.time=time; pcb1.next=&pcb2; pcb2.next=&pcb3; pcb3.next=&pcb4; pcb4.next=&pcb5; pcb5.next=&pcb1; head=&pcb1; tail=&pcb5; } void printf1()//顯示錶頭 { printf("+---------------|---------------|---------------|---------------+\n"); printf("|\tpid\t|\trr\t|\ttime\t|\tSTA\t|\n"); printf("|---------------|---------------|---------------|---------------|\n"); } void printf2()//顯示各個程序的初始狀態 { printf("processes p%d running\n",head->pid); printf1(); printf("|\t%d\t|\t%d\t|\t%d\t|\t%c\t|\n",head->pid,head->rr,head->time,head->sta); printf("|---------------|---------------|---------------|---------------|\n"); rp=head; while(rp!=tail) { rp=rp->next; printf("|\t%d\t|\t%d\t|\t%d\t|\t%c\t|\n",rp->pid,rp->rr,rp->time,rp->sta); printf("|---------------|---------------|---------------|---------------|\n"); } } void operation()//執行 { int flag=1; while(flag<=5) { head->rr++; if((head->rr==head->time)||(head->time==0)) { tail->sta='w';//將程序狀態設定為等待態 head->sta='f';//將程序狀態設定為執行態 printf2(); head=head->next; tail->next=head; flag++; } else { tail->sta='w';//將程序狀態設定為等待態 head->sta='r';//將程序狀態設定為就緒態 printf2(); tail=head; head=head->next; } } } void main() { init(); printf2(); operation(); }