实验二 总结

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define getpch(type)(type*)malloc(sizeof(type))
#define NULL 0    


struct jcb{
    char name[10];        //work name        
    int reachtime;        
    int starttime;
    int needtime;
    int finishtime;
    float cycletime;        //周转时间
    float weightedtime;        //带权周转时间
    float response;            //响应比
    char state;                //状态
    struct jcb *next;        //结构体指针
}*ready=NULL,*p,*q;
typedef struct jcb JCB;

int n,time=0;        //how many? time first work;
float T1=0,T2=0;

void input(){
    int i;
    printf("输入作业个数(2-24)(输入1为默认值5): ");
    scanf("%d",&n);
    if(n==1){
    n=5;
    }
    for(i=0;i<n;i++){
        p=getpch(JCB);
        printf("\n\n");
        printf("输入作业名:");
        scanf("%s",p->name);
        printf("输入到达时间:");
        scanf("%d",&p->reachtime);
        printf("输入所需运行时间:");
        scanf("%d",&p->needtime);
    p->state='W';
    p->next=NULL;
    if(ready==NULL) ready=q=p;
    else{
        q->next=p;
        q=p;
    }
}

}
void disp(JCB *q,int m){      //显示作业运行后的周转时间及带权周转时间等
    if(m==3){                    //显示高响应比算法调度作业后的运行情况 
        printf("\n作业%s正在运行,其运行情况为:\n",q->name);
        printf("开始运行时刻:%d\n",q->starttime);
        printf("完成时刻:%d\n",q->finishtime);
        printf("周转时间:%f\n",q->cycletime);
        printf("带权周转时间:%f\n",q->weightedtime);
        printf("响应比:%f\n",q->response);
        getch();
    }
    else{                         //显示先来先服务,最短作业优先算法调度后作业的运行情况 
         printf("\n作业%s正在运行,其运行情况为:\n",q->name);
         printf("开始运行时刻:%d\n",q->starttime);
        printf("完成时刻:%d\n",q->finishtime);
        printf("周转时间:%f\n",q->cycletime);
        printf("带权周转时间:%f\n",q->weightedtime);
        getch();
    }
} 
void running(JCB *p,int m){                //运行作业 
    if(p==ready){                    //先将要运行的作业从队列中分离出来 
        ready=p->next;
        p->next=NULL;
    }
    else{
        q=ready;
        while(q->next!=p){
            q=q->next;
        }
        q->next=p->next;
    }
    p->starttime=time;
    p->state='R';
    p->finishtime=p->starttime+p->needtime;
    p->cycletime=(float)(p->finishtime-p->reachtime);
    p->weightedtime=(float)(p->cycletime/p->needtime);
    T1=T1+p->cycletime;
    T2=T2+p->weightedtime;
    disp(p,m);
    time=p->needtime+time;
    p->state='F';
    printf("\n%s has been finished!\n press any key to continue...\n",p->name);
    free(p);
    getch();
}

void response(){            //计算队列中作业的高响应比 
    JCB *padv;
    padv=ready;
    do{
        if(padv->state=='W'&&padv->reachtime<=time)
        padv->response=(float)(time-padv->reachtime+padv->needtime)/padv->needtime;
        padv=padv->next;
    }while(padv!=NULL);
}

void final(){                    //最后打印作业的平均周转时间,平均带权周转时间 
    float s,t;
    t=T1/n;
    s=T2/n;
    getch();
    printf("\n\n作业已全部完成!");
    printf("\n%d个作业的平均周转时间是:%f",n,t);
    printf("\n%d个作业的平均带权周转时间是%f:\n\n\n",n,s);
}
void hrrn(int m){               //高响应比算法 
    JCB *min;
    int i,iden;
    system("cls");
    input();
    for(i=0;i<n;i++){
        p=min=ready;
        iden=1;
        response();
        do{
            if(p->state=='W'&&p->reachtime<=time){
                if(iden){
                    min=p;
                    iden=0;
                }
                else if(p->response>min->response)
                {
                    min=p;
                } 
                p=p->next;
            }
        }while(p!=NULL);
            
        if(iden){
            i--;
            time++;
            if(time>1000){
                printf("\n runtime is too long .. error..");
                getch();
            }
        }
        else{
            running(min,m);
        }
    }
        final();
}
void sjf(int m){                //最短作业优先 
    JCB *min;
    int i,iden;
    system("cls");
    input();
    for(i=0;i<n;i++){
        p=min=ready;
        iden=1;
        response();
        do{
            if(p->state=='W'&&p->reachtime<=time)
                if(iden){
                    min=p;
                    iden=0;
                }
            else if(p->response>min->response){
                min=p;
            }
            p=p->next;
        }while(p!=NULL);
        if(iden){
            i--;
            time++;
            if(time>1000){
                printf("\n runtime is too long .. error..");
                getch();
            }
            else{
                running(min,m);
            }

        }
    }
    final();
} 
void fcfs(int m){            //先来先服务 
    int i,iden;
    system("cls");
    input();
    for(i=0;i<n;i++){
        p=ready;
        iden=1;
        do{
            if(p->state=='W'&&p->reachtime<=time) iden=0;
            if(iden) p=p->next;
        }while(p!=NULL&&iden);
        if(iden){
            i--;
            printf("\n没有满足要求的进程,需等待");
            time++;
            if(time>100){
                printf("\n时间过长");
                getch();
            } 
            else{
                running(p,m);
            }
        }
    }
    final();
}
void output(){
    int m;
    system("cls");
    printf("\n\n\t\t**********************************\t\t\n");
    printf("\t\t\t\t作业调度演示\n");
    printf("\n\n\n\t\t\t1.先来先服务算法."); 
    printf("\n\t\t\t2.最短作业优先算法.");
    printf("\n\t\t\t3.响应比高者优先算法.");
    printf("\n\t\t\t0.退出程序.");
    printf("\n\n\t\t\t\t选择所要操作:");
    scanf("%d",&m);
    switch(m){
        case 1:
            fcfs(m);
            getch();
            system("cls");
            output();
            break;
        case 2:
            sjf(m);
            getch();
            system("cls");
            output();
            break;
        case 3:
            hrrn(m);
            getch();
            system("cls");
            output();
            break;
        case 0:
            system("cls");
            break;
        default:
            printf("选择错误,重新选择.");
            getch();
            system("cls");
            output();
    }
}

main(){
    output();
}


总结:目前这段代码还有点小问题,我会继续改进。这次的实验主要是调用函数和指针、栈的问题,之前数据结构学过的,不过。。我还给老师了。。所以这次的实验做起来很吃力,不过我会继续努力的

转载于:https://www.cnblogs.com/colorH/p/4491463.html

CPP程序 #include "stdio.h" #include #include #define getpch(type) (type*)malloc(sizeof(type)) #define NULL 0 struct pcb { /* 定义进程控制块PCB */ char name[10]; char state; int super; int ntime; int rtime; struct pcb* link; }*ready=NULL,*p; typedef struct pcb PCB; sort() /* 建立对进程进行优先级排列函数*/ { PCB *first, *second; int insert=0; if((ready==NULL)||((p->super)>(ready->super))) /*优先级最大者,插入队首*/ { p->link=ready; ready=p; } else /* 进程比较优先级,插入适当的位置中*/ { first=ready; second=first->link; while(second!=NULL) { if((p->super)>(second->super)) /*若插入进程比当前进程优先数大,*/ { /*插入到当前进程前面*/ p->link=second; first->link=p; second=NULL; insert=1; } else /* 插入进程优先数最低,则插入到队尾*/ { first=first->link; second=second->link; } } if(insert==0) first->link=p; } } input() /* 建立进程控制块函数*/ { int i,num; //clrscr(); /*清屏*/ printf("\n 请输入进程数?"); scanf("%d",&num); for(i=0;iname); printf("\n 输入进程优先数:"); scanf("%d",&p->super); printf("\n 输入进程运行时间:"); scanf("%d",&p->ntime); printf("\n"); p->rtime=0;p->state='w'; p->link=NULL; sort(); /* 调用sort函数*/ } } int space() { int l=0; PCB* pr=ready; while(pr!=NULL) { l++; pr=pr->link; } return(l); } disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/ { printf("\n qname \t state \t super \t ndtime \t runtime \n"); printf("|%s\t",pr->name); printf("|%c\t",pr->state); printf("|%d\t",pr->super); printf("|%d\t",pr->ntime); printf("|%d\t",pr->rtime); printf("\n"); } check() /* 建立进程查看函数 */ { PCB* pr; printf("\n **** 当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/ disp(p); pr=ready; printf("\n ****当前就绪队列状态为:\n"); /*显示就绪队列状态*/ while(pr!=NULL) { disp(pr); pr=pr->link; } } destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/ { printf("\n 进程 [%s] 已完成.\n",p->name); free(p); } running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/ { (p->rtime)++; if(p->rtime==p->ntime) destroy(); /* 调用destroy函数*/ else { (p->super)--; p->state='w'; sort(); /*调用sort函数*/ } } main() /*主函数*/ { int len,h=0; char ch; input(); len=space(); while((len!=0)&&(ready!=NULL)) { ch=getchar(); h++; printf("\n The execute number:%d \n",h); p=ready; ready=p->link; p->link=NULL; p->state='R'; check(); running(); printf("\n 按任一键继续......"); ch=getchar(); } printf("\n\n 进程已经完成.\n"); ch=getchar(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值