处理器的调度算法队列实现

#include<stdio.h>

#include<stdlib.h>

#include<malloc.h>

#include<string.h>

#define N 9

int time=0;

int qq=0;

typedef struct Elem{

              char name[2];

              int needtime;

              int cputime;

              int priority;

              char status[7];

}Elem,*Elemptr;

 

typedef struct Qnode{

       Elemptr data;

       struct Qnode *next;

}Qnode,*QueuePtr;

 

typedef struct{

       QueuePtr front;

       QueuePtr rear;

}LinkQueue;

 

void InitQueue(LinkQueue &Q){

       Q.front=Q.rear=(QueuePtr)malloc(sizeof(Qnode));

       Q.rear->data=(Elemptr)malloc(sizeof(Elem));

       if(!Q.front)

              exit(1);

       Q.front->next=NULL;

}

void EnQueue(LinkQueue &Q, Elemptr e){

       QueuePtr p;

       p=(QueuePtr)malloc(sizeof(Qnode));

       p->data=(Elemptr)malloc(sizeof(Elem));//指针没用被赋值,一定要初始化,否则就成了野指针了

       if(!p)

              exit(1);

       strcpy((p->data)->name,e->name);

       (p->data)->needtime=e->needtime;

       (p->data)->cputime=e->cputime;

       (p->data)->priority=e->priority;

       strcpy((p->data)->status,e->status);

       p->next=NULL;

       Q.rear->next=p;

       Q.rear=p;

}

void sort_queue(LinkQueue &Q){

       QueuePtr p=Q.front->next;

       QueuePtr p1,p2;

       QueuePtr pre_p1,pre_p2,max;     

       for(p1=p;p1->next!=NULL;pre_p1=max,p1=max->next){

              max=p1;

              for(p2=p1;p2->next!=NULL;p2=p2->next){

                     if(p2->next->data->priority>max->data->priority){

                            pre_p2=p2;

                            max=p2->next;

                     }

              }

              if(max==p1){

                     continue;

              }

              //head or not head

              if(p1==Q.front->next){

                     Q.front->next=max;

              }

              else{

                     pre_p1->next=max;

              }

              //switch point

              QueuePtr temp=max->next;

              if(p1->next==max){

                     max->next=p1;

                     p1->next=temp;

              }

              else{

                     max->next=p1->next;

                     pre_p2->next=p1;

                     p1->next=temp;

              }

       }

}

void DeQueue(LinkQueue &Q){

       QueuePtr p;

       if(Q.front==Q.rear)

                     exit(1);

       p=Q.front->next;

       Q.front->next=p->next;

       if(Q.rear==p)

                     Q.rear=Q.front;

       free(p);

}

void Insertsort(LinkQueue &Q,int &time,Elem *array){

       time++;

       Q.front->next->data->priority--;

       Q.front->next->data->needtime--;

       Q.front->next->data->cputime++;

       if(Q.front->next->data->needtime==0){

              array[qq].cputime=Q.front->next->data->cputime;

              strcpy(array[qq].name,Q.front->next->data->name);

              array[qq].needtime=0;

              array[qq].priority=Q.front->next->data->priority;

              strcpy(array[qq].status,"finish");

              qq++;

              DeQueue(Q);

       }

       else{

              strcpy(Q.front->next->data->status,"working");

       }

       if(Q.front->next!=NULL){

              for(QueuePtr p=Q.front->next->next;p!=NULL;p=p->next){

                     if(p->data->cputime!=0){

                            p->data->cputime++;

                     }

              }

       }

       if(Q.front->next!=NULL){

              sort_queue(Q);

       }

}

 

void print(LinkQueue Q,int n,Elem *array){

       printf("cputime is:%d\n",time);

       printf("name cputime needtime priority status\n");

       for(QueuePtr p=Q.front->next;p!=NULL;p=p->next){

              printf("%-4s %-8d %-8d %-9d %s\n",p->data->name,p->data->cputime,p->data->needtime,p->data->priority,p->data->status);

       }

       for(int i=0;i<qq;i++){

              printf("%-4s %-8d %-8d %-9d %s\n",array[i].name,array[i].cputime,array[i].needtime,array[i].priority,array[i].status);

       }

}

int main()

{

       Elemptr data;

       int n;

       int m=0;

       printf("please input process number:");

       scanf("%d",&n);

       data=(struct Elem*)malloc(sizeof(struct Elem));

       printf("input name needtime priority\n");

       for(int i=0;i<n;i++){

              memset(data[i].name,0,2);

              memset(data[i].status,0,7);

              scanf("%5s",data[i].name);

              scanf("%4d",&data[i].needtime);

              scanf("%3d",&data[i].priority);

              strcpy(data[i].status,"Ready");

              data[i].cputime=0;

              m=m+data[i].needtime;

       }

       LinkQueue Q;

       InitQueue(Q);

       Elem *array;

       array=(struct Elem*)malloc(N*sizeof(struct Elem));//声明数组必须确定最大存储量N

       for(int j=0;j<n;j++){

              EnQueue(Q,&data[j]);

       }

       sort_queue(Q);

       while(m--){

              Insertsort(Q,time,array);

              print(Q,n,array);

       }

       return 0; 

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值