⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 time-slice.cpp

📁 操作系统实验1 时间片轮转
💻 CPP
字号:
//time-slice.cpp
#include "stdio.h"
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0
struct pcb
{
  char name[10];
  char state;
  int slice;//时间片数
  int ntime;
  struct pcb* link;

}*p;
typedef struct pcb PCB;
typedef struct QueueLink{
  PCB* frout;//头
  PCB* rear;//尾
 }Queue;
Queue Q;

init() //建立空就绪队列
{
 Q.rear=getpch(PCB);
 Q.frout=Q.rear;
 Q.frout->link=NULL;
}

en() //插入就绪队列
{
 Q.rear->link=p;
 Q.rear=p;
}



input()//建立PCB
{
  int i,num,ts;
  printf("\n 请输入进程数:");
  scanf("%d", &num);
  printf("\n 请设置时间片大小:");
  scanf("%d", &ts);
  for(i=0;i<num;i++)
  {
   printf("\n 进程号NO.%d:\n",(i+1));
   p=getpch(PCB);
   printf("\n 输入进程名:");
   scanf("%s",p->name);
   printf("\n 输入进程运行时间:");
   scanf("%d",&p->ntime);
   printf("\n");
   p->slice=ceil(p->ntime/ts); //向上取整
   p->state='W';
   p->link=NULL;
   en();

}

disp(PCB* pr)
{
 printf("\n qname \t state \t time-slice \n");
 printf("%s \t ",pr->name);
 printf("%s \t ",pr->state);
 printf("%d \t ",pr->slice);
 printf("\n");

}

check()
{
 PCB* pr;
 printf("\n**** 当前正在运行的进程是:%s",p->name);
 disp(p);
 pr=Q.rear;
 print("****当前就绪队列状态为:\n",p->name);
 while(pr!=NULL);
 {
   disp(pr);
   pr=pr->link;
 }
}

run()
{
  p->Q.frout->link;
  p->slice--;
  if(p->slice==0)
  {
   Q.frout=p->link;
   printf("进程[%s]已完成...",p->name);
   free(p);
   }
   else
   {
    Q.frout=p->link;
    printf("\n 进程[%s]已经完成一个时间片,暂停执行并返回就绪队列末尾..",p->name);
    en();
   }
}


main()
{
 int len,h=0;
 char ch;
 input();
 len=1;
 while((len!=0)&&(Q.rear!=NULL))
 {
  printf("\n The execute number:%d\n", h);
  p=Q.rear;
  Q.rear=p->link;
  p->Link=NULL;
  p->state='R';
  check();
  run();
  printf("\n 按任意一键继续...");
  ch=getchar();
 }
 printf("\n\n 进程已经完成...\n");
 ch=getchar();
};




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -