设计一个C语言进程调度算法包括到达时间 执行时间 完成时间 并提示让用户自己输入时间片时间,对先来先服务算法,短进程优先算法,时间片轮转算法,进行三个算法的比较
时间: 2023-11-18 18:29:29 浏览: 69
好的,我可以为您提供一个基本的C语言进程调度算法的实现。
首先,让我们定义一些数据结构来表示进程的信息:
```c
struct process {
int pid; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int completion_time;// 完成时间
};
```
对于先来先服务算法,我们可以按照进程到达的顺序来执行它们。因此,我们可以按照以下方式实现:
```c
#include <stdio.h>
int main() {
int num_processes;
printf("Enter the number of processes: ");
scanf("%d", &num_processes);
struct process processes[num_processes];
for (int i = 0; i < num_processes; i++) {
printf("Enter arrival time and burst time for process %d: ", i+1);
scanf("%d %d", &processes[i].arrival_time, &processes[i].burst_time);
processes[i].pid = i+1;
}
// 执行进程调度
int current_time = 0;
for (int i = 0; i < num_processes; i++) {
int completion_time = current_time + processes[i].burst_time;
processes[i].completion_time = completion_time;
current_time = completion_time;
}
// 输出结果
printf("PID\tArrival Time\tBurst Time\tCompletion Time\n");
for (int i = 0; i < num_processes; i++) {
printf("%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].arrival_time,
processes[i].burst_time, processes[i].completion_time);
}
return 0;
}
```
对于短进程优先算法,我们可以按照进程的执行时间来排序,然后对排序后的进程执行先来先服务算法。因此,我们需要实现一个函数来按照进程的执行时间进行排序:
```c
void sort_by_burst_time(struct process *processes, int num_processes) {
for (int i = 0; i < num_processes-1; i++) {
for (int j = i+1; j < num_processes; j++) {
if (processes[i].burst_time > processes[j].burst_time) {
struct process temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
}
}
}
}
```
然后在主函数中调用这个函数,然后再进行先来先服务算法:
```c
#include <stdio.h>
int main() {
int num_processes;
printf("Enter the number of processes: ");
scanf("%d", &num_processes);
struct process processes[num_processes];
for (int i = 0; i < num_processes; i++) {
printf("Enter arrival time and burst time for process %d: ", i+1);
scanf("%d %d", &processes[i].arrival_time, &processes[i].burst_time);
processes[i].pid = i+1;
}
// 按照执行时间排序
sort_by_burst_time(processes, num_processes);
// 执行进程调度
int current_time = 0;
for (int i = 0; i < num_processes; i++) {
int completion_time = current_time + processes[i].burst_time;
processes[i].completion_time = completion_time;
current_time = completion_time;
}
// 输出结果
printf("PID\tArrival Time\tBurst Time\tCompletion Time\n");
for (int i = 0; i < num_processes; i++) {
printf("%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].arrival_time,
processes[i].burst_time, processes[i].completion_time);
}
return 0;
}
```
对于时间片轮转算法,我们需要让每个进程在一个时间片内执行一定的时间,然后切换到下一个进程。因此,我们需要实现一个函数来模拟时间片轮转:
```c
void round_robin(struct process *processes, int num_processes, int time_slice) {
int current_time = 0;
int num_completed_processes = 0;
while (num_completed_processes < num_processes) {
for (int i = 0; i < num_processes; i++) {
if (processes[i].burst_time <= 0) {
continue;
}
if (processes[i].arrival_time > current_time) {
continue;
}
int time_left = processes[i].burst_time - time_slice;
if (time_left < 0) {
current_time += processes[i].burst_time;
processes[i].burst_time = 0;
} else {
current_time += time_slice;
processes[i].burst_time = time_left;
}
if (processes[i].burst_time == 0) {
processes[i].completion_time = current_time;
num_completed_processes++;
}
}
}
}
```
然后在主函数中调用这个函数,然后再输出结果:
```c
#include <stdio.h>
int main() {
int num_processes;
printf("Enter the number of processes: ");
scanf("%d", &num_processes);
struct process processes[num_processes];
for (int i = 0; i < num_processes; i++) {
printf("Enter arrival time and burst time for process %d: ", i+1);
scanf("%d %d", &processes[i].arrival_time, &processes[i].burst_time);
processes[i].pid = i+1;
}
// 执行时间片轮转算法
int time_slice;
printf("Enter time slice: ");
scanf("%d", &time_slice);
round_robin(processes, num_processes, time_slice);
// 输出结果
printf("PID\tArrival Time\tBurst Time\tCompletion Time\n");
for (int i = 0; i < num_processes; i++) {
printf("%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].arrival_time,
processes[i].burst_time, processes[i].completion_time);
}
return 0;
}
```
至此,我们已经完成了三个进程调度算法的实现,并且可以进行比较了。您可以根据自己的需求选择合适的算法。
阅读全文