DOC

PPCB

By Carolyn Jenkins,2014-12-18 15:11
13 views 0
PPCB

    typedef struct node {

     char name[20]; //进程名

     int prio; //priority进程优先数

     int arrive_time; //进程到达时间

     int sever_time; //需要运行时间

     int finish_time; //完成时间

     int turnover_time; //周转时间

     float weight_time; //带权周转时间

     char state;//进程的状态,就绪 WWait?、运行RRun?、或完成FFinish

     struct pcb *next; }pcb;

    根据个人编程情况,可添加、修改上述的pcb

    #include struct fcfs{

     char name[10];//进程名

     int prio; //priority进程优先数

     int arrivetime; //进程到达时间

     int severtime; //需要运行时间

     int finishtime; //完成时间

     int turnovertime; //周转时间

     float weighttime; //带权周转时间

     char state;//进程的状态,就绪 WWait?、运行RRun?、或完成FFinish

     struct pcb *next; }pcb;

    #include #include int i;//循环值

    int j;//还在阻塞或就绪队列中的进程数 int s;

    int m;//最大priorityid

    struct pcb

    {

     int id;

     int p; //priority

     int cputime;

     int alltime;

     int startblock;

     int blocktime;

     int state; //0 表示ready 1表示end -1表示block };

    struct pcb pro[5]={

     {0,9,0,3,2,3,0},

     {1,38,0,3,-1,0,0},

     {2,30,0,6,-1,0,0},

     {3,29,0,3,-1,0,0},

     {4,0,0,4,-1,0,0}

    };

    int changestate0() { if(pro[0].startblock==0)

     {

     pro[0].state=-1;

     pro[0].startblock--;

     return 1;

     }

     if(pro[0].blocktime==0)

     {

     pro[0].state=0;

     return 1;

     }

     if(pro[0].state==0&&pro[0].startblock!=-1)

     {pro[0].startblock--;return 1;}

     if(pro[0].state==-1&&pro[0].blocktime!=0)

     {pro[0].blocktime--;return 1;}

}

    int state0()

    {

     changestate0();

     s=pro[0].p;

     if(pro[0].state==-1)

     s=-100;

     return s;

}

    int maxp()//求出最大priority

    { state0();

     int max=s;

     m=pro[0].id;

     for(i=0;i

     {

     if(pro[i+1].p>pro[i].p)

     {

     max=pro[i+1].p;

     m=pro[i+1].id;

     }

     }

     return m;

    }

    void change()

    {

     maxp();

     int x=0;//得到m现在的数组编号

     for(i=0;i

     {

     pro[i].p++;

     }

     for(i=0;i

     {

     if(pro[i].id==m)

     x=i;

     }

     pro[x].cputime++;

     pro[x].p=pro[x].p-4;

     pro[x].alltime--;

     if(pro[x].alltime==0)

     {

     pro[x].state=1;

     }

    }

    void display()

    {

     change();

     cout<<"RUNNING PROG:"<

     cout<<"READY_QUEUE: ";

     for(i=0;i

     { if(pro[i].state==0){

     cout<<"->"<

     }

     cout<

     for(i=0;i

     { if(pro[i].state==-1){

     cout<<"->"<

     }

     cout<0表示ready,1表示end,-1表示block"<

cout<<"===============================================================\

    n";

     cout<<"ID ";

     for(i=0;i

     {

     cout.width(10);

     cout<

     }

     cout<

     for(i=0;i

     {

     cout.width(10);

     cout<

     }

     cout<

     for(i=0;i

     {

     cout.width(10);

     cout<

     }

     cout<

     for(i=0;i

     {

     cout.width(10);

     cout<

     }

     cout<

     for(i=0;i

     {

     cout.width(10);

     cout<

     }

     cout<

     for(i=0;i

     {

     cout.width(10);

     cout<

     }

     cout<

     for(i=0;i

     {

     cout.width(10);

     cout<

     }

     cout<

}

    void main()

    {

     j=5;//刚开始有5个进程

     while(j!=0)

     {

     for(i=0;i

     {

     if(pro[i].state==1){

     for(;i

     {pro[i]=pro[i+1];}

     j=j-1;}

     }

     display();

     getchar();

     }

}

    #include struct fcfs{

    char name[10]; float arrivetime; float servicetime; float starttime; float finishtime; float zztime;

    float dqzztime; };

    fcfs a[100];

     void input(fcfs *p,int N)

    { int i;

    printf("intput the process's name & arrivetime & servicetime:\nfor exmple: a 0 100\n");

    for(i=0;i<=N-1;i++)

     { printf("input the %dth process's information:\n",i+1); scanf("%s%f%f",&p[i].name,&p[i].arrivetime,&p[i].servicetime); /* printf("input the %dth process's arrivetime:\n",i+1); scanf("%f",&p[i].arrivetime); printf("input

    the %dth process's servicetime:\n",i+1); scanf("%f",&p[i].servicetime); */ }

} void Print(fcfs *p,float arrivetime,float servicetime,float starttime,float finishtime,float

    zztime,float dqzztime,int N)

    {int k;

    printf("run order:");

    printf("%s",p[0].name);

     for(k=1;k

    {printf("-->%s",p[k].name);

    }

    printf("\nthe process's information:\n");

     printf("\nname\tarrive\tservice\tstart\tfinish\tzz\tdqzz\n"); for(k=0;k<=N-1;k++)

    { printf("%s\t%-.2f\t%-.2f\t%-.2f\t%-.2f\t%-.2f\t%-.2f\t\n",p[k].name ,p[k].arrivetime,p[k].service

    time,p[k].starttime,p[k].finishtime,p[k] .zztime,p[k].dqzztime); /* printf("\nname:%s\t",p[k].name);

    printf("\narrivetime:%-.2f\t",p[k].arrivetime);

    printf("\nservicetime:%-.2f\t",p[k].servicetime);

    printf("\nstarttime:%-.2f\t",p[k].starttime);

    printf("\nfinishtime:%-.2f\t",p[k].finishtime);

    printf("\nzztime:%-.2f\t",p[k].zztime);

    printf("\ndqzztime:%-.2f\t\n",p[k].dqzztime);*/

    }

}

     //pai xu

    void sort(fcfs *p,int N)

    {

    for(int i=0;i<=N-1;i++)

    for(int j=0;j<=i;j++)

    if(p[i].arrivetime

    {

    fcfs temp;

    temp=p[i];

    p[i]=p[j];

    p[j]=temp;

    }

    }

    //yun xing jieduan

     void deal(fcfs *p, float arrivetime,float servicetime,float starttime,float finishtime,float

&zztime,float &dqzztime,int N)

    {

     int k;

    for(k=0;k<=N-1;k++)

    {

    if(k==0)

    { p[k].starttime=p[k].arrivetime;

     p[k].finishtime=p[k].arrivetime+p[k].servicetime;} else

    {

    p[k].starttime=p[k-1].finishtime;

    p[k].finishtime=p[k-1].finishtime+p[k].servicetime;} }

    for(k=0;k<=N-1;k++)

    {

    p[k].zztime=p[k].finishtime-p[k].arrivetime;

     p[k].dqzztime=p[k].zztime/p[k].servicetime;

     }

    }

void FCFS(fcfs *p,int N)

     {

    float arrivetime=0,servicetime=0,starttime=0,finishtime=0,zztime=0,dqzztime =0;

    sort(p,N);

    deal(p,arrivetime,servicetime,starttime,finishtime,zztime,dqzztime,N) ;

    Print(p,arrivetime,servicetime,starttime,finishtime,zztime,dqzztime,N );

    }

     void main()

    {

    int N; printf("------先来先服务调度算法------\n");

    printf("input the process's number:\n"); scanf("%d",&N); input(a,N); FCFS(a,N); }

#include

    struct fcfs{

    char name[10]; //进程名称

    int prio; //priority进程优先数

    float arrivetime; //到达时间

    float servicetime; //运行时间

    float starttime; //开始时间

    float finishtime; //完成时间

float zztime; //周转时间

    float dqzztime; //带权周转时间

    };

    fcfs a[100];

     void input(fcfs *p,int N)

    { int i;

     printf("输入进程名称 & 到达时间 & 运行时间 & 进程优先数:\nfor exmple: a 0 100 1\n");

    for(i=0;i<=N-1;i++)

     {

     printf("输入 %dth 进程信息:\n",i+1);

     scanf("%s%f%f%d",&p[i].name,&p[i].arrivetime,&p[i].servicetime,&p[i].prio);

    /*

     printf("input the %dth process's arrivetime:\n",i+1);

     scanf("%f",&p[i].arrivetime);

     printf("input the %dth process's servicetime:\n",i+1);

     scanf("%f",&p[i].servicetime);

    */

    }

}

     void Print

    (fcfs *p,float arrivetime,float servicetime,float starttime,float finishtime,float zztime,float

    dqzztime,int prio,int N)

    {

     int k;

     printf("run order:");

     printf("%s",p[0].name);

     for(k=1;k

    {

     printf("-->%s",p[k].name);

    }

    printf("\n进程信息:\n");

     printf("\nname\tarrive\tservice\tstart\tfinish\tzz\tdqzz\tprio\n");

     for(k=0;k<=N-1;k++)

    {

printf("%s\t%-.2f\t%-.2f\t%-.2f\t%-.2f\t%-.2f\t%-.2f\t%d\n",p[k].name ,p[k].arrivetime,p[k].servic

    etime,p[k].starttime,p[k].finishtime,p[k] .zztime,p[k].dqzztime,p[k].prio);

    /*

    printf("\nname:%s\t",p[k].name);

    printf("\narrivetime:%-.2f\t",p[k].arrivetime); printf("\nservicetime:%-.2f\t",p[k].servicetime);

    printf("\nstarttime:%-.2f\t",p[k].starttime); printf("\nfinishtime:%-.2f\t",p[k].finishtime); printf("\nzztime:%-.2f\t",p[k].zztime); printf("\ndqzztime:%-.2f\t\n",p[k].dqzztime); */

    }

}

     //pai xu

    void sort(fcfs *p,int N)

    {

    for(int i=0;i<=N-1;i++)

    for(int j=0;j<=i;j++)

    if(p[i].arrivetime

    {

    fcfs temp;

    temp=p[i];

    p[i]=p[j];

    p[j]=temp;

    }

    }

    //yun xing jieduan

     void deal(fcfs *p, float arrivetime,float servicetime,float starttime,float finishtime,float

    &zztime,float &dqzztime,int prio,int N) {

     int k;

    for(k=0;k<=N-1;k++)

    {

    if(k==0)

    {

     p[k].starttime=p[k].arrivetime;

     p[k].finishtime=p[k].arrivetime+p[k].servicetime;}

    else

    {

     p[k].starttime=p[k-1].finishtime;

     p[k].finishtime=p[k-1].finishtime+p[k].servicetime;}

    }

    for(k=0;k<=N-1;k++)

    {

     p[k].zztime=p[k].finishtime-p[k].arrivetime;

     p[k].dqzztime=p[k].zztime/p[k].servicetime; }

}

void FCFS(fcfs *p,int N)

     {

    float arrivetime=0,servicetime=0,starttime=0,finishtime=0,zztime=0,dqzztime =0,prio = 0; sort(p,N);

    deal(p,arrivetime,servicetime,starttime,finishtime,zztime,dqzztime,prio,N) ; Print(p,arrivetime,servicetime,starttime,finishtime,zztime,dqzztime,prio,N ); }

     void main()

    {

    int N;

    printf("------先来先服务调度算法------\n");

    printf("输入进程号:\n");

    scanf("%d",&N);

    input(a,N);

    FCFS(a,N);

    }

Report this document

For any questions or suggestions please email
cust-service@docsford.com