2007-04-17

时间片调度

原创作品,作者:飞阳
转载请注明出处http://hi.baidu.com/burtcn

今天很郁闷,本来快要完工得时候,发现中毒了。
这病毒特别厉害,把我的还原镜像也给删除了。
只好浪费了许多时间来重装和安装软件,所以今天晚上基本上没做什么。

下面的代码没有怎么测试。

#include <stdio.h>
#include <stdlib.h>
#define alloc(type) (type*)malloc(sizeof(type))
//进程结构体
struct process{     
      char name[10];
      int starttime;
      int runtime;
      int count;
      process *next;
} *waitlist,*runlist;
//函数声明
void init();
void insert();
void del(process *pro);
int getCount(int starttime);
void move(int num);
void run();
//定义时间片长度
int slicetime=3; 
//函数定义
//创建
void init(){     
      //初始化waitlist
      waitlist=alloc(process);
      waitlist->next=NULL;
      int num=0;
      printf("输入进程的数目:");
      scanf("%d",&num);
      while(num<=0){
           printf("输入进程的数目:");
           scanf("%d",&num);
      }
      printf("请输入%d个进程信息\n",num);
      printf("格式:进程名 开始时间 运行时间\n");
      for(int i=0;i<num;i++){          
           printf("%d:",i+1);
           insert();
      }
      printf("\n");
      //初始化runlist,循环链表
      runlist=alloc(process);
      runlist->next=runlist;     
}
//输入节点
void insert(){     
      process *pro=alloc(process);
      scanf("%s %d %d",pro->name,&(pro->starttime),&(pro->runtime));     
      pro->count=0;
      process *p=waitlist,*q;
      q=p->next;
      while(q!=NULL&&q->starttime<=pro->starttime){
           p=q;
           q=p->next;
      }
      p->next=pro;
      pro->next=q;
}
//删除进程节点
void del(process *pro){     
      process *p,*q;
      p=pro;
      q=p->next;
      p->next=q->next;
      q->next=NULL;
      free(q);
}
//获得节点数目
int getCount(int starttime){
      process *p;     
      p=waitlist;
      int i=0;     
      while(p->next!=NULL&&p->starttime<=starttime){          
           p=p->next;
           i++;
      }
      return i;
}
//把waitlist的部分节点移动到runlist
void move(int num){
      if(num<=0)      return;     
      //获得runlist中合适的位置
      process *pos;
      pos=runlist;
      while(pos->next!=runlist){
           pos=pos->next;
      }
      //要移动的最后一个节点
      process *start,*end;
      end=waitlist;
      start=waitlist->next;
      for(int i=0;i<num;i++)
           end=end->next;
      waitlist->next=end->next;
      pos->next=start;
      end->next=runlist;
}
//运行
void run(){     
      int time=0;
      //p当前进程的前一进程,q为当前进程
      process *p,*q;
      p=runlist;     
      while(waitlist->next!=NULL||runlist->next!=runlist){          
           move(getCount(time));
           q=p->next;
           if(p==runlist&&p->next==runlist)
                time++;
           else if(p!=runlist&&q==runlist){
                p=runlist;
           }else{               
                printf("进程名:%s\t",q->name);               
                printf("到达时间:%d\t",q->starttime);               
                printf("启动时间:%d\t",time);
                printf("运行次数:%d\t",++(q->count));
                if(q->runtime<=slicetime){                         
                     printf("运行时间:%d\t",time);
                     time+=q->runtime;
                     printf("结束时间:%d\n",time);                    
                     del(p);
                }else{
                     printf("运行时间:%d\n",time);                         
                     time+=slicetime;
                     q->runtime-=slicetime;
                     p=q;     
                }
           }
      }
}
void main(){     
      printf("开始运行...\n");
      init();
      printf("显示结果...\n");
      run();
      printf("结束运行...\n");
}
评论
发表评论

您还没有登录,请登录后发表评论

burt
搜索本博客
博客分类
最近加入圈子
最新评论
评论排行榜