在linux环境下,使用c语言代码设计一个按先来先服务,算法时间片轮转法,优先数调度算法实现处理器调度的程序
时间: 2023-09-14 12:11:25 浏览: 111
以下是一个简单的处理器调度程序,实现了先来先服务、时间片轮转和优先数调度算法。
```c
#include <stdio.h>
#include <stdlib.h>
// 进程结构体
typedef struct process {
int pid; // 进程id
int arrival_time; // 到达时间
int burst_time; // 执行时间
int priority; // 优先级
int remaining_time; // 剩余执行时间
} process_t;
// 链表节点结构体
typedef struct node {
process_t *process; // 指向进程的指针
struct node *next; // 指向下一个节点的指针
} node_t;
// 队列结构体
typedef struct queue {
node_t *head; // 队头指针
node_t *tail; // 队尾指针
} queue_t;
// 初始化队列
void init_queue(queue_t *q) {
q->head = q->tail = NULL;
}
// 判断队列是否为空
int is_queue_empty(queue_t *q) {
return q->head == NULL;
}
// 入队
void enqueue(queue_t *q, process_t *process) {
node_t *node = malloc(sizeof(node_t));
node->process = process;
node->next = NULL;
if (is_queue_empty(q)) {
q->head = q->tail = node;
} else {
q->tail->next = node;
q->tail = node;
}
}
// 出队
process_t *dequeue(queue_t *q) {
if (is_queue_empty(q)) {
return NULL;
}
node_t *node = q->head;
process_t *process = node->process;
q->head = node->next;
if (q->head == NULL) {
q->tail = NULL;
}
free(node);
return process;
}
// 先来先服务调度算法
void fcfs(process_t *processes, int n) {
printf("先来先服务调度算法\n");
queue_t q;
init_queue(&q);
int time = 0;
float waiting_time = 0, turnaround_time = 0;
for (int i = 0; i < n; i++) {
// 进程到达前等待
if (time < processes[i].arrival_time) {
time = processes[i].arrival_time;
}
// 执行进程
time += processes[i].burst_time;
// 统计等待时间和周转时间
waiting_time += time - processes[i].arrival_time - processes[i].burst_time;
turnaround_time += time - processes[i].arrival_time;
}
printf("平均等待时间: %.2f\n", waiting_time / n);
printf("平均周转时间: %.2f\n", turnaround_time / n);
}
// 时间片轮转调度算法
void rr(process_t *processes, int n, int quantum) {
printf("时间片轮转调度算法 (时间片长度: %d)\n", quantum);
queue_t q;
init_queue(&q);
int time = 0;
float waiting_time = 0, turnaround_time = 0;
int *remaining_time = malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
remaining_time[i] = processes[i].burst_time;
}
int i = 0;
while (1) {
// 把到达时间小于等于当前时间的进程加入队列
while (i < n && processes[i].arrival_time <= time) {
enqueue(&q, &processes[i]);
i++;
}
// 如果队列为空,时间推进到下一个到达时间
if (is_queue_empty(&q)) {
if (i < n) {
time = processes[i].arrival_time;
} else {
break;
}
} else {
process_t *process = dequeue(&q);
// 执行进程
int execute_time = remaining_time[process->pid];
if (execute_time <= quantum) {
time += execute_time;
waiting_time += time - process->arrival_time - process->burst_time;
turnaround_time += time - process->arrival_time;
remaining_time[process->pid] = 0;
} else {
time += quantum;
remaining_time[process->pid] -= quantum;
enqueue(&q, process);
}
}
}
printf("平均等待时间: %.2f\n", waiting_time / n);
printf("平均周转时间: %.2f\n", turnaround_time / n);
free(remaining_time);
}
// 优先数调度算法
void psa(process_t *processes, int n) {
printf("优先数调度算法\n");
queue_t q;
init_queue(&q);
int time = 0;
float waiting_time = 0, turnaround_time = 0;
int *remaining_time = malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
remaining_time[i] = processes[i].burst_time;
}
int i = 0;
while (1) {
// 把到达时间小于等于当前时间的进程加入队列
while (i < n && processes[i].arrival_time <= time) {
enqueue(&q, &processes[i]);
i++;
}
// 如果队列为空,时间推进到下一个到达时间
if (is_queue_empty(&q)) {
if (i < n) {
time = processes[i].arrival_time;
} else {
break;
}
} else {
// 找到优先级最高的进程
node_t *node = q.head;
process_t *process = node->process;
while (node != NULL) {
if (node->process->priority < process->priority) {
process = node->process;
}
node = node->next;
}
dequeue(&q);
// 执行进程
int execute_time = remaining_time[process->pid];
time += execute_time;
waiting_time += time - process->arrival_time - process->burst_time;
turnaround_time += time - process->arrival_time;
remaining_time[process->pid] = 0;
}
}
printf("平均等待时间: %.2f\n", waiting_time / n);
printf("平均周转时间: %.2f\n", turnaround_time / n);
free(remaining_time);
}
int main() {
int n, quantum;
printf("请输入进程数: ");
scanf("%d", &n);
printf("请输入时间片长度: ");
scanf("%d", &quantum);
process_t *processes = malloc(n * sizeof(process_t));
printf("请输入每个进程的到达时间、执行时间和优先级:\n");
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &processes[i].arrival_time, &processes[i].burst_time, &processes[i].priority);
processes[i].pid = i;
processes[i].remaining_time = processes[i].burst_time;
}
fcfs(processes, n);
rr(processes, n, quantum);
psa(processes, n);
free(processes);
return 0;
}
```
在这个程序中,我们定义了三个调度算法函数:`fcfs()`、`rr()` 和 `psa()`,分别实现了先来先服务、时间片轮转和优先数调度算法。每个函数都接受一个 `processes` 数组和一个整数 `n`,表示进程数量,然后计算出平均等待时间和平均周转时间并输出。
在 `main()` 函数中,我们先从用户输入进程数和时间片长度,然后读入每个进程的到达时间、执行时间和优先级,最后依次调用三个调度算法函数。
阅读全文