c语言实现FCFS、SJF、PSA、RR的代码
时间: 2023-07-10 15:34:50 浏览: 133
以下是C语言实现FCFS、SJF、PSA、RR的代码。
FCFS(先来先服务):
```c
#include<stdio.h>
int main()
{
int bt[20], wt[20], tat[20], n, i, j;
float avwt = 0, avtat = 0;
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;
for(i = 1; i < n; i++)
{
wt[i] = 0;
for(j = 0; j < i; j++)
wt[i] += bt[j];
}
printf("\nProcess\t\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\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: %f", avwt);
printf("\nAverage Turnaround Time: %f", avtat);
return 0;
}
```
SJF(短作业优先):
```c
#include<stdio.h>
int main()
{
int bt[20], wt[20], tat[20], p[20], i, j, n, temp;
float avwt = 0, avtat = 0;
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]);
p[i] = i+1;
}
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;
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
wt[0] = 0;
for(i = 1; i < n; i++)
{
wt[i] = 0;
for(j = 0; j < i; j++)
wt[i] += bt[j];
}
printf("\nProcess\t\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\t%d\t\t%d\t\t%d", p[i], bt[i], wt[i], tat[i]);
}
avwt /= i;
avtat /= i;
printf("\n\nAverage Waiting Time: %f", avwt);
printf("\nAverage Turnaround Time: %f", avtat);
return 0;
}
```
PSA(优先级调度算法):
```c
#include<stdio.h>
int main()
{
int bt[20], wt[20], tat[20], p[20], pr[20], i, j, n, temp;
float avwt = 0, avtat = 0;
printf("Enter total number of processes(maximum 20): ");
scanf("%d", &n);
printf("\nEnter Process Burst Time and Priority: \n");
for(i = 0; i < n; i++)
{
printf("P[%d]: ", i+1);
scanf("%d %d", &bt[i], &pr[i]);
p[i] = i+1;
}
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(pr[i] > pr[j])
{
temp = bt[i];
bt[i] = bt[j];
bt[j] = temp;
temp = pr[i];
pr[i] = pr[j];
pr[j] = temp;
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
wt[0] = 0;
for(i = 1; i < n; i++)
{
wt[i] = 0;
for(j = 0; j < i; j++)
wt[i] += bt[j];
}
printf("\nProcess\t\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\t%d\t\t%d\t\t%d", p[i], bt[i], wt[i], tat[i]);
}
avwt /= i;
avtat /= i;
printf("\n\nAverage Waiting Time: %f", avwt);
printf("\nAverage Turnaround Time: %f", avtat);
return 0;
}
```
RR(时间片轮转调度算法):
```c
#include<stdio.h>
int main()
{
int i, n, qt, wt[20], tat[20], bt[20], rem_bt[20], time = 0;
float avwt = 0, avtat = 0;
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]);
rem_bt[i] = bt[i];
}
printf("\nEnter Time Quantum: ");
scanf("%d", &qt);
while(1)
{
int flag = 0;
for(i = 0; i < n; i++)
{
if(rem_bt[i] > 0)
{
flag = 1;
if(rem_bt[i] > qt)
{
time += qt;
rem_bt[i] -= qt;
}
else
{
time += rem_bt[i];
wt[i] = time - bt[i];
rem_bt[i] = 0;
}
}
}
if(flag == 0)
break;
}
for(i = 0; i < n; i++)
{
tat[i] = bt[i] + wt[i];
avwt += wt[i];
avtat += tat[i];
}
avwt /= i;
avtat /= i;
printf("\n\nAverage Waiting Time: %f", avwt);
printf("\nAverage Turnaround Time: %f", avtat);
return 0;
}
```
阅读全文