调度算法:时间轮转调度算法

操作系统的作业,模拟一个进程调度。

百度了好多个都不太好,自己根据百度的加以改进。

#include <stdio.h>
#include <stdlib.h> 
#include <string.h> 
#include <conio.h>
#include <ctype.h>
typedef struct node{
	char name[10];//进程名称
	int prio;//进程优先数
	int round;//进程时间轮转时间片
	int cputime;//进程占CPU时间
	int needtime;//进程到完成还要的时间
	int count;//计数器
	char state;//进程状态
	struct node *next; 
}PCB;
PCB *finish,*ready,*tail,*run;//队列指针 
int n;//进程数 
void firstin(){//将就绪队列中的第一个进程投入运行
	run=ready;//就绪队列头指针赋值给运行头指针 
	run->state = 'R';//进程状态变为运行态 
	ready=ready->next;//就绪队列头指针后移到下一个进程 
}
/*标题输出函数*/
void prt1(char a)
{
      printf("  name     cputime  needtime   count   round     state\n");
}
/*进程PCB输出*/
void prt2(char a,PCB *q)
{
      printf("  %-10s%-10d%-10d%-10d%-10d %-c\n",q->name,
       q->cputime,q->needtime,q->count,q->round,q->state);
}
/*输出函数*/
void prt(char algo)
{
   PCB *p;
   prt1(algo);  /*输出标题*/
   if(run!=NULL) /*如果运行指针不空*/
      prt2(algo,run); /*输出当前正在运行的PCB*/
   p=ready;  /*输出就绪队列PCB*/
   while(p!=NULL)
   {
      prt2(algo,p);
      p=p->next;
   }
   p=finish;  /*输出完成队列的PCB*/
   while(p!=NULL)
   {
      prt2(algo,p);
      p=p->next;
   }
   getch();  /*压任意键继续*/
}

/*轮转法插入函数*/
void insert2(PCB *p2)
{
   tail->next=p2;  /*将新的PCB插入在当前就绪队列的尾*/
   tail=p2;
   p2->next=NULL;
}
/*轮转法创建进程PCB*/
void create2(char alg)
{
   PCB *p;
   int i,time;
   char na[10];
   ready=NULL;
   finish=NULL;
   run=NULL;
   printf("Enter name and time of round process\n");
   for(i=1;i<=n;i++)
   {
      p=(node *)malloc(sizeof(PCB));
      scanf("%s",na);
      scanf("%d",&time);
      strcpy(p->name,na);
      p->cputime=0;
      p->needtime=time;
      p->count=0; /*计数器*/
      p->state='w';
      p->round=2;  /*时间片*/
      if(ready!=NULL)
     insert2(p);
      else
      {
     p->next=ready;
     ready=p;
     tail=p;
      }
   }
   system("cls");
   printf("              output of round\n");
   printf("************************************************\n");
   prt(alg);   /*输出进程PCB信息*/
   run=ready;  /*将就绪队列的第一个进程投入运行*/
   ready=ready->next;
   run->state='R';
}
/*时间片轮转法*/
void roundrun(char alg)
{
   while(run!=NULL)
   {
      run->cputime=run->cputime+1;
      run->needtime=run->needtime-1;
      run->count=run->count+1;
      if(run->needtime==0)/*运行完将其变为完成态,插入完成队列*/
      {
     run->next=finish;
     finish=run;
     run->state='F';
     run=NULL;
     if(ready!=NULL)
        firstin(); /*就绪对列不空,将第一个进程投入运行*/
      }
      else
     if(run->count==run->round)  /*如果时间片到*/
     {
        run->count=0;  /*计数器置0*/
        if(ready!=NULL) /*如就绪队列不空*/
        {
           run->state='W'; /*将进程插入到就绪队列中等待轮转*/
           insert2(run);
           firstin(); /*将就绪对列的第一个进程投入运行*/
        }
     }
      prt(alg); /*输出进程信息*/
   }
}
/*主函数*/
int main()
{
   char algo;  /*算法标记*/
   system("cls");

   printf("Enter process number\n");
   scanf("%d",&n); /*输入进程数*/
     create2(algo); /*轮转法*/
     roundrun(algo);
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的时间片轮转调度算法的 CPU 调度代码: ```c #include <stdio.h> // 进程结构体 typedef struct process { int pid; // 进程ID int burst_time; // 运行时间 int remaining_time; // 剩余时间 } process; int main() { int n, quantum; // n: 进程数,quantum: 时间片大小 printf("请输入进程数和时间片大小:"); scanf("%d%d", &n, &quantum); process p[n]; int waiting_time[n], turnaround_time[n]; // 输入进程信息 for (int i = 0; i < n; i++) { printf("请输入进程 %d 的信息(ID 运行时间):", i + 1); scanf("%d%d", &p[i].pid, &p[i].burst_time); p[i].remaining_time = p[i].burst_time; } // 时间片轮转算法 int time = 0, count = 0; // time: 当前时间,count: 已完成的进程数 while (count < n) { for (int i = 0; i < n; i++) { if (p[i].remaining_time > 0) { if (p[i].remaining_time <= quantum) { time += p[i].remaining_time; waiting_time[i] = time - p[i].burst_time; p[i].remaining_time = 0; count++; } else { time += quantum; p[i].remaining_time -= quantum; } } } } // 计算等待时间和周转时间 float total_waiting_time = 0, total_turnaround_time = 0; for (int i = 0; i < n; i++) { turnaround_time[i] = p[i].burst_time + waiting_time[i]; total_waiting_time += waiting_time[i]; total_turnaround_time += turnaround_time[i]; } // 输出结果 printf("进程ID\t运行时间\t等待时间\t周转时间\n"); for (int i = 0; i < n; i++) { printf("%d\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].burst_time, waiting_time[i], turnaround_time[i]); } printf("平均等待时间:%.2f\n", total_waiting_time / n); printf("平均周转时间:%.2f\n", total_turnaround_time / n); return 0; } ``` 代码中使用了一个 `process` 结构体来存储每个进程的信息,包括进程ID、运行时间和剩余时间。首先输入进程数和时间片大小,然后输入每个进程的信息。接下来使用时间片轮转算法进行 CPU 调度,计算每个进程的等待时间和周转时间。最后输出结果,包括每个进程的ID、运行时间、等待时间和周转时间,以及平均等待时间和平均周转时间

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值