高响应比作业调度代码c语言
时间: 2023-08-01 10:09:00 浏览: 84
以下是一个简单的高响应比作业调度的代码示例,使用 C 语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_PROCESSES 10
struct Process {
int pid;
int arrival_time;
int burst_time;
int priority;
int waiting_time;
int turnaround_time;
int response_time;
int remaining_time;
};
int main() {
int n, i, j, current_time = 0, completed = 0;
float total_waiting_time = 0.0, total_turnaround_time = 0.0, total_response_time = 0.0;
// Prompt user to input number of processes
printf("Enter the number of processes: ");
scanf("%d", &n);
// Declare an array to store the processes
struct Process processes[MAX_PROCESSES];
// Prompt user to input process details
for (i = 0; i < n; i++) {
printf("\nEnter details for process %d:\n", i + 1);
processes[i].pid = i + 1;
printf("Arrival time: ");
scanf("%d", &processes[i].arrival_time);
printf("Burst time: ");
scanf("%d", &processes[i].burst_time);
printf("Priority: ");
scanf("%d", &processes[i].priority);
processes[i].remaining_time = processes[i].burst_time;
}
// Sort the processes based on arrival time
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (processes[j].arrival_time > processes[j+1].arrival_time) {
struct Process temp = processes[j];
processes[j] = processes[j+1];
processes[j+1] = temp;
}
}
}
// Loop until all processes are completed
while (completed < n) {
int highest_priority = -1, selected_process = -1;
for (i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time && processes[i].remaining_time > 0) {
float response_ratio = (float)(current_time - processes[i].arrival_time + processes[i].burst_time) / processes[i].burst_time;
if (processes[i].priority > highest_priority || (processes[i].priority == highest_priority && response_ratio > (float)(current_time - processes[selected_process].arrival_time + processes[selected_process].burst_time) / processes[selected_process].burst_time)) {
highest_priority = processes[i].priority;
selected_process = i;
}
}
}
if (selected_process == -1) {
current_time++;
continue;
}
processes[selected_process].remaining_time--;
if (processes[selected_process].remaining_time == 0) {
completed++;
processes[selected_process].turnaround_time = current_time - processes[selected_process].arrival_time + 1;
processes[selected_process].waiting_time = processes[selected_process].turnaround_time - processes[selected_process].burst_time;
processes[selected_process].response_time = current_time - processes[selected_process].arrival_time - processes[selected_process].burst_time + 1;
total_waiting_time += processes[selected_process].waiting_time;
total_turnaround_time += processes[selected_process].turnaround_time;
total_response_time += processes[selected_process].response_time;
}
current_time++;
}
// Calculate average waiting time, turnaround time, and response time
float avg_waiting_time = total_waiting_time / n;
float avg_turnaround_time = total_turnaround_time / n;
float avg_response_time = total_response_time / n;
// Print the results
printf("\nPID\tArrival Time\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\tResponse Time\n");
for (i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t\t%d\n",
processes[i].pid,
processes[i].arrival_time,
processes[i].burst_time,
processes[i].priority,
processes[i].waiting_time,
processes[i].turnaround_time,
processes[i].response_time);
}
printf("\nAverage Waiting Time: %.2f\n", avg_waiting_time);
printf("Average Turnaround Time: %.2f\n", avg_turnaround_time);
printf("Average Response Time: %.2f\n", avg_response_time);
return 0;
}
```
上述代码实现了一个简单的高响应比作业调度算法,根据进程的到达时间、剩余执行时间和优先级来决定下一个要执行的进程。在完成每个进程时,计算其等待时间、周转时间和响应时间,并计算所有进程的平均等待时间、平均周转时间和平均响应时间。
阅读全文