c语言2. 编写先来先服务算法(FCFS)和扫描算法(SCAN)
时间: 2023-11-18 08:12:03 浏览: 70
先来先服务算法(FCFS)是一种最简单的调度算法,它按照作业到达的顺序进行调度,即先到先服务。
以下是一个简单的C语言实现:
```c
#include <stdio.h>
struct Process {
int pid;
int arrival_time;
int burst_time;
int waiting_time;
int turnaround_time;
};
void fcfs_scheduling(struct Process p[], int n) {
int i, j;
float avg_waiting_time = 0, avg_turnaround_time = 0;
p[0].waiting_time = 0;
p[0].turnaround_time = p[0].burst_time;
for (i = 1; i < n; i++) {
p[i].waiting_time = p[i-1].waiting_time + p[i-1].burst_time;
p[i].turnaround_time = p[i].waiting_time + p[i].burst_time;
}
printf("Process ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].arrival_time, p[i].burst_time, p[i].waiting_time, p[i].turnaround_time);
avg_waiting_time += p[i].waiting_time;
avg_turnaround_time += p[i].turnaround_time;
}
printf("Average Waiting Time: %.2f\n", avg_waiting_time/n);
printf("Average Turnaround Time: %.2f\n", avg_turnaround_time/n);
}
int main() {
int n, i;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process p[n];
for(i = 0; i < n; i++) {
printf("Process %d\n", i+1);
printf("Enter arrival time: ");
scanf("%d", &p[i].arrival_time);
printf("Enter burst time: ");
scanf("%d", &p[i].burst_time);
p[i].pid = i+1;
}
fcfs_scheduling(p, n);
return 0;
}
```
扫描算法(SCAN)是一种磁盘调度算法,它模拟磁头在磁盘上移动的过程。磁头按照一个方向移动,直到遇到最后一个请求,然后返回另一个方向,直到遇到最早的请求。
以下是一个简单的C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#define LOW 0
#define HIGH 199
void scan_scheduling(int arr[], int head, int size) {
int total_seek_time = 0, i, j, temp, pos, end = size-1, nearest_end = -1;
for (i = 0; i < size; i++) {
for (j = i+1; j < size; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for (i = 0; i < size; i++) {
if (arr[i] > head) {
pos = i;
nearest_end = pos - 1;
break;
}
}
if (nearest_end == -1) {
nearest_end = size - 1;
for (i = nearest_end; i >= 0; i--) {
printf("%d -> ", arr[i]);
total_seek_time += abs(head - arr[i]);
head = arr[i];
}
} else {
for (i = nearest_end; i >= 0; i--) {
printf("%d -> ", arr[i]);
total_seek_time += abs(head - arr[i]);
head = arr[i];
}
}
for (i = pos; i <= end; i++) {
printf("%d -> ", arr[i]);
total_seek_time += abs(head - arr[i]);
head = arr[i];
}
printf("\nTotal Seek Time: %d\n", total_seek_time);
}
int main() {
int n, i, head;
printf("Enter the number of requests: ");
scanf("%d", &n);
int arr[n];
printf("Enter the request queue:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the initial position of head: ");
scanf("%d", &head);
scan_scheduling(arr, head, n);
return 0;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.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)
![](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)
![](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)