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

📄 2.txt

📁 这是操作系统原理中CPU进程调度的模拟算法
💻 TXT
字号:
#include<stdio.h>   
  #include<stdlib.h>   
  #include<time.h>   
  #define   null   0   
  FILE   *   f;   
  int   flag=1;   
  /*保存现场部分*/   
  typedef   struct   sce{   
                          int   midresult;   
                          int   times;   
                            }scen;   
    
  /*进程控制块*/   
  typedef   struct   pcb_type{   
                            char   name[20];   
                            int   pid;   
                            int   (*fp)();   
                            scen   scene;   
                            int   priority;   
                            int   status;/*4为未结束,5为结束*/   
                            struct   pcb_type   *next;   
                                }pcb;   
  /*按优先级入队列*/   
  pcb   *inqueue(pcb   *head,pcb   *apcb)   
  {   pcb   *p=head;   
      while(p->next!=null)   
      {   if(p->next->priority<apcb->priority)   
                break;   
              p=p->next;   
      }/*while*/   
      if(p->next==null)   
            p->next=apcb;   
      else   
              {   
                apcb->next=p->next;   
                p->next=apcb;   
              }   
      return   head;   
  }/*inqueue*/   
    
  /*出队列*/   
  pcb   *outqueue(pcb   *head)   
  {   
      pcb   *p=head->next;   
      head->next=p->next;   
      p->next=null;   
      return   p;   
  }/*outqueue*/   
    
  /*判断队列是否为空*/   
  int   checkqueue(pcb   *head)   
  {   
    if(head->next==null)   
      return   0;   
      else   return   1;   
  }   
    
  float   Random()   
  {   
      srand(time(0));   
      return   random(1000)/1000;   
  }   
    
  /*进程一*/   
  int   process1(int   *midresult,int   *times)   
  {   int   allTheTimes=3;   
      int   temp=*midresult;   
      int   n=0;   
      int   i=0;   
      for(;i<allTheTimes-(*times);i++)   
      {     temp++;   
            n++;   
        if(Random()>0.33)/*模拟时间片*/   
        {   
          *midresult=temp;   
          *times=*times+n;   
          return   4;   
        }   
      }   
      *midresult=temp;   
      return   5;   
  }   
    
  /*进程二*/   
  int   process2(int   *midresult,int   *times)   
  {   
      int   allTheTimes=4;   
      int   temp=*midresult;   
      int   n=0;   
      int   i=0;   
      for(;i<allTheTimes-(*times);i++)   
            {   
            temp++;   
            n++;   
                if(Random()<0.33||Random()>0.66)   
                      {   
                          *midresult=temp;   
                  *times=*times+n;   
                            return   4;   
                      }   
            }   
  *midresult=temp;   
  return   5;   
  }   
    
  /*进程三*/   
  int   process3(int   *midresult,int   *times)   
  {   
      int   allTheTimes=5;   
      int   temp=*midresult;   
      int   n=0;   
      int   i=0;   
      for(;i<allTheTimes-(*times);i++)   
            {   
                temp++;   
                n++;   
                if(Random()<0.66)   
                      {   
                          *midresult=temp;   
                  *times=*times+n;   
                          return   4;   
                      }   
              }   
    *midresult=temp;   
    return   5;   
  }   
    
  /*调度器*/   
  void   schedule(pcb   *head1,pcb   *head2)   
  {   pcb   *p,*r;   
      pcb   *temp_head;   
      /*检验就绪队列是否为空*/   
      if(!checkqueue(head1))   
          {       
                  /*空则交换两个优先级队列*/   
                  temp_head=head1;   
  head1=head2;   
  head2=temp_head;   
                  /*如果仍为空,则退出*/   
  if(!checkqueue(head1))   
    {   
      fprintf(f,"All   processes   are   complete!");   
      flag=0;   
      return;   
    }   
          }   
        /*以下为对队列的现状的显示*/   
        r=head1;   
        fprintf(f,"\nhead1     ");   
        while(r->next!=null)   
        {   r=r->next;   
            fprintf(f,"[%s]\t",r->name);   
            }   
        fprintf(f,"null\n");   
        r=head2;   
        fprintf(f,"head2     ");   
        while(r->next!=null)   
        {   
          r=r->next;   
          fprintf(f,"[%s]\t",r->name);   
        }   
        fprintf(f,"null\n");   
      /*以上为对队列的现状的显示*/   
    
      /*不为空,从高优先级队列队头取一个PCB*/   
      p=outqueue(head1);   
    
    /*检查进程的状态,恢复现场   */   
      if(p->status!=5)   
      {   
          fprintf(f,"%s",p->name);   
        fprintf(f,"   is   using   CPU\t");   
        p->status=p->fp(&(p->scene.midresult),&(p->scene.times));   
      }   
      /*进程结束,打印结果*/   
      if(p->status==5)   
      {   fprintf(f,"%s",p->name);   
          fprintf(f,"   is   complete,the   result   is   ");   
          fprintf(f,"%d\n",p->scene.midresult);   
          /*释放进程控制块*/   
          free(p);   
      /*结束本次调度*/   
      return;   
      }   
    /*进程未结束,入队列*/   
    fprintf(f,"\n%s",p->name);   
    fprintf(f,"   is   in   queue2");   
      inqueue(head2,p);   
    
    /*以下为对队列的现状的显示*/   
      r=head1;   
        fprintf(f,"\nhead1     ");   
        while(r->next!=null)   
        {   r=r->next;   
            fprintf(f,"[%s]\t",r->name);   
            }   
        fprintf(f,"null\n");   
        r=head2;   
        fprintf(f,"head2     ");   
        while(r->next!=null)   
        {   
          r=r->next;   
          fprintf(f,"[%s]\t",r->name);   
        }   
        fprintf(f,"null\n");   
  /*以上为对队列的现状的显示*/   
  }/*schedule*/   
    
  /*初始化进程控制块*/   
  void   init_process(pcb   *apcb)   
  {   
      apcb->scene.midresult=0;   
      apcb->scene.times=0;   
      /*Set   name*/   
      printf("Please   give   a   name   for   the   process:");   
      fflush(stdin);   
      gets(apcb->name);   
      /*Set   priority*/   
      printf("Please   give   ");   
      printf("%s",apcb->name);   
      printf("   a   priority:   ");   
      fflush(stdin);   
      scanf("%d",&apcb->priority);   
      /*Set   status*/   
      apcb->status=4;   
      apcb->next=null;   
  }/*init_process*/   
    
  /*主函数*/   
  void   main()   
  {   pcb   *r;   
    
      pcb   *head1=(pcb   *)malloc(sizeof(pcb));   
      pcb   *head2=(pcb   *)malloc(sizeof(pcb));   
      pcb   *p1=(pcb   *)malloc(sizeof(pcb));   
      pcb   *p2=(pcb   *)malloc(sizeof(pcb));   
      pcb   *p3=(pcb   *)malloc(sizeof(pcb));   
      f=fopen("a.txt","w");   
      head1->next=null;   
      head2->next=null;   
      p1->fp=process1;   
      p2->fp=process2;   
      p3->fp=process3;   
      init_process(p1);   
      init_process(p2);   
      init_process(p3);   
      head1=inqueue(head1,p1);   
      head1=inqueue(head1,p2);   
      head1=inqueue(head1,p3);   
      while(flag)   
            {   
        /*以下为对队列的现状的显示(在schedule函数外)*/   
        fprintf(f,"/*out   side*/");   
        r=head1;   
        fprintf(f,"\nhead1     ");   
        while(r->next!=null)   
        {   r=r->next;   
            fprintf(f,"[%s]\t",r->name);   
            }   
        fprintf(f,"null\n");   
        r=head2;   
        fprintf(f,"head2     ");   
        while(r->next!=null)   
        {   
          r=r->next;   
          fprintf(f,"[%s]\t",r->name);   
        }   
        fprintf(f,"null\n");   
        fprintf(f,"/*eof*/");   
  /*以上为对队列的现状的显示(在schedule函数外)*/   
    
                schedule(head1,head2);/*调度*/   
            }   
            fclose(f);   
            getch();   
  }/*main*/ 

⌨️ 快捷键说明

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