运用c语言编程实现进程调度算法的基本过程,设计要求: (1)能够选择进程调度算法(先来先服务、短进程优先算法和高响应比优先算法)。 (2)可以输入进程数目(至少3个进程),以及各进程的提交时间和运行时间。 (3)能够显示调度过程及相应时间。并有相关注释
时间: 2023-12-29 17:05:37 浏览: 77
C _实现进程调度算法_有先来先服务、优先级调度、短作业优先、响应比高优先.doc
5星 · 资源好评率100%
下面是一个基于C语言的进程调度算法实现,包含先来先服务、短进程优先算法和高响应比优先算法。设计要求能够输入进程数目,提交时间和运行时间,并显示调度过程及相应时间。
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_PROCESS_NUM 20
// 进程结构体
typedef struct process {
int id; // 进程标识符
int arrive_time; // 到达时间
int run_time; // 运行时间
int wait_time; // 等待时间
int start_time; // 开始时间
int finish_time; // 结束时间
} process_t;
// 进程队列结构体
typedef struct process_queue {
process_t *process_list[MAX_PROCESS_NUM];
int size;
} process_queue_t;
// 初始化进程队列
void init_process_queue(process_queue_t *queue) {
int i;
for (i = 0; i < MAX_PROCESS_NUM; ++i) {
queue->process_list[i] = NULL;
}
queue->size = 0;
}
// 添加进程到队列中
void add_process_to_queue(process_queue_t *queue, process_t *process) {
queue->process_list[queue->size++] = process;
}
// 从队列中删除进程
void remove_process_from_queue(process_queue_t *queue, process_t *process) {
int i, j;
for (i = 0; i < queue->size; ++i) {
if (queue->process_list[i]->id == process->id) {
queue->size--;
for (j = i; j < queue->size; ++j) {
queue->process_list[j] = queue->process_list[j + 1];
}
break;
}
}
}
// 获取队列中最短进程
process_t* get_shortest_process(process_queue_t *queue) {
int i;
process_t *shortest_process = queue->process_list[0];
for (i = 1; i < queue->size; ++i) {
if (queue->process_list[i]->run_time < shortest_process->run_time) {
shortest_process = queue->process_list[i];
}
}
return shortest_process;
}
// 获取队列中高响应比进程
process_t* get_highest_response_ratio_process(process_queue_t *queue, int current_time) {
int i;
process_t *highest_response_ratio_process = queue->process_list[0];
float highest_response_ratio = (float)(current_time - highest_response_ratio_process->arrive_time + highest_response_ratio_process->run_time) / highest_response_ratio_process->run_time;
for (i = 1; i < queue->size; ++i) {
float response_ratio = (float)(current_time - queue->process_list[i]->arrive_time + queue->process_list[i]->run_time) / queue->process_list[i]->run_time;
if (response_ratio > highest_response_ratio) {
highest_response_ratio = response_ratio;
highest_response_ratio_process = queue->process_list[i];
}
}
return highest_response_ratio_process;
}
// 先来先服务算法
void fcfs_schedule(process_queue_t *queue) {
int current_time = 0;
while (queue->size > 0) {
process_t *process = queue->process_list[0];
if (current_time < process->arrive_time) {
current_time = process->arrive_time;
}
process->start_time = current_time;
process->finish_time = current_time + process->run_time;
process->wait_time = current_time - process->arrive_time;
current_time = process->finish_time;
remove_process_from_queue(queue, process);
}
}
// 短进程优先算法
void sjf_schedule(process_queue_t *queue) {
int current_time = 0;
while (queue->size > 0) {
process_t *shortest_process = get_shortest_process(queue);
if (current_time < shortest_process->arrive_time) {
current_time = shortest_process->arrive_time;
}
shortest_process->start_time = current_time;
shortest_process->finish_time = current_time + shortest_process->run_time;
shortest_process->wait_time = current_time - shortest_process->arrive_time;
current_time = shortest_process->finish_time;
remove_process_from_queue(queue, shortest_process);
}
}
// 高响应比优先算法
void hrrn_schedule(process_queue_t *queue) {
int current_time = 0;
while (queue->size > 0) {
process_t *highest_response_ratio_process = get_highest_response_ratio_process(queue, current_time);
if (current_time < highest_response_ratio_process->arrive_time) {
current_time = highest_response_ratio_process->arrive_time;
}
highest_response_ratio_process->start_time = current_time;
highest_response_ratio_process->finish_time = current_time + highest_response_ratio_process->run_time;
highest_response_ratio_process->wait_time = current_time - highest_response_ratio_process->arrive_time;
current_time = highest_response_ratio_process->finish_time;
remove_process_from_queue(queue, highest_response_ratio_process);
}
}
int main() {
int i, num_of_processes;
process_queue_t queue;
process_t processes[MAX_PROCESS_NUM];
// 初始化进程队列
init_process_queue(&queue);
// 输入进程数目
printf("请输入进程数目(至少3个进程):");
scanf("%d", &num_of_processes);
if (num_of_processes < 3 || num_of_processes > MAX_PROCESS_NUM) {
printf("进程数目无效!\n");
return -1;
}
// 输入各进程的提交时间和运行时间
for (i = 0; i < num_of_processes; ++i) {
printf("请输入第%d个进程的提交时间和运行时间:", i + 1);
scanf("%d%d", &processes[i].arrive_time, &processes[i].run_time);
processes[i].id = i + 1;
processes[i].wait_time = 0;
processes[i].start_time = 0;
processes[i].finish_time = 0;
add_process_to_queue(&queue, &processes[i]);
}
// 先来先服务算法
printf("先来先服务算法:\n");
fcfs_schedule(&queue);
for (i = 0; i < num_of_processes; ++i) {
printf("进程%d的开始时间:%d,结束时间:%d,等待时间:%d\n", processes[i].id, processes[i].start_time, processes[i].finish_time, processes[i].wait_time);
}
printf("\n");
// 短进程优先算法
printf("短进程优先算法:\n");
init_process_queue(&queue);
for (i = 0; i < num_of_processes; ++i) {
add_process_to_queue(&queue, &processes[i]);
}
sjf_schedule(&queue);
for (i = 0; i < num_of_processes; ++i) {
printf("进程%d的开始时间:%d,结束时间:%d,等待时间:%d\n", processes[i].id, processes[i].start_time, processes[i].finish_time, processes[i].wait_time);
}
printf("\n");
// 高响应比优先算法
printf("高响应比优先算法:\n");
init_process_queue(&queue);
for (i = 0; i < num_of_processes; ++i) {
add_process_to_queue(&queue, &processes[i]);
}
hrrn_schedule(&queue);
for (i = 0; i < num_of_processes; ++i) {
printf("进程%d的开始时间:%d,结束时间:%d,等待时间:%d\n", processes[i].id, processes[i].start_time, processes[i].finish_time, processes[i].wait_time);
}
return 0;
}
```
注释:
1. 定义了进程结构体`process_t`和进程队列结构体`process_queue_t`;
2. `init_process_queue()`函数用于初始化进程队列,将队列中所有元素置为NULL,队列大小为0;
3. `add_process_to_queue()`函数用于向队列中添加一个进程;
4. `remove_process_from_queue()`函数用于从队列中删除一个进程;
5. `get_shortest_process()`函数用于从队列中获取运行时间最短的进程;
6. `get_highest_response_ratio_process()`函数用于从队列中获取响应比最高的进程;
7. `fcfs_schedule()`函数实现先来先服务算法;
8. `sjf_schedule()`函数实现短进程优先算法;
9. `hrrn_schedule()`函数实现高响应比优先算法;
10. `main()`函数中首先输入进程数目以及各进程的提交时间和运行时间,然后分别调用三种调度算法,并输出每个进程的开始时间、结束时间和等待时间。
阅读全文