现在的位置: 首页 > 综合 > 正文

进程调度算法实现(自己写的)

2013年10月11日 ⁄ 综合 ⁄ 共 3563字 ⁄ 字号 评论关闭

 //////////////////////////////////////////////////////////////////////////////////
 //数据:进程,队列结构
 //处理流程:
 //1 初始化--进程队列结构(包括:就绪队列,等待队列,运行队列)等必要的数据结构 init();
 //2 进入无限循环,反复调度队列
 /////////////////////////////////////////////////////////////////////////
 
 #define MAX 5
 #include<stdio.h>
 #include<stdlib.h>
 
 int total_time=20;
 int time_slice=3;
 
 typedef struct process {    // 进程控制块
     char pname[10];
     int WaitTime;
     int BurstTime;
     int priority;    // 数字越小优先级越高
     struct process *next;
     }PROCESS;
 
   //typedef struct process PROCESS;

 PROCESS * in_queue(PROCESS *head,PROCESS *p);  //声明
  
   PROCESS *init()   //进程初始化
 {
  int i=0;
     char a;
  PROCESS *head_new;  //队列的队头
  head_new=(struct process*)malloc(sizeof(struct process));
  if(!head_new)  exit(1);
        head_new=NULL;

  
  do
  {
   struct process *s;
     printf("initialize the process:/n");
     s=(struct process *)malloc(sizeof(struct process));
     if(!s)  exit(1);
     printf("please input the pname:WaitTime: BurstTime:  priority:/n");
     scanf("%c",&(s->pname));
     scanf("%d",&(s->WaitTime));
     scanf("%d",&(s->BurstTime));
     scanf("%d",&(s->priority));
     s->next=NULL;
     in_queue(head_new,s);
     i++;
  
     printf("do u want to insert process more ??  'Y'or'N'/n");
  printf("----------------------------------------'/n");

     scanf("%c",&a);
     scanf("%c",&a);
    // if(a=='Y'||a=='y') continue;
    // else if(a=='N'||a=='n') break;
  }while((i<MAX) &&(a=='Y'||a=='y'));
  
  return  head_new;
 }
 
///////////////////////////////////////////////////////////
 
 PROCESS *in_queue(PROCESS *head,PROCESS *p)   //入队函数
 {
  if(head==NULL)
  {
   head=p;
   p->next=NULL;
  }
  else
  {
   p->next=head;
   head=p;
  }
 // printf("the process insert into the mothball queue :/n");
  return  head;
  
 }
 
/////////////////////////////////////////////////////////////
 /*
 void new_queue()  //后备队列  先来先服务方式进入就绪
 {
  
  return *head_new;
 }
    */
   
 PROCESS *FCFS_process()
 {
  PROCESS *p,*q,*a;  //a用来记录选中结点的前一个结点
  q=p=init();  //这里是不是有个问题??
  while(p->next!=NULL)
  {
   a=p;
   if(p->WaitTime>=q->WaitTime)
   {
    q=p;
    p=p->next;
   }
  }

  q->WaitTime--;
  if(q->WaitTime==0)   //如果等待时间为0则把该进程从后备队列中移除
  {
   a->next=p->next;
   free(p);
  }
  return q;   //选择等待时间最久的)
 }
 
 
//////////////////////就绪队列,入口函数为就绪队列的头指针/////////////
 
 
 int count=0;
 PROCESS *ready_queue(PROCESS *head)   //就绪队列  优先级进入运行 4道
 { 
  PROCESS *p;
  while(count<4)
  {
   p=FCFS_process();
   p->next=head->next;
   head=p;
   count++;
  
  printf("the process has inserted into the ready queue :/n");
  }
  return head;
 }
 
 
 //insert_ready()  //
 PROCESS *high_priority(PROCESS *P)  //选择优先级最高的进程
 {
  PROCESS *q,*p;               //问题,入口形参中
  q=p;
  
    while(p->next!=NULL)
   {  
     if(q->priority>p->priority)
    q=p;
    p=p->next;
   }
   return q;
    }
  
  
 PROCESS *pick_ready(PROCESS *a)   //从就绪队列中选择进程运行
 {
  PROCESS *p=ready_queue(a);
  PROCESS *b=high_priority(p);
   return b;
 }
 
 void run(PROCESS *a)   //运行一个时间片
 {
  while((time_slice>0)&&(a->BurstTime>0))   //指针用->  变量用.
  {
   printf("the process %c is runing",a->pname);
   printf("the process BurstTime time is %d ",a->WaitTime);
   printf("the process BurstTime time is %d ",a->BurstTime);
   printf("the process priority is %d ",a->priority);
   a->BurstTime--;
   time_slice--;
  } 
  a->priority--;
  total_time--;
  /*
  if(a->runtime>0)
  return a;
  else return NULL;*/
 }
 
   void main()
 {
  PROCESS *p;
  PROCESS *head=init();
  while(total_time!=0)
  {
   ready_queue(head);
   p=pick_ready(head);
   
   if(p->BurstTime==0)
   {
    p=p->next;
    free(p);
    count--;
      }
    run(p);
   time_slice=3;
   
  }
 }

   ////////////////////////////////////////////////////////////////////////
   //注意的点:
   //(1)pedef struct process process;  c++ 可以直接用process 定义,但是c 不可以
   //(2)返回值一致,PROCESS *MM(){PROCESS *P;return p}
   //指针用.  链表用-> 

【上篇】
【下篇】

抱歉!评论已关闭.