实验三,04彭得源

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 24

struct pcb{
    char name[10];
int arrtime;
int reqtime;
int runtime;
int startime;
int finitime;
int wtime;
int prio;
char status;


};

typedef struct pcb PCB;

//输入函数

void input (PCB job[],int *pnum)

{
    int num;
    int i;
    printf("进程数:");
    scanf("%d",&num);

//输入 
for(i=0;i<num;i++) { 
printf("\n第%d个进程:\n",i+1);
printf("输入进程名:");
scanf("%s",job[i].name);
//printf("到达时间:");
//scanf("%d",&job[i].arrtime);
printf("优先次序:");
scanf("%d",&job[i].prio);
printf("要求服务的时间:");
scanf("%d",&job[i].reqtime);
job[i].status='r';
 job[i].runtime=0;
} 
//设置个数 
*pnum=num;          
}

void pcbprintf(PCB pcbp[],int n)

{

int i; 
if(n==0)
{ 
printf("THE    QUEUE IS NULL!!\n");
return;
} 
printf("\tname\tprio\trqtime\truntime\tstatus:"); 
for(i=0;i<n;i++) 
{ 
printf("\nN %d",i+1);
printf("\t%s",pcbp[i].name);
printf("\t%d",pcbp[i].prio); 
printf("\t%d",pcbp[i].reqtime);
printf("\t%d",pcbp[i].runtime); 
printf("\t%c",pcbp[i].status);
}
}

//冒泡排序

void btsort(PCB btpcb[],int n)

{

int i,j;
PCB pcbtemp; 
for(j=1;j<n;j++) 
{
    for(i=0;i<n-j;i++) 
    {
        if(btpcb[i].prio<btpcb[i+1].prio) 
        { 
            pcbtemp=btpcb[i]; 
            btpcb[i]=btpcb[i+1];
            btpcb[i+1]=pcbtemp;
        }
    }
}
}

void main()

{
    PCB jobarrived[MAX],jobfinished[MAX];
    int numarr,numfin;
    int systime=0;

    int i,j,n;
    PCB pcbrunning;

printf("\n welcome to the DEMO program which shows Process Scheduling!\n\n");
printf("\t\t\tFrom:网络工程彭得源\n");
printf(" NOV,5月,21日,2015\n\n");
input(jobarrived,&numarr); 
numfin=0;


//系统时间置零

systime=0; 
btsort(jobarrived,numarr); 
printf("经按priority排序后,ready Queue是\n"); 
pcbprintf(jobarrived,numarr);

//debug

do
{ 
    btsort(jobarrived,numarr);
    printf("\n\n\t\t现在系统时间 %d:\n\n",systime); 
    printf("ready Queue有 \n"); 
    pcbprintf(jobarrived,numarr);
    pcbrunning=jobarrived[0]; 
    numarr--;
    for(i=0;i<numarr;i++) 
        jobarrived[i]=jobarrived[i+1]; 
        pcbrunning.status='R';
        pcbrunning.startime=systime; 
        printf("\n\n\t\t处于运行态的作业名 %s \n",pcbrunning.name); 
        systime++; 
        pcbrunning.runtime++; 
        if(pcbrunning.runtime==pcbrunning.reqtime) 
        {
        jobfinished[numfin]=pcbrunning;
        jobfinished[numfin].status='F';
        numfin++;
        } 
        else
        { 
            pcbrunning.prio--;
            pcbrunning.status='r';
            jobarrived[numarr]=pcbrunning;
            numarr++; 
        }
        printf("\n\n\t\t 系统时间:%d 已经完成的有\n\n",systime); 
        pcbprintf(jobfinished,numfin); 
        getchar(); 
        getchar(); 
    }while((numarr!=0)); 
    //debug 
    printf("\nCompleted!! ByeBye!!"); 
    getchar(); 
    getchar();
}

 

posted on 2015-05-14 09:50  04彭得源  阅读(152)  评论(0编辑  收藏  举报