c语言代码实现完成单处理器系统中的进程调度,要求实现时间片轮转、优先数、最短进程优 先和最短剩余时间优先四种调度算法。实验具体包括:首先确定进程控制块的内容,进程控 制块的组成方式;然后完成进程创建原语和进程调度原语;最后编写主函数对所作工作进行 测试。 模拟程序只对你所设置的“虚拟 PCB”进行相应的调度模拟操作,即每发生“调度” 时,要求输出当前运行进程的“进程标识符”、“优先数”、“剩余运行时间”等,而不需要对系 统中真正的 PCB 等数据进行修改。要求能够动态地随机生成新进程添加到就绪队列中。
时间: 2023-12-26 14:03:45 浏览: 86
下面是一个简单的单处理器系统中进程调度的 C 语言代码实现,包括时间片轮转、优先数、最短进程优先和最短剩余时间优先四种调度算法。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 进程状态
#define READY 0
#define RUNNING 1
#define BLOCKED 2
#define TERMINATED 3
// 进程控制块结构体
typedef struct pcb {
int pid; // 进程标识符
int priority; // 优先数
int state; // 进程状态
int cpu_burst_time; // CPU 执行时间
int time_left; // 剩余执行时间
struct pcb *next; // 指向下一个 PCB 的指针
} PCB;
// 进程队列结构体
typedef struct queue {
PCB *head; // 队列头指针
PCB *tail; // 队列尾指针
} Queue;
// 将 PCB 添加到队列尾部
void enqueue(Queue *queue, PCB *pcb) {
pcb->next = NULL;
if (queue->tail == NULL) {
queue->head = pcb;
queue->tail = pcb;
} else {
queue->tail->next = pcb;
queue->tail = pcb;
}
}
// 从队列头部取出 PCB
PCB *dequeue(Queue *queue) {
if (queue->head == NULL) {
return NULL;
}
PCB *pcb = queue->head;
queue->head = queue->head->next;
if (queue->head == NULL) {
queue->tail = NULL;
}
pcb->next = NULL;
return pcb;
}
// 创建 PCB
PCB *create_pcb(int pid, int cpu_burst_time, int priority) {
PCB *pcb = (PCB *)malloc(sizeof(PCB));
pcb->pid = pid;
pcb->priority = priority;
pcb->state = READY;
pcb->cpu_burst_time = cpu_burst_time;
pcb->time_left = cpu_burst_time;
pcb->next = NULL;
return pcb;
}
// 销毁 PCB
void destroy_pcb(PCB *pcb) {
free(pcb);
}
// 初始化队列
void init_queue(Queue *queue) {
queue->head = NULL;
queue->tail = NULL;
}
// 打印队列中的 PCB 信息
void print_queue(Queue *queue) {
printf("[");
PCB *pcb = queue->head;
while (pcb != NULL) {
printf("(pid:%d,priority:%d,time-left:%d)", pcb->pid, pcb->priority, pcb->time_left);
pcb = pcb->next;
}
printf("]\n");
}
// 时间片轮转调度算法
void rr(Queue *ready_queue, int time_slice) {
printf("Time slice: %d\n", time_slice);
PCB *current_process = dequeue(ready_queue);
if (current_process == NULL) {
printf("No process is running.\n");
return;
}
current_process->state = RUNNING;
printf("Running process: (pid:%d,priority:%d,time-left:%d)\n", current_process->pid, current_process->priority, current_process->time_left);
if (current_process->time_left > time_slice) {
current_process->time_left -= time_slice;
current_process->state = READY;
enqueue(ready_queue, current_process);
} else {
current_process->time_left = 0;
current_process->state = TERMINATED;
destroy_pcb(current_process);
}
}
// 优先数调度算法
void priority(Queue *ready_queue) {
PCB *current_process = dequeue(ready_queue);
if (current_process == NULL) {
printf("No process is running.\n");
return;
}
current_process->state = RUNNING;
printf("Running process: (pid:%d,priority:%d,time-left:%d)\n", current_process->pid, current_process->priority, current_process->time_left);
if (current_process->time_left > 0) {
current_process->priority--;
if (current_process->priority < 0) {
current_process->priority = 0;
}
current_process->state = READY;
enqueue(ready_queue, current_process);
} else {
current_process->state = TERMINATED;
destroy_pcb(current_process);
}
}
// 最短进程优先调度算法
void sjf(Queue *ready_queue) {
PCB *current_process = dequeue(ready_queue);
if (current_process == NULL) {
printf("No process is running.\n");
return;
}
PCB *pcb = ready_queue->head;
while (pcb != NULL) {
if (pcb->cpu_burst_time < current_process->cpu_burst_time) {
PCB *tmp = current_process;
current_process = pcb;
pcb = tmp;
}
pcb = pcb->next;
}
current_process->state = RUNNING;
printf("Running process: (pid:%d,priority:%d,time-left:%d)\n", current_process->pid, current_process->priority, current_process->time_left);
if (current_process->time_left > 0) {
current_process->cpu_burst_time--;
current_process->state = READY;
enqueue(ready_queue, current_process);
} else {
current_process->state = TERMINATED;
destroy_pcb(current_process);
}
}
// 最短剩余时间优先调度算法
void srtf(Queue *ready_queue) {
PCB *current_process = dequeue(ready_queue);
if (current_process == NULL) {
printf("No process is running.\n");
return;
}
PCB *pcb = ready_queue->head;
while (pcb != NULL) {
if (pcb->time_left < current_process->time_left) {
PCB *tmp = current_process;
current_process = pcb;
pcb = tmp;
}
pcb = pcb->next;
}
current_process->state = RUNNING;
printf("Running process: (pid:%d,priority:%d,time-left:%d)\n", current_process->pid, current_process->priority, current_process->time_left);
if (current_process->time_left > 0) {
current_process->time_left--;
current_process->state = READY;
enqueue(ready_queue, current_process);
} else {
current_process->state = TERMINATED;
destroy_pcb(current_process);
}
}
int main() {
srand(time(NULL));
Queue ready_queue;
init_queue(&ready_queue);
int pid = 1;
int i;
for (i = 0; i < 5; i++) {
int cpu_burst_time = rand() % 10 + 1;
int priority = rand() % 5;
PCB *pcb = create_pcb(pid++, cpu_burst_time, priority);
enqueue(&ready_queue, pcb);
}
print_queue(&ready_queue);
rr(&ready_queue, 3);
print_queue(&ready_queue);
rr(&ready_queue, 3);
print_queue(&ready_queue);
priority(&ready_queue);
print_queue(&ready_queue);
sjf(&ready_queue);
print_queue(&ready_queue);
srtf(&ready_queue);
print_queue(&ready_queue);
return 0;
}
```
在上面的代码中,我们定义了进程控制块(PCB)结构体和进程队列(Queue)结构体,实现了 PCB 的创建和销毁、队列的初始化、PCB 的添加和取出等基本操作。然后,我们实现了时间片轮转、优先数、最短进程优先和最短剩余时间优先四种调度算法,以及主函数用于测试进程调度。在测试过程中,我们动态地随机生成了 5 个进程添加到就绪队列中,并依次调用了每种调度算法进行模拟操作,输出了每次调度后的队列中的 PCB 信息。
需要注意的是,这里的实现仅仅是一个简单的例子,实际的单处理器系统中还需要考虑更多的因素,比如进程同步和互斥、进程阻塞和唤醒等问题,同时还需要考虑系统资源的分配和管理等问题。因此,在实际应用中,需要根据具体情况进行进一步的优化和改进。
阅读全文