编写C程序模拟实现单处理机系统中的进程调度算法,实现对多个进程的调度模拟
时间: 2023-12-18 09:05:44 浏览: 141
下面是一个简单的 C 语言程序,模拟了三种进程调度算法:先来先服务(FCFS)、最短作业优先(SJF)和时间片轮转(RR)。
程序中定义了一个 `Process` 结构体,包含了进程的 ID、到达时间、服务时间、开始时间、完成时间、周转时间和带权周转时间等信息。在主函数中,首先读入进程数和时间片长度(对于 RR 算法),然后读入每个进程的信息。接着按照三种算法分别进行进程调度,并输出每个进程的各项信息。
```
#include <stdio.h>
#include <stdlib.h>
typedef struct Process {
int id; // 进程 ID
int arrive; // 到达时间
int serve; // 服务时间
int start; // 开始时间
int finish; // 完成时间
int turnaround; // 周转时间
double weight; // 带权周转时间
} Process;
int cmp_arrive(const void *a, const void *b) {
return ((Process*)a)->arrive - ((Process*)b)->arrive;
}
int cmp_serve(const void *a, const void *b) {
return ((Process*)a)->serve - ((Process*)b)->serve;
}
int main() {
int n, q; // 进程数和时间片长度(对于 RR 算法)
scanf("%d", &n);
Process proc[n];
for (int i = 0; i < n; i++) {
scanf("%d%d", &proc[i].arrive, &proc[i].serve);
proc[i].id = i + 1;
}
// 先来先服务(FCFS)
qsort(proc, n, sizeof(Process), cmp_arrive);
proc[0].start = proc[0].arrive;
proc[0].finish = proc[0].start + proc[0].serve;
proc[0].turnaround = proc[0].finish - proc[0].arrive;
proc[0].weight = (double)proc[0].turnaround / proc[0].serve;
for (int i = 1; i < n; i++) {
proc[i].start = proc[i-1].finish;
proc[i].finish = proc[i].start + proc[i].serve;
proc[i].turnaround = proc[i].finish - proc[i].arrive;
proc[i].weight = (double)proc[i].turnaround / proc[i].serve;
}
printf("FCFS:\n");
printf("ID\tArrive\tServe\tStart\tFinish\tTurnaround\tWeight\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\t%d\t\t%.2f\n", proc[i].id, proc[i].arrive, proc[i].serve,
proc[i].start, proc[i].finish, proc[i].turnaround, proc[i].weight);
}
puts("");
// 最短作业优先(SJF)
qsort(proc, n, sizeof(Process), cmp_serve);
int curtime = proc[0].arrive;
for (int i = 0; i < n; i++) {
if (proc[i].arrive > curtime) {
curtime = proc[i].arrive;
}
proc[i].start = curtime;
proc[i].finish = curtime + proc[i].serve;
proc[i].turnaround = proc[i].finish - proc[i].arrive;
proc[i].weight = (double)proc[i].turnaround / proc[i].serve;
curtime = proc[i].finish;
}
printf("SJF:\n");
printf("ID\tArrive\tServe\tStart\tFinish\tTurnaround\tWeight\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\t%d\t\t%.2f\n", proc[i].id, proc[i].arrive, proc[i].serve,
proc[i].start, proc[i].finish, proc[i].turnaround, proc[i].weight);
}
puts("");
// 时间片轮转(RR)
printf("Please input the time slice length:\n");
scanf("%d", &q);
int cur = 0, remain = n;
while (remain > 0) {
for (int i = 0; i < n; i++) {
if (proc[i].serve > 0) {
if (proc[i].arrive <= cur) {
int slice = (proc[i].serve > q) ? q : proc[i].serve;
proc[i].serve -= slice;
cur += slice;
if (proc[i].serve == 0) {
remain--;
proc[i].finish = cur;
proc[i].turnaround = proc[i].finish - proc[i].arrive;
proc[i].weight = (double)proc[i].turnaround / proc[i].serve;
}
}
}
}
}
printf("RR:\n");
printf("ID\tArrive\tServe\tStart\tFinish\tTurnaround\tWeight\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\t%d\t\t%.2f\n", proc[i].id, proc[i].arrive, proc[i].serve,
proc[i].start, proc[i].finish, proc[i].turnaround, proc[i].weight);
}
return 0;
}
```
注意,这只是一个简单的模拟程序,实际的进程调度算法还需要考虑更多的因素,如进程优先级、进程的 I/O 操作、进程间的同步与通信等。
阅读全文