用C语言编写一个包含scan算法、Cscan算法、look调度算法、Clook调度算法的程序
时间: 2023-12-24 19:04:16 浏览: 105
C语言编写的算法程序
好的,下面是一个包含scan算法、Cscan算法、look调度算法、Clook调度算法的C语言程序的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
void scan(int queue[], int head, int size){
int distance = 0;
int current = head;
for(int i = 0; i < size; i++){
if(queue[i] >= current){
distance += queue[i] - current;
current = queue[i];
}
}
printf("SCAN Algorithm:\nTotal Head Movement = %d\n", distance);
}
void cscan(int queue[], int head, int size){
int distance = 0;
int current = head;
int max = MAX - 1;
for(int i = 0; i < size; i++){
if(queue[i] >= current){
distance += queue[i] - current;
current = queue[i];
}
}
distance += max - current;
current = 0;
for(int i = 0; i < size; i++){
if(queue[i] >= current){
distance += queue[i] - current;
current = queue[i];
}
}
printf("CSCAN Algorithm:\nTotal Head Movement = %d\n", distance);
}
void look(int queue[], int head, int size){
int distance = 0;
int current = head;
int max = MAX - 1;
for(int i = 0; i < size; i++){
if(queue[i] >= current){
distance += queue[i] - current;
current = queue[i];
}
}
for(int i = size - 1; i >= 0; i--){
if(queue[i] < current){
distance += current - queue[i];
current = queue[i];
}
}
printf("LOOK Algorithm:\nTotal Head Movement = %d\n", distance);
}
void clook(int queue[], int head, int size){
int distance = 0;
int current = head;
for(int i = 0; i < size; i++){
if(queue[i] >= current){
distance += queue[i] - current;
current = queue[i];
}
}
for(int i = size - 1; i >= 0; i--){
if(queue[i] < current){
distance += current - queue[i];
current = queue[i];
}
}
printf("CLOOK Algorithm:\nTotal Head Movement = %d\n", distance);
}
int main(){
int queue[] = {98, 183, 37, 122, 14, 124, 65, 67};
int size = sizeof(queue) / sizeof(queue[0]);
int head = 53;
scan(queue, head, size);
cscan(queue, head, size);
look(queue, head, size);
clook(queue, head, size);
return 0;
}
```
这个程序中,我定义了四个函数:`scan()`、`cscan()`、`look()`、`clook()`,分别对应SCAN算法、CSCAN算法、LOOK算法和CLOOK算法。这些函数接收一个整数数组表示磁盘访问队列、一个整数表示磁头的起始位置、一个整数表示队列的长度。函数中实现了对应算法,并打印出总的磁头移动距离。
在`main()`函数中,我定义了一个整数数组表示磁盘访问队列,一个整数表示磁头的起始位置,然后分别调用四个函数计算总的磁头移动距离,并打印出来。
这只是一个简单的示例程序,你可以根据自己的需求进行修改和扩展。
阅读全文