用c语言编写程序,采用时间片轮转、优先数、最短进程优 先和最短剩余时间优先四种调度算法。能够动态地随机生成新进程添加到就绪队列中。
时间: 2024-02-11 08:07:47 浏览: 96
好的,下面是一个简单的示例程序,实现了时间片轮转、优先数、最短进程优先和最短剩余时间优先四种调度算法,并能够动态地随机生成新进程添加到就绪队列中。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_PROCESS_NUM 100 // 最大进程数量
#define MAX_PRIORITY 10 // 最大优先级
#define MAX_CPU_TIME 10 // 最大CPU时间
// 进程结构体
typedef struct process {
int id; // 进程ID
int priority; // 优先级
int cpu_time; // 需要的CPU时间
int run_time; // 已经运行的CPU时间
int remain_time; // 剩余CPU时间
} Process;
// 就绪队列结构体
typedef struct ready_queue {
int front; // 队首
int rear; // 队尾
int process_num; // 进程数量
Process process[MAX_PROCESS_NUM]; // 进程数组
} ReadyQueue;
// 时间片轮转算法
void RR(ReadyQueue *queue, int time_slice) {
// 如果队列为空,直接返回
if (queue->process_num == 0) {
printf("队列为空,无法调度\n");
return;
}
printf("使用时间片轮转算法\n");
int i, j;
for (i = 0; i < queue->process_num; i++) {
Process *p = &(queue->process[i]);
for (j = 0; j < time_slice && p->remain_time > 0; j++) {
p->remain_time--;
p->run_time++;
printf("进程%d正在运行\n", p->id);
}
if (p->remain_time == 0) {
printf("进程%d已经完成\n", p->id);
for (; i < queue->process_num - 1; i++) {
queue->process[i] = queue->process[i + 1];
}
queue->process_num--;
i--;
}
}
}
// 优先数算法
void priority(ReadyQueue *queue) {
// 如果队列为空,直接返回
if (queue->process_num == 0) {
printf("队列为空,无法调度\n");
return;
}
printf("使用优先数算法\n");
int i, max_priority = 0, max_priority_index = 0;
for (i = 0; i < queue->process_num; i++) {
if (queue->process[i].priority > max_priority) {
max_priority = queue->process[i].priority;
max_priority_index = i;
}
}
Process *p = &(queue->process[max_priority_index]);
p->remain_time--;
p->run_time++;
printf("进程%d正在运行\n", p->id);
if (p->remain_time == 0) {
printf("进程%d已经完成\n", p->id);
for (i = max_priority_index; i < queue->process_num - 1; i++) {
queue->process[i] = queue->process[i + 1];
}
queue->process_num--;
}
}
// 最短进程优先算法
void SJF(ReadyQueue *queue) {
// 如果队列为空,直接返回
if (queue->process_num == 0) {
printf("队列为空,无法调度\n");
return;
}
printf("使用最短进程优先算法\n");
int i, min_cpu_time = MAX_CPU_TIME, min_cpu_time_index = 0;
for (i = 0; i < queue->process_num; i++) {
if (queue->process[i].cpu_time < min_cpu_time) {
min_cpu_time = queue->process[i].cpu_time;
min_cpu_time_index = i;
}
}
Process *p = &(queue->process[min_cpu_time_index]);
p->remain_time--;
p->run_time++;
printf("进程%d正在运行\n", p->id);
if (p->remain_time == 0) {
printf("进程%d已经完成\n", p->id);
for (i = min_cpu_time_index; i < queue->process_num - 1; i++) {
queue->process[i] = queue->process[i + 1];
}
queue->process_num--;
}
}
// 最短剩余时间优先算法
void SRTF(ReadyQueue *queue) {
// 如果队列为空,直接返回
if (queue->process_num == 0) {
printf("队列为空,无法调度\n");
return;
}
printf("使用最短剩余时间优先算法\n");
int i, min_remain_time = MAX_CPU_TIME, min_remain_time_index = 0;
for (i = 0; i < queue->process_num; i++) {
if (queue->process[i].remain_time < min_remain_time) {
min_remain_time = queue->process[i].remain_time;
min_remain_time_index = i;
}
}
Process *p = &(queue->process[min_remain_time_index]);
p->remain_time--;
p->run_time++;
printf("进程%d正在运行\n", p->id);
if (p->remain_time == 0) {
printf("进程%d已经完成\n", p->id);
for (i = min_remain_time_index; i < queue->process_num - 1; i++) {
queue->process[i] = queue->process[i + 1];
}
queue->process_num--;
}
}
// 随机生成新进程
void generate_new_process(ReadyQueue *queue) {
int priority = rand() % MAX_PRIORITY + 1;
int cpu_time = rand() % MAX_CPU_TIME + 1;
Process p = {queue->process_num + 1, priority, cpu_time, 0, cpu_time};
queue->process[queue->process_num++] = p;
printf("新进程%d已经生成,优先级为%d,需要的CPU时间为%d\n", p.id, p.priority, p.cpu_time);
}
int main() {
srand(time(NULL));
ReadyQueue queue = {0, 0, 0, {0}};
int i;
for (i = 0; i < 5; i++) {
generate_new_process(&queue);
}
for (i = 0; i < 3; i++) {
RR(&queue, 2);
}
generate_new_process(&queue);
SJF(&queue);
for (i = 0; i < 3; i++) {
SRTF(&queue);
}
generate_new_process(&queue);
priority(&queue);
return 0;
}
```
以上是一个简单的示例程序,实现了时间片轮转、优先数、最短进程优先和最短剩余时间优先四种调度算法,并能够动态地随机生成新进程添加到就绪队列中。需要注意的是,这只是一个简单的示例,并没有考虑很多实际情况,具体实现时需要根据实际情况进行调整和优化。
阅读全文