#include #define N 4 // Process count typedef struct{ char pid[3]; //Process name int bt; //CPU Burst time int rt; //Remaining time int ct; //Completion time int tat; //Turnaround time int wt; //Waiting time } Process; int main(){ Process p[N] = { {"P1", 6, 6, 0, 0, 0}, {"P2", 3, 3, 0, 0, 0}, {"P3", 1, 1, 0, 0, 0}, {"P4", 7, 7, 0, 0, 0} }; int q; //Time quantum int cs; //Context switch time printf("규정 시간량 입력 : "); scanf("%d", &q); printf("문맥 교환 시간 입력 : "); scanf("%d", &cs); int time = 0; int done = 0; printf("\n간트 차트 : \n"); while(done < N){ for(int i = 0; i < N; i++){ if(p[i].rt > 0){ int exec = (p[i].rt > q) ? q : p[i].rt; int start = time; time += exec; p[i].rt -= exec; if(p[i].rt == 0){ p[i].ct = time; p[i].tat = p[i].ct; p[i].wt = p[i].tat - p[i].bt; done++; printf("| %s (%d~%d 완료) ", p[i].pid, time - exec, time); }else{ printf("| %s (%d~%d, 남은 %d) ", p[i].pid, start, time, p[i].rt); } if(done < N){ printf("| CS (%d~%d) ", time, time + cs); time += cs; } } } } printf("|\n"); printf("\nProcess\tBT\tCT\tTAT\tWT\n"); int sumTAT = 0, sumWT = 0; for(int i = 0; i < N; i++){ printf("%s\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].bt, p[i].ct, p[i].tat, p[i].wt); sumTAT += p[i].tat; sumWT += p[i].wt; } printf("\n평균 반환시간 : %.2f\n", (float)sumTAT / N); printf("평균 대기시간 : %.2f\n", (float)sumWT / N); return 0; }