首页 > 用户发贴区 > 编程问题提问区 > 时间片轮转法
2008
05-14

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef struct _proc/*定义结构体,进程控制块*/
{
  char name[32];
  int run_time;
  int req_time;
  enum {ready,end}state;
  struct _proc *next;
}proc;
int time=0;
proc *head=NULL,*p1=NULL,*p2=NULL;/*定义三个结构体指针指向空*/
FILE *fp;


void Init()/*建立循环链表*/
{
proc *p;
head=(proc*)malloc(sizeof(proc));
head->next=NULL;
p=(proc*)malloc(sizeof(proc));
  strcpy(p->name, “Q1″);
  p->run_time=0;
  p->req_time = 3;
  p->state =ready;
  p->next=NULL;
  head->next=p;


p=(proc*)malloc(sizeof(proc));
strcpy(p->name, “Q2″);
  p->run_time=0;
  p->req_time=2;
  p->state=ready;
  p->next=NULL;
  head->next->next=p;


p=(proc*)malloc(sizeof(proc));
strcpy(p->name, “Q3″);
  p->run_time = 0;
  p->req_time = 1;
  p->state=ready;
  p->next=NULL;
  head->next->next->next=p;


p=(proc*)malloc(sizeof(proc));
strcpy(p->name, “Q4″);
  p->run_time = 0;
  p->req_time = 2;
  p->state=ready;
  p->next=NULL;
  head->next->next->next->next=p;


p=(proc*)malloc(sizeof(proc));
strcpy(p->name, “Q5″);
  p->run_time = 0;
  p->req_time = 3;
  p->state=ready;
  p->next=head;
  head->next->next->next->next->next=p;


}
void PrintInfo()/*打印PCB*/
{
 struct _proc *t=head->next;


 fprintf(fp,”Time : %d\t Process Information\n”, time);
 fprintf(fp,”name  run_time  req_time  state\n”);
 fprintf(fp,”————————————\n”);


 do
 {
  fprintf(fp,”%4s  %8d  %8d  %5s\n”, t->name, t->run_time, t->req_time, t->state == ready? “ready” : “end”);
  t=t->next;
 }while(t->next!=head->next);


 fprintf(fp,”Current process is %s\n”, p1->name);
 fprintf(fp,”-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n\n”);
}


 void run()/*执行函数进行时间片轮转法*/
{
  p2=head;/*使p1,p2分别指向head,head->next*/
  p1=head->next;


  PrintInfo();


do
 {
   time++;
   p1->run_time++;
   p1->req_time–;



   if(p1->req_time==0)
   {
    p1->state=end;
    PrintInfo();
    p2->next=p1->next;/*删除需要时间为零的节点*/
    p1=p1->next;
   }
   else
   {
   PrintInfo();
    p1=p1->next;
    p2=p2->next;
   }
   if(p1==p2)
   break;
}while(p1->state!=end);


}
void main()/*主函数,调用其它函数*/
{
fp=fopen(“d:os.out”,”w”);
Init();
run();
fclose(fp);


}





时间片轮转法》有 2 条评论

  1. 影之神001 说:

    顶一下啊

     

留下一个回复