处理机调度算法!

#include <stdio.h>

#include <stdlib.h>

typedef struct{

       char name[5];

       int need_time;

       int privilege;

       char state;

}NODE;

 

typedef struct node{

       NODE data;

       struct node *link;

}LNODE;

 

void delay(int i)

{

       int x,y;

       while (i--)

       {

              x=0;

              while(x<10000)

              {

                     y=0;

                     while(y<40000)y++;

                     x++;

              }

       }

}

 

void len_queue(LNODE**hpt,NODE x)

{

       LNODE *q,*r,*p;

       q=*hpt;

       r=*hpt;

       p=(LNODE*)malloc(sizeof(LNODE));

       p->data=x;

       p->link=NULL;

       if(*hpt==NULL)*hpt=p;

       else

       {

              while(q!=NULL&&(p->data).privilege<(q->data).privilege)

              {

                     r=q;

                     q=q->link;

              }

              if(q==NULL)

                     r->link=p;

              else if(r==q)

              {

                     p->link=*hpt;

                     *hpt=p;

              }else

              {

                     r->link=p;

                     p->link=q;

              }

       }

}

 

void lde_queue(LNODE **hpt,NODE *cp)

{

       LNODE *p=*hpt;

       *cp=(*hpt)->data;

       *hpt=(*hpt)->link;

       free(p);

       printf("the elected process's name: %/n",cp->name);

}

 

void output(LNODE **hpt)

{

       LNODE *p=*hpt;

       printf("Name/t Need_time/t privilege/t state/n");

       do

       {

              printf("%s/t %d/t/t %d/t/t %c/n",

                     (p->data).name,(p->data).need_time,(p->data).privilege,(p->data).state);

              p=p->link;

       }while(p!=NULL);

       delay(4);

}

 

int main()

{

       LNODE *head=NULL;

       NODE curr,temp;

       printf("The period time is 4s/n");

       printf("please input/n");

       printf("if need_time=0,input over/n");

       printf("Name/t Need_time/t priviledge/n");

       while(1)

       {

              scanf("%s %d%d",temp.name,&temp.need_time,&temp.privilege);

              if(temp.need_time==0)break;

              temp.state='R';

              len_queue(&head,temp);

       }

      

       while(head!=NULL)

       {

              output(&head);

              lde_queue(&head,&curr);

              curr.need_time--;

              curr.privilege--;

              if(curr.need_time!=0)len_queue(&head,curr);

       }

       return 0;

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值