进程调度实验实现FCFS算法和SJF算法的C语言代码
时间: 2023-11-18 19:26:41 浏览: 113
FCFS算法的C语言代码:
#include <stdio.h>
int main() {
int n, bt[20], wt[20], tat[20], avwt = 0, avtat = 0, i;
printf("Enter total number of processes(maximum 20): ");
scanf("%d", &n);
printf("\nEnter Process Burst Time\n");
for(i = 0; i < n; i++) {
printf("P[%d]: ", i+1);
scanf("%d", &bt[i]);
}
wt[0] = 0; // waiting time for first process is 0
for(i = 1; i < n; i++) {
wt[i] = wt[i-1] + bt[i-1];
}
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time");
for(i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
avwt += wt[i];
avtat += tat[i];
printf("\nP[%d]\t%d\t\t%d\t\t%d", i+1, bt[i], wt[i], tat[i]);
}
avwt /= i;
avtat /= i;
printf("\n\nAverage Waiting Time: %d", avwt);
printf("\nAverage Turnaround Time: %d", avtat);
return 0;
}
SJF算法的C语言代码:
#include <stdio.h>
int main() {
int n, bt[20], wt[20], tat[20], avwt = 0, avtat = 0, i, j, temp;
printf("Enter total number of processes(maximum 20): ");
scanf("%d", &n);
printf("\nEnter Process Burst Time\n");
for(i = 0; i < n; i++) {
printf("P[%d]: ", i+1);
scanf("%d", &bt[i]);
}
for(i = 0; i < n; i++) {
for(j = i+1; j < n; j++) {
if(bt[i] > bt[j]) {
temp = bt[i];
bt[i] = bt[j];
bt[j] = temp;
}
}
}
wt[0] = 0; // waiting time for first process is 0
for(i = 1; i < n; i++) {
wt[i] = wt[i-1] + bt[i-1];
}
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time");
for(i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
avwt += wt[i];
avtat += tat[i];
printf("\nP[%d]\t%d\t\t%d\t\t%d", i+1, bt[i], wt[i], tat[i]);
}
avwt /= i;
avtat /= i;
printf("\n\nAverage Waiting Time: %d", avwt);
printf("\nAverage Turnaround Time: %d", avtat);
return 0;
}
阅读全文