编写和调试一个进程调度程序,实现基于优先权的进程调度算法和基于时间片轮转调度算法。
时间: 2023-06-16 11:08:19 浏览: 162
pcb.rar_visual c_调度_调度算法_轮转调度_轮转调度算法
这里是一个基于C语言的进程调度程序,实现了基于优先权的进程调度算法和基于时间片轮转调度算法。本程序中,优先级值越小,优先级越高。时间片长度为2个时间单位。
```
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
#define TIME_QUANTUM 2
struct process {
int pid; // 进程ID
int priority; // 优先级
int burst_time; // 执行时间
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
};
void execute_priority(struct process *proc, int n) {
int i, j;
struct process temp;
float total_waiting_time = 0, total_turnaround_time = 0;
// 按照优先级排序
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (proc[i].priority > proc[j].priority) {
temp = proc[i];
proc[i] = proc[j];
proc[j] = temp;
}
}
}
// 计算等待时间和周转时间
for (i = 0; i < n; i++) {
if (i == 0) {
proc[i].waiting_time = 0;
proc[i].turnaround_time = proc[i].burst_time;
} else {
proc[i].waiting_time = proc[i-1].waiting_time + proc[i-1].burst_time;
proc[i].turnaround_time = proc[i].waiting_time + proc[i].burst_time;
}
total_waiting_time += proc[i].waiting_time;
total_turnaround_time += proc[i].turnaround_time;
}
// 输出结果
printf("\nProcess ID\tPriority\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", proc[i].pid, proc[i].priority, proc[i].burst_time, proc[i].waiting_time, proc[i].turnaround_time);
}
printf("Average Waiting Time: %f\n", total_waiting_time/n);
printf("Average Turnaround Time: %f\n", total_turnaround_time/n);
}
void execute_round_robin(struct process *proc, int n) {
int i, j, time = 0, remain = n, flag = 0;
float total_waiting_time = 0, total_turnaround_time = 0;
// 执行进程
while (remain != 0) {
if (proc[flag].burst_time <= TIME_QUANTUM && proc[flag].burst_time > 0) {
time += proc[flag].burst_time;
proc[flag].burst_time = 0;
flag++;
remain--;
} else if (proc[flag].burst_time > 0) {
proc[flag].burst_time -= TIME_QUANTUM;
time += TIME_QUANTUM;
flag++;
}
if (flag == n) flag = 0;
}
// 计算等待时间和周转时间
for (i = 0; i < n; i++) {
proc[i].turnaround_time = time;
proc[i].waiting_time = proc[i].turnaround_time - proc[i].burst_time;
total_waiting_time += proc[i].waiting_time;
total_turnaround_time += proc[i].turnaround_time;
}
// 输出结果
printf("\nProcess ID\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\n", proc[i].pid, proc[i].burst_time, proc[i].waiting_time, proc[i].turnaround_time);
}
printf("Average Waiting Time: %f\n", total_waiting_time/n);
printf("Average Turnaround Time: %f\n", total_turnaround_time/n);
}
int main() {
int i, n, algorithm;
struct process proc[MAX];
// 输入进程信息
printf("Enter the number of processes: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter the process ID: ");
scanf("%d", &proc[i].pid);
printf("Enter the burst time: ");
scanf("%d", &proc[i].burst_time);
printf("Enter the priority: ");
scanf("%d", &proc[i].priority);
}
// 选择调度算法
printf("\nChoose a scheduling algorithm:\n");
printf("1. Priority scheduling\n");
printf("2. Round-robin scheduling\n");
printf("Enter your choice: ");
scanf("%d", &algorithm);
// 执行调度算法
switch (algorithm) {
case 1: execute_priority(proc, n); break;
case 2: execute_round_robin(proc, n); break;
default: printf("\nInvalid choice!\n");
}
return 0;
}
```
阅读全文