RR时间片轮转法调度C语言实现

什么是RR?
RR=Round-Robin;
实现思想:
在FCFS的基础上,加入时间片的概念,从第一个到达的进程开始,CPU分配其一个时间片的长度,第一个进程放到其余任务后面,然后给第二个进程分配一个时间片,第二个进程放到其余任务后面,依次类推,直到所有进程完成。假如进程结束了,时间片没用完,则回收剩余时间片。

完整代码:

#include<stdio.h>
#include<stdlib.h>
#define circletime 5
typedef struct PCB {
   
	int id;//任务序号
	int arrivetime;//任务到达时间
	int runtime;//任务需要执行的时间
	int counttime;//记录任务处理一段时间后,剩下的需要处理的时间
	int aetime;//假定义当前到达时间
	struct PCB* next;
}*task, pcb;
pcb* creattask(int x, int y, int z) {
   
	task newtask = (task)malloc(sizeof(pcb));
	newtask->id = x;
	newtask->arrivetime = y;
	newtask->runtime = z;
	newtask->counttime = z;
	newtask->aetime = y;
	newtask->next = NULL;
	return newtask;
}
void deltask
  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一段使用C语言时间片轮转调度算法的伪代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义进程结构体 typedef struct Process { char name[10]; // 进程名 int arrival_time; // 到达时间 int burst_time; // 执行时间 int remaining_time; // 剩余执行时间 int turnaround_time;// 周转时间 int waiting_time; // 等待时间 int finish_time; // 完成时间 int started; // 标记是否已经开始运行 } Process; // 时间片轮转调度算法 void rr_scheduling(Process *processes, int n, int time_quantum) { int time = 0; // 当前时间 int i, j, k, flag, total_time; Process *current_process; // 当前正在运行的进程 Process *next_process; // 下一个要运行的进程 int *finished = (int *)malloc(n * sizeof(int)); // 标记进程是否已经完成 memset(finished, 0, n * sizeof(int)); total_time = 0; while (1) { flag = 0; // 标记是否有进程正在运行 for (i = 0; i < n; i++) { if (processes[i].arrival_time <= time && !finished[i]) { if (!processes[i].started) { processes[i].started = 1; processes[i].waiting_time = time - processes[i].arrival_time; } current_process = &processes[i]; flag = 1; break; } } if (!flag) { // 所有进程都已完成 break; } if (current_process->remaining_time <= time_quantum) { // 进程执行完毕 next_process = NULL; current_process->remaining_time = 0; current_process->finish_time = time + current_process->remaining_time; current_process->turnaround_time = current_process->finish_time - current_process->arrival_time; finished[i] = 1; total_time += current_process->turnaround_time; } else { // 进程还没有执行完毕 next_process = current_process; next_process->remaining_time -= time_quantum; } for (j = time; j < time + time_quantum; j++) { flag = 0; for (k = 0; k < n; k++) { if (processes[k].arrival_time <= j && !finished[k]) { flag = 1; break; } } if (!flag) { // 所有进程都已完成 break; } } time = j; if (next_process != NULL) { // 把进程放到队列的末尾 processes[n] = *next_process; n++; } } printf("Average Turnaround Time: %.2f\n", (float)total_time / n); free(finished); } int main() { int n, i, time_quantum; Process *processes; printf("Enter the number of processes: "); scanf("%d", &n); processes = (Process *)malloc(n * sizeof(Process)); for (i = 0; i < n; i++) { printf("Enter the name, arrival time and burst time of process %d: ", i + 1); scanf("%s %d %d", processes[i].name, &processes[i].arrival_time, &processes[i].burst_time); processes[i].remaining_time = processes[i].burst_time; processes[i].started = 0; } printf("Enter the time quantum: "); scanf("%d", &time_quantum); rr_scheduling(processes, n, time_quantum); free(processes); return 0; } ``` 这段代码实现了一个简单的时间片轮转调度算法,用于调度进程的执行顺序。它会根据每个进程的到达时间和执行时间进行调度,以最小化平均周转时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值