单道处理系统的作业等待模拟程序。 作业等待算法:分别采用先来先服务(FCFS),最短作业优先(SJF)、响应比高者优先(HRN)的调度算法。使用c语言
时间: 2023-06-13 21:03:27 浏览: 169
以下是一个基于C语言的作业等待模拟程序,包括FCFS、SJF和HRN算法的调度实现:
```c
#include <stdio.h>
#define MAX_JOBS 10 // 最大作业数
// 作业结构体
typedef struct {
int id; // 作业编号
int arrival_time; // 到达时间
int burst_time; // 运行时间
int start_time; // 开始时间
int finish_time; // 完成时间
int turnaround_time; // 周转时间
int waiting_time; // 等待时间
} Job;
// 作业队列结构体
typedef struct {
Job jobs[MAX_JOBS]; // 作业数组
int size; // 作业数
} JobQueue;
// 初始化作业队列
void init_job_queue(JobQueue *queue) {
queue->size = 0;
}
// 添加作业到队列
void add_job(JobQueue *queue, Job job) {
if (queue->size < MAX_JOBS) {
queue->jobs[queue->size++] = job;
}
}
// FCFS调度算法实现
void fcfs_schedule(JobQueue *queue) {
int current_time = 0;
for (int i = 0; i < queue->size; i++) {
Job *job = &queue->jobs[i];
if (job->arrival_time > current_time) {
current_time = job->arrival_time;
}
job->start_time = current_time;
job->finish_time = current_time + job->burst_time;
job->turnaround_time = job->finish_time - job->arrival_time;
job->waiting_time = job->turnaround_time - job->burst_time;
current_time = job->finish_time;
}
}
// SJF调度算法实现
void sjf_schedule(JobQueue *queue) {
int current_time = 0;
int remaining_jobs = queue->size;
while (remaining_jobs > 0) {
int shortest_job_index = -1;
int shortest_job_burst_time = -1;
for (int i = 0; i < queue->size; i++) {
Job *job = &queue->jobs[i];
if (job->arrival_time <= current_time && job->burst_time > 0) {
if (shortest_job_index < 0 || job->burst_time < shortest_job_burst_time) {
shortest_job_index = i;
shortest_job_burst_time = job->burst_time;
}
}
}
if (shortest_job_index < 0) {
current_time++;
} else {
Job *job = &queue->jobs[shortest_job_index];
job->start_time = current_time;
job->finish_time = current_time + job->burst_time;
job->turnaround_time = job->finish_time - job->arrival_time;
job->waiting_time = job->turnaround_time - job->burst_time;
current_time = job->finish_time;
job->burst_time = 0;
remaining_jobs--;
}
}
}
// HRN调度算法实现
void hrn_schedule(JobQueue *queue) {
int current_time = 0;
int remaining_jobs = queue->size;
while (remaining_jobs > 0) {
int highest_response_ratio_job_index = -1;
float highest_response_ratio = -1;
for (int i = 0; i < queue->size; i++) {
Job *job = &queue->jobs[i];
if (job->arrival_time <= current_time && job->burst_time > 0) {
float response_ratio = (current_time - job->arrival_time + job->burst_time) / (float)job->burst_time;
if (highest_response_ratio_job_index < 0 || response_ratio > highest_response_ratio) {
highest_response_ratio_job_index = i;
highest_response_ratio = response_ratio;
}
}
}
if (highest_response_ratio_job_index < 0) {
current_time++;
} else {
Job *job = &queue->jobs[highest_response_ratio_job_index];
job->start_time = current_time;
job->finish_time = current_time + job->burst_time;
job->turnaround_time = job->finish_time - job->arrival_time;
job->waiting_time = job->turnaround_time - job->burst_time;
current_time = job->finish_time;
job->burst_time = 0;
remaining_jobs--;
}
}
}
// 输出作业队列信息
void print_job_queue(JobQueue *queue) {
printf("ID Arrival Time Burst Time Start Time Finish Time Turnaround Time Waiting Time\n");
for (int i = 0; i < queue->size; i++) {
Job *job = &queue->jobs[i];
printf("%-3d %-13d %-11d %-11d %-12d %-16d %-12d\n", job->id, job->arrival_time, job->burst_time,
job->start_time, job->finish_time, job->turnaround_time, job->waiting_time);
}
}
int main() {
// 初始化作业队列
JobQueue queue;
init_job_queue(&queue);
// 添加作业到队列
add_job(&queue, (Job) {1, 0, 6});
add_job(&queue, (Job) {2, 1, 8});
add_job(&queue, (Job) {3, 2, 7});
add_job(&queue, (Job) {4, 3, 3});
add_job(&queue, (Job) {5, 4, 4});
add_job(&queue, (Job) {6, 5, 5});
// FCFS调度算法
fcfs_schedule(&queue);
printf("FCFS Scheduling:\n");
print_job_queue(&queue);
// SJF调度算法
sjf_schedule(&queue);
printf("SJF Scheduling:\n");
print_job_queue(&queue);
// HRN调度算法
hrn_schedule(&queue);
printf("HRN Scheduling:\n");
print_job_queue(&queue);
return 0;
}
```
上述程序中,先定义了一个作业结构体和一个作业队列结构体,其中作业结构体包含作业的各项信息(编号、到达时间、运行时间、开始时间、完成时间、周转时间、等待时间),作业队列结构体包含作业数组和作业数。然后定义了一些函数,包括初始化作业队列、添加作业到队列、FCFS调度算法实现、SJF调度算法实现、HRN调度算法实现、输出作业队列信息等。主函数中初始化了一个作业队列,并添加了6个作业到队列中,然后依次调用FCFS、SJF和HRN算法进行作业调度,并输出结果。
在FCFS算法中,作业按照到达时间的先后顺序依次被执行。在SJF算法中,每次选择剩余运行时间最短的作业执行。在HRN算法中,每次选择响应比最高的作业执行,其中响应比定义为(等待时间+运行时间)/运行时间。
阅读全文