• 操作系统操作系统 实验二 作业调度模拟程序


    #include<stdio.h>
    #include<string.h>
    #include<windows.h>
    typedef struct jcb
    {
        char name[10];
        char status;    //状态:开始s,等待d,就绪j
        int id;
        int arrtime;    //到达时间
        int reqtime;    //要求服务时间
        int startime;    //开始时间
        int finitime;    //完成时间
    
        float TAtime,TAWtime;    //周转时间、带权周转时间
        float prio;        //优先级
        float HRRF;            //响应比
    
    }jcb;
    
    
    
    jcb jcbs[100]; //最多100个作业
    int systime=0,bfsum,add,del;
    int intarr,infin,intjob,sumjcb;
    char T='A';              //T的默认值是A
    
    
    void ins()    //插入
    {
        int i;
        while(T!='E'&&T!='e')
        {
                printf("'I'nsert or 'D'elete or 'E'xit?
    ");
                getchar();
                scanf("%c",&T);
                if(T=='I'||T=='i')
                {
                    printf("你想插入多少个作业?
    ");
                    scanf("%d",&add);
                    bfsum=sumjcb;
                    sumjcb=sumjcb+add;
                    for(i=bfsum+1;i<=sumjcb;i++)
                    {
                        printf("
    第%d个作业:
    ",i);
                        getchar();
                        printf("输入作业名:
    ");
                        gets(jcbs[i].name);
                        printf("到达时间:
    ");
                        scanf("%d",&jcbs[i].arrtime);
                        printf("要求服务时间:
    ");
                        scanf("%d",&jcbs[i].reqtime);
                    }
                }
                else if(T=='D'||T=='d')
                {
                    printf("你想删除第几组?");
                    scanf("%d",&del);
                    for(i=del;i<=sumjcb-1;i++)
                    {
                        strcpy(jcbs[i].name,jcbs[i+1].name);
                        jcbs[i].arrtime=jcbs[i+1].arrtime;
                        jcbs[i].reqtime=jcbs[i+1].reqtime;
                    }
                    sumjcb--;
                }
        }
    }
    void jisuan()
    {
        int i;
        jcbs[1].startime=jcbs[1].arrtime;
        jcbs[1].finitime=jcbs[1].arrtime+jcbs[1].reqtime;
        jcbs[1].TAtime=(float)(jcbs[1].finitime-jcbs[1].arrtime);
        jcbs[1].TAWtime=(float)(jcbs[1].TAtime/jcbs[1].reqtime);
        jcbs[1].HRRF=(float)(jcbs[1].TAtime/jcbs[1].reqtime);
        for(i=2;i<=sumjcb;i++)
        {
            jcbs[i].startime=jcbs[i-1].finitime;
            jcbs[i].finitime=jcbs[i].startime+jcbs[i].reqtime;
            jcbs[i].TAtime=(float)(jcbs[i].finitime-jcbs[i].arrtime);
            jcbs[i].TAWtime=(float)(jcbs[i].TAtime/jcbs[i].reqtime);
            jcbs[i].HRRF=(float)(jcbs[i].TAtime/jcbs[i].reqtime);
        }
    }
    void input()
    {
        int i;
        printf("作业个数:
    ");
        scanf("%d",&sumjcb);
        for(i=1;i<=sumjcb;i++)
        {
            printf("
    第%d个作业:
    ",i);
            getchar();
            printf("输入作业名:
    ");
            gets(jcbs[i].name);
            printf("到达时间:
    ");
            scanf("%d",&jcbs[i].arrtime);
            printf("要求服务时间:
    ");
            scanf("%d",&jcbs[i].reqtime);
        }
        
    }
    void Pseudo_random_number()
    {
        int i;
        srand((unsigned)time(0));  //参数seed是rand()的种子,用来初始化rand()的起始值。
        sumjcb=rand()%23+5;
        for(i=1;i<=sumjcb;i++)
        {
            itoa(i,jcbs[i].name, 2);
         
            jcbs[i].arrtime=rand()%29+1;
        
            jcbs[i].reqtime=rand()%7+1;
        }
        printf("
     	id    	作业到达时间     	作业运行所需要时间
    ");
        for(i=1; i<=sumjcb; i++)
        {
            printf("
    	%s	%12d	%15d",jcbs[i].name,jcbs[i].arrtime,jcbs[i].reqtime);
        }
       printf("
    ");
    
    }
    
    int readfiles()
    {
        int m=0;
        int i=1;
        FILE *fp;     //定义文件指针
        fp=fopen("C:\3.txt","r");  //打开文件
        if(fp==NULL)
        {
            printf("File open error !
    ");
            exit(0);
        }
        printf("
     id    作业到达时间     作业运行所需要时间
    ");
         while(!feof(fp))  
        {
            fscanf(fp,"%d%d%d",&jcbs[i].id,&jcbs[i].arrtime,&jcbs[i].reqtime);  //fscanf()函数将数据读入
            printf("
    %3d%12d%15d
    ",jcbs[i].id,jcbs[i].arrtime,jcbs[i].reqtime);  //输出到屏幕
            itoa(jcbs[i].id,jcbs[i].name,10);
            i++;  
        }
        sumjcb=i-1;
        if(fclose(fp))     //关闭文件
        {
            printf("Can not close the file !
    ");
            exit(0);
        }
        m=i-1;
        return m;
    
    }
    /*void printarr()    //打印
    {
        int i;
        printf("		name	artime	rqtime
    ");
        for(i=1;i<=sumjcb;i++)
        {
            printf("N  %d	",i);
            printf("	%s",jcbs[i].name);
            printf("	%d",jcbs[i].arrtime);
            printf("	%d
    ",jcbs[i].reqtime);
        }
        printf("						现在系统时间%d
    ",systime);
        
    }*/
    void suanfa(int choose)  //排序
    {
        int i,j;
        jcb temp;
        for(i=1;i<=sumjcb;i++)
        {
            for(j=i+1;j<=sumjcb;j++)
            {
                if(choose==1)
                {
                    if(jcbs[i].arrtime>jcbs[j].arrtime)
                    {
                        strcpy(temp.name,jcbs[i].name);
                        temp.status=jcbs[i].status;
                        temp.arrtime=jcbs[i].arrtime;
                        temp.reqtime=jcbs[i].reqtime;
                        temp.startime=jcbs[i].startime;
                        temp.finitime=jcbs[i].finitime;
                        temp.TAtime=jcbs[i].TAtime;
                        temp.TAWtime=jcbs[i].TAWtime;
                        temp.prio=jcbs[i].prio;
                        temp.id=jcbs[i].id;
                        temp.HRRF=jcbs[i].HRRF;
    
                        strcpy(jcbs[i].name,jcbs[j].name);
                        jcbs[i].status=jcbs[j].status;
                        jcbs[i].arrtime=jcbs[j].arrtime;
                        jcbs[i].reqtime=jcbs[j].reqtime;
                        jcbs[i].startime=jcbs[j].startime;
                        jcbs[i].finitime=jcbs[j].finitime;
                        jcbs[i].TAtime=jcbs[j].TAtime;
                        jcbs[i].TAWtime=jcbs[j].TAWtime;
                        jcbs[i].prio=jcbs[j].prio;
                        jcbs[i].id=jcbs[j].id;
                        jcbs[i].HRRF=jcbs[j].HRRF;
    
                        strcpy(jcbs[j].name,temp.name);
                        jcbs[j].status=temp.status;
                        jcbs[j].arrtime=temp.arrtime;
                        jcbs[j].reqtime=temp.reqtime;
                        jcbs[j].startime=temp.startime;
                        jcbs[j].finitime=temp.finitime;
                        jcbs[j].TAtime=temp.TAtime;
                        jcbs[j].TAWtime=temp.TAWtime;
                        jcbs[j].prio=temp.prio;
                        jcbs[j].id=temp.id;
                        jcbs[j].HRRF=temp.HRRF;
                    }
                }
                else if(choose==2)
                {
                    if(jcbs[i].reqtime>jcbs[j].reqtime)
                    {
                        strcpy(temp.name,jcbs[i].name);
                        temp.status=jcbs[i].status;
                        temp.arrtime=jcbs[i].arrtime;
                        temp.reqtime=jcbs[i].reqtime;
                        temp.startime=jcbs[i].startime;
                        temp.finitime=jcbs[i].finitime;
                        temp.TAtime=jcbs[i].TAtime;
                        temp.TAWtime=jcbs[i].TAWtime;
                        temp.prio=jcbs[i].prio;
                        temp.id=jcbs[i].id;
                        temp.HRRF=jcbs[i].HRRF;
    
                        strcpy(jcbs[i].name,jcbs[j].name);
                        jcbs[i].status=jcbs[j].status;
                        jcbs[i].arrtime=jcbs[j].arrtime;
                        jcbs[i].reqtime=jcbs[j].reqtime;
                        jcbs[i].startime=jcbs[j].startime;
                        jcbs[i].finitime=jcbs[j].finitime;
                        jcbs[i].TAtime=jcbs[j].TAtime;
                        jcbs[i].TAWtime=jcbs[j].TAWtime;
                        jcbs[i].prio=jcbs[j].prio;
                        jcbs[i].id=jcbs[j].id;
                        jcbs[i].HRRF=jcbs[j].HRRF;
    
                        strcpy(jcbs[j].name,temp.name);
                        jcbs[j].status=temp.status;
                        jcbs[j].arrtime=temp.arrtime;
                        jcbs[j].reqtime=temp.reqtime;
                        jcbs[j].startime=temp.startime;
                        jcbs[j].finitime=temp.finitime;
                        jcbs[j].TAtime=temp.TAtime;
                        jcbs[j].TAWtime=temp.TAWtime;
                        jcbs[j].prio=temp.prio;
                        jcbs[j].id=temp.id;
                        jcbs[j].HRRF=temp.HRRF;
                    }
                }
                else if(choose==3)
                {
                    if(jcbs[i].HRRF<jcbs[j].HRRF)
                    {
                        strcpy(temp.name,jcbs[i].name);
                        temp.status=jcbs[i].status;
                        temp.arrtime=jcbs[i].arrtime;
                        temp.reqtime=jcbs[i].reqtime;
                        temp.startime=jcbs[i].startime;
                        temp.finitime=jcbs[i].finitime;
                        temp.TAtime=jcbs[i].TAtime;
                        temp.TAWtime=jcbs[i].TAWtime;
                        temp.prio=jcbs[i].prio;
                        temp.id=jcbs[i].id;
                        temp.HRRF=jcbs[i].HRRF;
    
                        strcpy(jcbs[i].name,jcbs[j].name);
                        jcbs[i].status=jcbs[j].status;
                        jcbs[i].arrtime=jcbs[j].arrtime;
                        jcbs[i].reqtime=jcbs[j].reqtime;
                        jcbs[i].startime=jcbs[j].startime;
                        jcbs[i].finitime=jcbs[j].finitime;
                        jcbs[i].TAtime=jcbs[j].TAtime;
                        jcbs[i].TAWtime=jcbs[j].TAWtime;
                        jcbs[i].prio=jcbs[j].prio;
                        jcbs[i].id=jcbs[j].id;
                        jcbs[i].HRRF=jcbs[j].HRRF;
    
                        strcpy(jcbs[j].name,temp.name);
                        jcbs[j].status=temp.status;
                        jcbs[j].arrtime=temp.arrtime;
                        jcbs[j].reqtime=temp.reqtime;
                        jcbs[j].startime=temp.startime;
                        jcbs[j].finitime=temp.finitime;
                        jcbs[j].TAtime=temp.TAtime;
                        jcbs[j].TAWtime=temp.TAWtime;
                        jcbs[j].prio=temp.prio;
                        jcbs[j].id=temp.id;
                        jcbs[j].HRRF=temp.HRRF;
                    }
                }
                else 
                    printf("Error!
    ");
            }
        }
    }
    void printz()
    {
        int i;
        float sum1=0,sum2=0;    //计算平均周转时间、平均带权周转时间
        printf("	name	artime	rqtime	stime	ftime	TAtime		TAWtime
    ");
        for(i=1;i<=sumjcb;i++)
        {
            printf("N %d	",i);
            printf("%s",jcbs[i].name);
            printf("	%d",jcbs[i].arrtime);
            printf("	%d",jcbs[i].reqtime);
            printf("	%d",jcbs[i].startime);
            printf("	%d",jcbs[i].finitime);
            printf("	%f",jcbs[i].TAtime);
            printf("	%f
    ",jcbs[i].TAWtime);
        }
        for(i=1;i<=sumjcb;i++)
        {
            sum1=sum1+jcbs[i].TAtime;
        }
        for(i=1;i<=sumjcb;i++)
        {
            sum2=sum2+jcbs[i].TAWtime;
        }
        printf("
    平均周转时间=%f
    ",sum1/sumjcb);
        printf("
    平均带权周转时间=%f
    ",sum2/sumjcb);
        printf("						现在系统时间%d
    ",systime);
    }
    
    void startit()
    {
        int n;
        printf("
    ");
        printf("**************************
    ");
        printf("**1.调用文本写入数据******
    ");
        printf("**2.调用伪随机数产生数据**
    ");
        printf("**3.调用自己输入模拟数据**
    ");
        printf("**************************
    ");
        scanf("%d",&n);
        switch(n)
        {
        case 1:
            readfiles();
            jisuan();
            break;
        case 2:
            Pseudo_random_number();
            jisuan();
            break;
        case 3:
            input();
            jisuan();
            break;
        default:
            {
            printf("输入有误,请重新输入");
            break;
            }
        }
    }
    
    void choosesuanfa()
    {
        int n;
        printf("*****************************
    ");
        printf("*******1.FCFS算法调度********
    ");
        printf("*******2.SJF算法调度*********
    ");
        printf("*******3.HRRF算法调度********
    ");
        printf("*******4.调用系统清屏********
    ");
        printf("*******5.退出算法调度********
    ");
        printf("*****************************
    ");
        scanf("%d",&n);
        switch(n)
        {
        case 1:
            printf("运行先来先服务算法FCFS
    ");
            suanfa(1);
            printf("
    经按到达时间排序后,未达到队列是
    ");
            printz();
            break;
        case 2:
            printf("运行最短作业优先算法FJS
    ");
            suanfa(2);
            printf("
    经按到达时间排序后,未达到队列是
    ");
            printz();
            break;
        case 3:
            printf("运行最高响应比优先算法HRRF
    ");
            suanfa(3);
            printf("
    经按到达时间排序后,未达到队列是
    ");
            printz();
            break;
        case 4:
            system("cls");
            break;
        case 5:
            exit(0);
            break;
        }
    }
    void main()
    {
        while(1){
        startit();
        choosesuanfa();
        }
        
    
    }
     

  • 相关阅读:
    hdu 4334 Trouble
    hdu 4324 Triangle LOVE
    hdu 4255 A Famous Grid
    hdu 3549 Flow Problem
    hdu 3371 Connect the Cities
    hdu 2846 Repository
    hdu 2120 Ice_cream's world I
    P3304 [SDOI2013]直径(【模板】树直径的必经边)
    P5490 【模板】扫描线
    P1364 医院设置(【模板】树的重心)
  • 原文地址:https://www.cnblogs.com/huaziyi666/p/5422430.html
Copyright © 2020-2023  润新知