进程调度算法c语言实现
时间: 2023-10-16 13:06:36 浏览: 255
操作系统进程调度C++代码实现
进程调度算法的 C 语言实现需要涉及到操作系统的相关知识和编程技巧,这里只提供一个简单的示例代码,供参考:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PROCESS_NUM 10
// 进程状态枚举类型
enum process_state {
READY,
RUNNING,
BLOCKED,
FINISHED
};
// 进程结构体类型
struct process {
char name[20]; // 进程名
int arrive_time; // 到达时间
int burst_time; // 执行时间
int priority; // 优先级
int remaining_time; // 剩余执行时间
enum process_state state; // 进程状态
};
// 进程队列结构体类型
struct process_queue {
struct process *processes[MAX_PROCESS_NUM]; // 进程指针数组
int front; // 队首指针
int rear; // 队尾指针
int count; // 队列中进程数
};
// 初始化进程队列
void init_process_queue(struct process_queue *queue) {
queue->front = 0;
queue->rear = -1;
queue->count = 0;
}
// 入队
void enqueue(struct process_queue *queue, struct process *proc) {
if (queue->count < MAX_PROCESS_NUM) {
queue->rear = (queue->rear + 1) % MAX_PROCESS_NUM;
queue->processes[queue->rear] = proc;
queue->count++;
}
}
// 出队
struct process *dequeue(struct process_queue *queue) {
struct process *proc = NULL;
if (queue->count > 0) {
proc = queue->processes[queue->front];
queue->front = (queue->front + 1) % MAX_PROCESS_NUM;
queue->count--;
}
return proc;
}
// 创建一个新进程
struct process *create_process(char *name, int arrive_time, int burst_time, int priority) {
struct process *proc = (struct process *)malloc(sizeof(struct process));
strncpy(proc->name, name, sizeof(proc->name));
proc->arrive_time = arrive_time;
proc->burst_time = burst_time;
proc->priority = priority;
proc->remaining_time = burst_time;
proc->state = READY;
return proc;
}
// 先来先服务(FCFS)算法
void fcfs(struct process *procs[], int count) {
int i;
struct process_queue ready_queue;
struct process *current_proc = NULL;
init_process_queue(&ready_queue);
for (i = 0; i < count; i++) {
enqueue(&ready_queue, procs[i]);
}
printf("FCFS 调度算法执行结果:\n");
while (ready_queue.count > 0) {
current_proc = dequeue(&ready_queue);
printf("正在执行进程:%s\n", current_proc->name);
current_proc->state = RUNNING;
while (current_proc->remaining_time > 0) {
current_proc->remaining_time--;
}
current_proc->state = FINISHED;
printf("进程 %s 执行完毕\n", current_proc->name);
}
}
// 时间片轮转(RR)算法
void rr(struct process *procs[], int count, int time_slice) {
int i, time_count = 0;
struct process_queue ready_queue;
struct process *current_proc = NULL;
init_process_queue(&ready_queue);
for (i = 0; i < count; i++) {
enqueue(&ready_queue, procs[i]);
}
printf("RR 调度算法执行结果:\n");
while (ready_queue.count > 0) {
current_proc = dequeue(&ready_queue);
printf("正在执行进程:%s\n", current_proc->name);
current_proc->state = RUNNING;
for (time_count = 0; time_count < time_slice && current_proc->remaining_time > 0; time_count++) {
current_proc->remaining_time--;
}
if (current_proc->state == RUNNING) {
current_proc->state = (current_proc->remaining_time > 0) ? READY : FINISHED;
if (current_proc->state == READY) {
enqueue(&ready_queue, current_proc);
}
printf("进程 %s 执行完毕\n", current_proc->name);
}
}
}
int main() {
struct process *procs[MAX_PROCESS_NUM];
int count, i, time_slice;
char name[20];
int arrive_time, burst_time, priority;
printf("请输入进程数:");
scanf("%d", &count);
for (i = 0; i < count; i++) {
printf("请输入第 %d 个进程的信息:\n", i + 1);
printf("进程名:");
scanf("%s", name);
printf("到达时间:");
scanf("%d", &arrive_time);
printf("执行时间:");
scanf("%d", &burst_time);
printf("优先级:");
scanf("%d", &priority);
procs[i] = create_process(name, arrive_time, burst_time, priority);
}
printf("请输入时间片长度:");
scanf("%d", &time_slice);
fcfs(procs, count);
rr(procs, count, time_slice);
return 0;
}
```
以上代码实现了两种进程调度算法:先来先服务(FCFS)算法和时间片轮转(RR)算法。在主函数中,首先输入进程数和每个进程的基本信息,然后输入时间片长度,最后依次执行两种调度算法。在算法执行过程中,会输出当前正在执行的进程名和执行完毕的进程名。
阅读全文