C语言编程模拟实现进程调度算法 SPF算法 (1)设计一个具有n(5≦n≦10)个并发进程的进程调度程序。每个进程用一个进程控制块(PCB)表示并作为管理的依据,采用结构体类型即可。进程控制块一般包含下列信息:进程名,进程到达时间、进程要求服务的时间,还需占用CPU的时间、进程优先级数以及进程的状态等。具体参数可以根据调度算法的不同进行增删,或者设计成一个通用的PCB。 (2)调度程序应包含两种不同的调度算法:FCFS和SPF调度算法。运行时,可由用户通过终端任选一种,以便进行各种算法的分析比较。 (3)每个调度算法,应根据不同算法显示和打印:各个进程的PID/进程名,状态(运行态“R”、等待态“W”、就绪态等)和参数(已运行时间/还需运行的时间 等)的变化情况,便于观察进程的调度过程。 (4)所有进程结束后,要打印输出各进程的执行次序,各进程的完成时间,各进程的周转时间及带权周转时间等参数指标,最后计算平均周转时间和平均带权周转时间
时间: 2024-02-06 19:12:11 浏览: 210
C语言的进程调度的算法
3星 · 编辑精心推荐
好的,下面是一个简单的实现过程,供你参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PROCESS_NUM 10
#define PROCESS_NAME_LEN 20
typedef enum {
NEW = 0,
READY,
RUNNING,
WAITING,
TERMINATED
} process_status_t;
typedef struct {
char name[PROCESS_NAME_LEN];
int arrive_time;
int service_time;
int remain_time;
int priority;
int start_time;
int finish_time;
process_status_t status;
} pcb_t;
pcb_t processes[MAX_PROCESS_NUM];
int process_num = 0;
void init_processes() {
// 初始化进程控制块
int i;
for (i = 0; i < MAX_PROCESS_NUM; i++) {
processes[i].name[0] = '\0';
processes[i].arrive_time = 0;
processes[i].service_time = 0;
processes[i].remain_time = 0;
processes[i].priority = 0;
processes[i].start_time = 0;
processes[i].finish_time = 0;
processes[i].status = NEW;
}
process_num = 0;
}
void add_process(char name[], int arrive_time, int service_time) {
// 添加进程
if (process_num < MAX_PROCESS_NUM) {
strcpy(processes[process_num].name, name);
processes[process_num].arrive_time = arrive_time;
processes[process_num].service_time = service_time;
processes[process_num].remain_time = service_time;
processes[process_num].priority = 0;
processes[process_num].start_time = 0;
processes[process_num].finish_time = 0;
processes[process_num].status = NEW;
process_num++;
}
}
void print_process(pcb_t process) {
// 打印进程信息
printf("%s\t", process.name);
switch (process.status) {
case NEW:
printf("NEW\t");
break;
case READY:
printf("READY\t");
break;
case RUNNING:
printf("RUNNING\t");
break;
case WAITING:
printf("WAITING\t");
break;
case TERMINATED:
printf("TERMINATED\t");
break;
}
printf("%d/%d\t", process.service_time - process.remain_time, process.service_time);
printf("%d\t", process.priority);
printf("%d\t", process.start_time);
printf("%d\n", process.finish_time);
}
void print_processes() {
// 打印所有进程信息
int i;
for (i = 0; i < process_num; i++) {
print_process(processes[i]);
}
}
void swap(pcb_t *a, pcb_t *b) {
// 交换两个进程控制块
pcb_t tmp = *a;
*a = *b;
*b = tmp;
}
void sort_by_arrive_time() {
// 按到达时间排序
int i, j;
for (i = 0; i < process_num - 1; i++) {
for (j = 0; j < process_num - i - 1; j++) {
if (processes[j].arrive_time > processes[j + 1].arrive_time) {
swap(&processes[j], &processes[j + 1]);
}
}
}
}
void sort_by_remain_time() {
// 按剩余服务时间排序
int i, j;
for (i = 0; i < process_num - 1; i++) {
for (j = 0; j < process_num - i - 1; j++) {
if (processes[j].remain_time > processes[j + 1].remain_time) {
swap(&processes[j], &processes[j + 1]);
}
}
}
}
void fcfs() {
// 先来先服务调度算法
int current_time = 0;
int i;
sort_by_arrive_time();
for (i = 0; i < process_num; i++) {
if (processes[i].arrive_time > current_time) {
current_time = processes[i].arrive_time;
}
processes[i].start_time = current_time;
processes[i].finish_time = current_time + processes[i].service_time;
processes[i].status = TERMINATED;
current_time = processes[i].finish_time;
}
}
void spf() {
// 短作业优先调度算法
int current_time = 0;
int i, j;
sort_by_arrive_time();
for (i = 0; i < process_num; i++) {
processes[i].status = READY;
}
for (i = 0; i < process_num; i++) {
int min_remain_time = INT_MAX;
int min_remain_time_index = -1;
for (j = 0; j < process_num; j++) {
if (processes[j].status == READY && processes[j].arrive_time <= current_time && processes[j].remain_time < min_remain_time) {
min_remain_time = processes[j].remain_time;
min_remain_time_index = j;
}
}
if (min_remain_time_index == -1) {
current_time++;
} else {
processes[min_remain_time_index].status = RUNNING;
processes[min_remain_time_index].start_time = current_time;
current_time += processes[min_remain_time_index].remain_time;
processes[min_remain_time_index].remain_time = 0;
processes[min_remain_time_index].finish_time = current_time;
processes[min_remain_time_index].status = TERMINATED;
}
}
}
void print_result() {
// 打印结果
int i;
float total_turnaround_time = 0;
float total_weighted_turnaround_time = 0;
printf("Process execution sequence:\n");
for (i = 0; i < process_num; i++) {
printf("%s ", processes[i].name);
}
printf("\n");
printf("Process execution result:\n");
printf("Name\tStatus\tProgress\tPriority\tStart\tFinish\n");
print_processes();
printf("Process performance:\n");
printf("Name\tTurnaround Time\tWeighted Turnaround Time\n");
for (i = 0; i < process_num; i++) {
float turnaround_time = processes[i].finish_time - processes[i].arrive_time;
float weighted_turnaround_time = turnaround_time / processes[i].service_time;
total_turnaround_time += turnaround_time;
total_weighted_turnaround_time += weighted_turnaround_time;
printf("%s\t%.2f\t%.2f\n", processes[i].name, turnaround_time, weighted_turnaround_time);
}
printf("Average turnaround time: %.2f\n", total_turnaround_time / process_num);
printf("Average weighted turnaround time: %.2f\n", total_weighted_turnaround_time / process_num);
}
int main() {
init_processes();
add_process("P1", 0, 3);
add_process("P2", 2, 6);
add_process("P3", 4, 4);
add_process("P4", 6, 5);
add_process("P5", 8, 2);
spf();
print_result();
return 0;
}
```
以上代码实现了两种调度算法,FCFS和SPF,同时可以根据进程的状态和参数动态打印执行过程。你可以根据自己的需要进行修改和优化。
阅读全文