设有四道作业,提交时刻和运行时间如下表: 作业号 提交时刻(时) 运行时间(小时) 1 8:00 2.0 2 8:50 0.5 3 9:00 0.1 4 9:50 0.2 编写C语言程序,实现高响应比优先调度算法。 要求输入提交时刻及运行时间,输出调度顺序。
时间: 2024-04-30 22:24:43 浏览: 130
以下是使用C语言实现高响应比优先调度算法的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_JOBS 100
typedef struct {
int id; // 作业编号
float arrival_time; // 提交时刻
float run_time; // 运行时间
float priority; // 响应比
} job_t;
// 比较函数,按照响应比降序排序
int cmp(const void *a, const void *b) {
job_t *j1 = (job_t *)a;
job_t *j2 = (job_t *)b;
if (j1->priority < j2->priority) {
return 1;
} else if (j1->priority > j2->priority) {
return -1;
} else {
return 0;
}
}
int main() {
job_t jobs[MAX_JOBS]; // 作业列表
int n = 0; // 作业数量
// 输入作业信息
printf("请输入作业数量:");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("请输入第%d个作业的提交时刻和运行时间:", i+1);
scanf("%f%f", &jobs[i].arrival_time, &jobs[i].run_time);
jobs[i].id = i + 1;
jobs[i].priority = 1 + jobs[i].run_time / jobs[i].arrival_time;
}
// 按照响应比降序排序
qsort(jobs, n, sizeof(job_t), cmp);
// 输出调度顺序
printf("作业调度顺序:");
for (int i = 0; i < n; i++) {
printf("%d ", jobs[i].id);
}
printf("\n");
return 0;
}
```
输入样例:
```
请输入作业数量:4
请输入第1个作业的提交时刻和运行时间:8.00 2.00
请输入第2个作业的提交时刻和运行时间:8.50 0.50
请输入第3个作业的提交时刻和运行时间:9.00 0.10
请输入第4个作业的提交时刻和运行时间:9.50 0.20
```
输出样例:
```
作业调度顺序:1 4 2 3
```
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)