操作系统:时间片轮转(RR)调度算法C语言实现

时间片轮转(RR)调度算法

时间片轮转(RR)调度算法是专门为分时系统设计的。它类似于FCFS调度,但是增加了抢占以切换进程。该算法中,将一个较小时间单元定义为时间量或时间片,时间片的大小通常为 10~100ms。就绪队列作为循环队列,CPU 调度程序循环整个就绪队列,为每个进程分配不超过一个时间片的 CPU。为了实现 RR 调度,我们再次将就绪队列视为进程的 FIFO 队列。新进程添加到就绪队列的尾部。CPU调度程序从就绪队列中选择第一个进程,将定时器设置在一个时间片后中断,最后分派这个进程。
  流程图如下所示:
  在这里插入图片描述

#include "string.h"
#include "stdio.h"
#define NULL 0
typedef struct quen
{
    char pname[8];
    int time1;
    int time2;
    char state;
    struct quen *next;
}QUEN;

void main()
{
    QUEN *q, *p, *head, *m;
    char str[8],f;
    int t,d,n;
    printf("Enter the maxnumber of nodes (n): \ n");
    scanf ("%d", &n);
    d=n;
    if (d> 0)
    {
        printf ("enter the pname:");
        scanf ("%s", str);
        printf("enter the need time:");
        scanf("%d",&t);
        head=p=(QUEN*)malloc(sizeof(QUEN));
        strcpy(p->pname,str);
        p->time1=t;
        p->time2=0;
        p->state='R';
        p->next=NULL;
        head=p;
        getchar();
        --d;
    }
    while(d>0)
    {
        printf ("enter the pname:");
        scanf ("%s", str);
        printf ("enter need time:");
        scanf ("%d", & t);
        q= (QUEN *) malloc (sizeof (QUEN));
        strcpy (q-> pname, str); q-> time1=t;
        q-> time2=0;
        q->state='R';
        q->next=NULL;
        p->next=q;
        p=q;
        --d;
        p->next=head;
        q=head;
    }
    printf("process name need time runned static\n");
    do
    {
        printf("%s %d %d %c\n",q->pname,q->time1,q->time2,q->state);
        q=q->next;
    }while(q!=head);
    printf("\n");
    do
    {
        if(head->time2<head->time1)
        {
            head->time2++;
            if(head->time2==head->time1)
            {
                head->state='E';
                q=head;
                printf("The running process is %s\n",q->pname);
                printf("process name left time runned static \n");
                do
                {
                    printf ("%s %d %d %c \n", q-> pname, q-> time1, q-> time2, q-> state);
                    q=q-> next;
                }while (q!=head);
                printf("\n");
                head=head->next;
                q=head;
                p->next=head;
            }else
            {
                printf("The running process is %s\n",q->pname);
                printf("process name left time runned static\n");
                do
                {
                    printf("%s %d %d %c\n",q->pname,q->time1,q->time2,q->state);
                    q=q->next;
                }while(q!=head);
                printf("\n");
                head=head->next;
                q=head;
                p=p->next;
            }
            printf("Is it needing new process?(y or n) \n ");
            getchar();
            scanf ("%c" ,&f);
            if(f=='Y'||f=='y')
            {
                getchar();
                printf("Enter the new pname:");
                scanf("%s",str);
                printf("Enter the new neededtime:");
                scanf("%d",&t);  m=(QUEN *)malloc(sizeof(QUEN));
                strcpy(m->pname,str);
                m->time1=t;  m->time2=0;
                m->state='R';
                m->next=NULL;
                if(q->next->state=='E')
                {
                    p=m;
                    head=m;
                    p->next=head;
                    q=head;
                }
                else
                {
                    p->next=m;
                    m->next=head;
                    p=m;
                }
            }
        }
    }while(q->next->state!='E');
    printf("The processes are finished\n");
}

  • 1
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一段使用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; } ``` 这段代码实现了一个简单的时间片轮转调度算法,用于调度进程的执行顺序。它会根据每个进程的到达时间和执行时间进行调度,以最小化平均周转时间。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值