本次实验需要用优先权算法和轮转法实现两种进程调度算法。假设进程为计算型的,进程状态为ready、running、finish。进程需要的CPU时间以时间片为单位确定。需要使用动态数据结构,产生的各种随机数的取值范围需要加以限制,如所需的CPU时间限制在1~20之间。进程数n不要太大通常取4~8个。 实验要求:1,产生的各种随机数的取值范围加以限制,如所需的CPU时间限制在1~20之间。2,进程数n不要太大通常取4~8个。3,使用动态数据结构。4,独立编程。5,两种调度算法的结果进行比较和分析,包括平均等待时间、平均周转时间等指标。 最后用c语言分别输出两种算法的源代码
时间: 2024-02-13 13:04:33 浏览: 88
好的,我已经了解您的问题。下面是使用C语言实现优先权算法和轮转法的进程调度算法的源代码。
优先权算法:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
int pid;
int priority;
int burst_time;
int waiting_time;
int turnaround_time;
int remaining_time;
} process;
void swap(process* a, process* b) {
process temp = *a;
*a = *b;
*b = temp;
}
void sort(process* processes, int n) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (processes[i].priority < processes[j].priority) {
swap(&processes[i], &processes[j]);
}
}
}
}
int main() {
srand(time(NULL));
int n = 5; // 进程数
int time_slice = 2; // 时间片
process* processes = (process*)malloc(n * sizeof(process));
// 随机生成进程的priority和burst_time
for (int i = 0; i < n; i++) {
processes[i].pid = i + 1;
processes[i].priority = rand() % 10 + 1;
processes[i].burst_time = rand() % 20 + 1;
processes[i].remaining_time = processes[i].burst_time;
}
int current_time = 0;
int total_waiting_time = 0;
int total_turnaround_time = 0;
while (1) {
int all_finished = 1;
for (int i = 0; i < n; i++) {
if (processes[i].remaining_time > 0) {
all_finished = 0;
if (processes[i].remaining_time > time_slice) {
current_time += time_slice;
processes[i].remaining_time -= time_slice;
} else {
current_time += processes[i].remaining_time;
processes[i].turnaround_time = current_time;
processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time;
processes[i].remaining_time = 0;
total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;
}
}
}
if (all_finished) {
break;
}
sort(processes, n);
}
float avg_waiting_time = (float)total_waiting_time / n;
float avg_turnaround_time = (float)total_turnaround_time / n;
printf("优先权算法\n");
printf("进程\t优先级\t服务时间\t等待时间\t周转时间\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].priority, processes[i].burst_time, processes[i].waiting_time, processes[i].turnaround_time);
}
printf("平均等待时间: %.2f\n", avg_waiting_time);
printf("平均周转时间: %.2f\n", avg_turnaround_time);
free(processes);
return 0;
}
```
轮转法:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
int pid;
int burst_time;
int waiting_time;
int turnaround_time;
int remaining_time;
} process;
int main() {
srand(time(NULL));
int n = 5; // 进程数
int time_slice = 2; // 时间片
process* processes = (process*)malloc(n * sizeof(process));
// 随机生成进程的burst_time
for (int i = 0; i < n; i++) {
processes[i].pid = i + 1;
processes[i].burst_time = rand() % 20 + 1;
processes[i].remaining_time = processes[i].burst_time;
}
int current_time = 0;
int total_waiting_time = 0;
int total_turnaround_time = 0;
while (1) {
int all_finished = 1;
for (int i = 0; i < n; i++) {
if (processes[i].remaining_time > 0) {
all_finished = 0;
if (processes[i].remaining_time > time_slice) {
current_time += time_slice;
processes[i].remaining_time -= time_slice;
} else {
current_time += processes[i].remaining_time;
processes[i].turnaround_time = current_time;
processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time;
processes[i].remaining_time = 0;
total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;
}
}
}
if (all_finished) {
break;
}
}
float avg_waiting_time = (float)total_waiting_time / n;
float avg_turnaround_time = (float)total_turnaround_time / n;
printf("轮转法\n");
printf("进程\t服务时间\t等待时间\t周转时间\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].burst_time, processes[i].waiting_time, processes[i].turnaround_time);
}
printf("平均等待时间: %.2f\n", avg_waiting_time);
printf("平均周转时间: %.2f\n", avg_turnaround_time);
free(processes);
return 0;
}
```
阅读全文