前言
使用C语言设计采用先来先服务算法、运行时间最短者优先算法和最高响应比优先调度算法模拟设计作业调度程序。
一、
二、使用步骤
1.引入库
代码如下(示例):
#include <stdio.h>
#include <stdlib.h>
struct Job {
char name[20];
float arrival_time;
float completion_time;
};
void firstComeFirstServe(struct Job jobs[], int n) {
float current_time = 0;
printf("先来先服务\t开始时间\t结束时间\t周转时间\t周转系数\t\n");
for (int i = 0; i < n; i++) {
if (jobs[i].arrival_time > current_time) {
current_time = jobs[i].arrival_time;
}
float start_time = current_time;
float end_time = start_time + jobs[i].completion_time;
float turnaround_time = end_time - jobs[i].arrival_time;
float turnaround_ratio = turnaround_time / jobs[i].completion_time;
printf("\t %s \t%.2f \t%.2f \t%.2f \t%.2f\n",
jobs[i].name, start_time, end_time, turnaround_time, turnaround_ratio);
current_time = end_time;
}
printf("-----------------------------\n");
}
void shortestJobFirst(struct Job jobs[], int n) {
float current_time = 0;
int* completed = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
completed[i] = 0;
}
printf("短作业优先\t开始时间\t结束时间\t周转时间\t周转系数\t\n");
while (1) {
int shortest_job = -1;
float shortest_time = -1;
for (int i = 0; i < n; i++) {
if (jobs[i].arrival_time <= current_time && !completed[i] && (shortest_job == -1 || jobs[i].completion_time < shortest_time)) {
shortest_job = i;
shortest_time = jobs[i].completion_time;
}
}
if (shortest_job != -1) {
int i = shortest_job;
completed[i] = 1;
float start_time = current_time;
float end_time = start_time + jobs[i].completion_time;
float turnaround_time = end_time - jobs[i].arrival_time;
float turnaround_ratio = turnaround_time / jobs[i].completion_time;
printf("\t %s \t%.2f \t%.2f \t%.2f \t%.2f\n",
jobs[i].name, start_time, end_time, turnaround_time, turnaround_ratio);
current_time = end_time;
}
else {
int all_completed = 1;
for (int i = 0; i < n; i++) {
if (!completed[i]) {
all_completed = 0;
break;
}
}
if (all_completed) {
break;
}
else {
current_time++;
}
}
}
printf("-----------------------------\n");
}
void highestResponseRatioNext(struct Job jobs[], int n) {
float current_time = 0;
int* completed = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
completed[i] = 0;
}
printf("最高响应比\t开始时间\t结束时间\t周转时间\t周转系数\t\n");
while (1) {
int highest_response_job = -1;
float highest_response_ratio = -1;
for (int i = 0; i < n; i++) {
if (!completed[i] && jobs[i].arrival_time <= current_time) {
float waiting_time = current_time - jobs[i].arrival_time;
float response_ratio = (waiting_time + jobs[i].completion_time) / jobs[i].completion_time;
if (highest_response_job == -1 || response_ratio > highest_response_ratio) {
highest_response_job = i;
highest_response_ratio = response_ratio;
}
}
}
if (highest_response_job != -1) {
int i = highest_response_job;
completed[i] = 1;
float start_time = current_time;
float end_time = start_time + jobs[i].completion_time;
float turnaround_time = end_time - jobs[i].arrival_time;
float turnaround_ratio = turnaround_time / jobs[i].completion_time;
printf("\t %s \t%.2f \t%.2f \t%.2f \t%.2f\n",
jobs[i].name, start_time, end_time, turnaround_time, turnaround_ratio);
current_time = end_time;
}
else {
int all_completed = 1;
for (int i = 0; i < n; i++) {
if (!completed[i]) {
all_completed = 0;
break;
}
}
if (all_completed) {
break;
}
else {
current_time++;
}
}
}
free(completed);
printf("-----------------------------\n");
}
int main() {
int n;
printf("输入作业数量: ");
scanf_s("%d", &n);
struct Job* jobs = (struct Job*)malloc(n * sizeof(struct Job));
if (jobs == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return 1;
}
for (int i = 0; i < n; i++) {
printf("输入作业名, 抵达时间, 运行时间 %d: ", i + 1);
scanf_s("%s %f %f", jobs[i].name, (unsigned)_countof(jobs[i].name), &jobs[i].arrival_time, &jobs[i].completion_time);
}
int choice;
printf("-----------------------------\n");
while (1) {
printf("选择算法:\n");
printf("1. 先来先服务\n");
printf("2. 短作业优先\n");
printf("3.最高响应比\n");
printf("0. 重新输入\n");
printf("Enter 'end' to exit\n");
printf("-----------------------------\n");
scanf_s("%d", &choice);
switch (choice) {
case 1:
firstComeFirstServe(jobs, n);
break;
case 2:
shortestJobFirst(jobs, n);
break;
case 3:
highestResponseRatioNext(jobs, n);
break;
case 0:
main(); // Restart data input
break;
default:
free(jobs);
return 0; // Exit if invalid choice
}
}
free(jobs);
return 0;
}
3总结
作业管理是用户与操作系统的接口。作业调度的主要功能是检查系统是否能满足用户作业的资源要求以及按照一定的算法选取作业。