设计一个按优先数调度算法实现处理器调度的程序。

  #include<stdio.h>
  #include<stdlib.h>
  #include<string.h>
 typedef struct PCB {  /*定义一个结构体数组,包括进程的信息*/
  char name[20] ;
  float servertime;
  float priority;
  int flags;
  struct  PCB *next ;
}PCB,Linklist;
struct PCB *list;
 insert(int s )
{   struct PCB *q,*b,*a;
    q=(struct PCB*)malloc(sizeof(PCB));
    puts("/ninput process name,servetime,priority/n");
    scanf("%s%f%f",q->name,&q->servertime,&q->priority);
 q->flags=0;
    if(list->next==NULL)
 {    q->next=list->next;
      list->next=q;
 }
    else
 { 
  if (q->priority>list->next->priority)
  {
   q->next=list->next;
   list->next=q;
  }
        else
  {  b=list->next;
           a=b->next;
           while(a->next!=NULL)
     {
      if(q->priority>a->priority&&q->priority<b->priority)
      {
    
             q->next=a;
       b->next=q;
      }
   
   
              else
     { 
      b=a;
         a=a->next; 
     }
    
     }
         q->next=a;
               b->next=q;    
   
  }
 }
 }
Create(int n) /*初始化*/
{ int i;
      for(i=n;i>0;--i)
   { 
    insert(i);
   }
running()
{  
 struct PCB *q,*b,*a;
    q=list->next;
    q->servertime-=1;
    q->priority-=1;
   if(q->servertime==0)
   {
    q->flags=1;
    printf("/nto the end/n");
    free(q);
   }
   else
   {
    if(list->next==NULL)
       {
        q->next=list->next;
        list->next=q;
    }
       else
    { 
    
     if ((q->priority)>(list->next->priority))
     {
          q->next=list->next;
          list->next=q;
     }
           else
     {  
     b=list->next;
              a=b->next;
              while(a->next!=NULL)
     {
      if((q->priority>a->priority)&&(q->priority<b->priority))
      {
               q->next=a;
         b->next=q;
      }
                  else
     {
        b=a;
        a=a->next;
      }
     }
               q->next=a;
               b->next=q;
    }
    }
   }
}
   
 
 
 void main()
 { int m ;
   struct PCB *list;
   list=(struct PCB*)malloc(sizeof(PCB));
   list->next=NULL;
   puts("/n input creat process number: ");
   scanf("%d",&m);
   Create(m);
   running();
   
 }
 
 

 
 
  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值