FIFO算法,优先数调度算法,时间片轮转调度算法进程调度代码
时间: 2023-11-18 16:57:37 浏览: 100
FIFO算法的进程调度代码:
```c
#include <stdio.h>
int main()
{
int n, bt[20], wt[20], tat[20], avwt = 0, avtat = 0, i, j;
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\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;
}
```
优先数调度算法的进程调度代码:
```c
#include <stdio.h>
int main()
{
int n, bt[20], wt[20], tat[20], p[20], pr[20], i, j, temp;
float avwt = 0, avtat = 0;
printf("Enter total number of processes (maximum 20): ");
scanf("%d", &n);
printf("\nEnter 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 = pr[i];
pr[i] = pr[j];
pr[j] = temp;
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];
avwt += wt[i];
}
avwt /= i;
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time");
for(i = 0; i < n; i++)
{
tat[i] = bt[i] + wt[i];
avtat += tat[i];
printf("\nP[%d]\t%d\t\t%d\t\t%d", p[i], bt[i], wt[i], tat[i]);
}
avtat /= i;
printf("\n\nAverage Waiting Time: %f", avwt);
printf("\nAverage Turnaround Time: %f", avtat);
return 0;
}
```
时间片轮转调度算法的进程调度代码:
```c
#include <stdio.h>
int main()
{
int n, bt[20], wt[20], tat[20], rt[20], i, j, time, remain, flag=0, tq;
float avwt = 0, avtat = 0;
printf("Enter total number of processes (maximum 20): ");
scanf("%d", &n);
remain = n;
printf("\nEnter Burst Time\n");
for(i = 0; i < n; i++)
{
printf("P[%d]: ", i+1);
scanf("%d", &bt[i]);
rt[i] = bt[i];
}
printf("Enter Time Quantum: ");
scanf("%d", &tq);
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
for(time = 0, i = 0; remain != 0;)
{
if(rt[i] <= tq && rt[i] > 0)
{
time += rt[i];
rt[i] = 0;
flag = 1;
}
else if(rt[i] > 0)
{
rt[i] -= tq;
time += tq;
}
if(rt[i] == 0 && flag == 1)
{
remain--;
wt[i] = time - bt[i];
tat[i] = time;
avwt += wt[i];
avtat += tat[i];
printf("P[%d]\t%d\t\t%d\t\t%d\n", i+1, bt[i], wt[i], tat[i]);
flag = 0;
}
if(i == n-1)
i = 0;
else if(time >= i+1)
i++;
}
avwt /= n;
avtat /= n;
printf("\nAverage Waiting Time: %f", avwt);
printf("\nAverage Turnaround Time: %f", avtat);
return 0;
}
```
阅读全文