首页
登录 | 注册

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

#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?\n");
            getchar();
            scanf("%c",&T);
            if(T=='I'||T=='i')
            {
                printf("你想插入多少个作业?\n");
                scanf("%d",&add);
                bfsum=sumjcb;
                sumjcb=sumjcb+add;
                for(i=bfsum+1;i<=sumjcb;i++)
                {
                    printf("\n第%d个作业:\n",i);
                    getchar();
                    printf("输入作业名:\n");
                    gets(jcbs[i].name);
                    printf("到达时间:\n");
                    scanf("%d",&jcbs[i].arrtime);
                    printf("要求服务时间:\n");
                    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("作业个数:\n");
    scanf("%d",&sumjcb);
    for(i=1;i<=sumjcb;i++)
    {
        printf("\n第%d个作业:\n",i);
        getchar();
        printf("输入作业名:\n");
        gets(jcbs[i].name);
        printf("到达时间:\n");
        scanf("%d",&jcbs[i].arrtime);
        printf("要求服务时间:\n");
        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("\n \tid    \t作业到达时间     \t作业运行所需要时间\n");
    for(i=1; i<=sumjcb; i++)
    {
        printf("\n\t%s\t%12d\t%15d",jcbs[i].name,jcbs[i].arrtime,jcbs[i].reqtime);
    }
   printf("\n");

}

int readfiles()
{
    int m=0;
    int i=1;
    FILE *fp;     //定义文件指针
    fp=fopen("C:\\3.txt","r");  //打开文件
    if(fp==NULL)
    {
        printf("File open error !\n");
        exit(0);
    }
    printf("\n id    作业到达时间     作业运行所需要时间\n");
     while(!feof(fp))  
    {
        fscanf(fp,"%d%d%d",&jcbs[i].id,&jcbs[i].arrtime,&jcbs[i].reqtime);  //fscanf()函数将数据读入
        printf("\n%3d%12d%15d\n",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 !\n");
        exit(0);
    }
    m=i-1;
    return m;

}
/*void printarr()    //打印
{
    int i;
    printf("\t\tname\tartime\trqtime\n");
    for(i=1;i<=sumjcb;i++)
    {
        printf("N  %d\t",i);
        printf("\t%s",jcbs[i].name);
        printf("\t%d",jcbs[i].arrtime);
        printf("\t%d\n",jcbs[i].reqtime);
    }
    printf("\t\t\t\t\t\t现在系统时间%d\n",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!\n");
        }
    }
}
void printz()
{
    int i;
    float sum1=0,sum2=0;    //计算平均周转时间、平均带权周转时间
    printf("\tname\tartime\trqtime\tstime\tftime\tTAtime\t\tTAWtime\n");
    for(i=1;i<=sumjcb;i++)
    {
        printf("N %d\t",i);
        printf("%s",jcbs[i].name);
        printf("\t%d",jcbs[i].arrtime);
        printf("\t%d",jcbs[i].reqtime);
        printf("\t%d",jcbs[i].startime);
        printf("\t%d",jcbs[i].finitime);
        printf("\t%f",jcbs[i].TAtime);
        printf("\t%f\n",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("\n平均周转时间=%f\n",sum1/sumjcb);
    printf("\n平均带权周转时间=%f\n",sum2/sumjcb);
    printf("\t\t\t\t\t\t现在系统时间%d\n",systime);
}

void startit()
{
    int n;
    printf("\n");
    printf("**************************\n");
    printf("**1.调用文本写入数据******\n");
    printf("**2.调用伪随机数产生数据**\n");
    printf("**3.调用自己输入模拟数据**\n");
    printf("**************************\n");
    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("*****************************\n");
    printf("*******1.FCFS算法调度********\n");
    printf("*******2.SJF算法调度*********\n");
    printf("*******3.HRRF算法调度********\n");
    printf("*******4.调用系统清屏********\n");
    printf("*******5.退出算法调度********\n");
    printf("*****************************\n");
    scanf("%d",&n);
    switch(n)
    {
    case 1:
        printf("运行先来先服务算法FCFS\n");
        suanfa(1);
        printf("\n经按到达时间排序后,未达到队列是\n");
        printz();
        break;
    case 2:
        printf("运行最短作业优先算法FJS\n");
        suanfa(2);
        printf("\n经按到达时间排序后,未达到队列是\n");
        printz();
        break;
    case 3:
        printf("运行最高响应比优先算法HRRF\n");
        suanfa(3);
        printf("\n经按到达时间排序后,未达到队列是\n");
        printz();
        break;
    case 4:
        system("cls");
        break;
    case 5:
        exit(0);
        break;
    }
}
void main()
{
    while(1){
    startit();
    choosesuanfa();
    }
    

}
 

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

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

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

 


相关文章

  • 实验4(2019.4.23)
    [实验结论] 一.对Part1-Part4的总结. 1.数组名作为函数参数时,形参.实参的语法形式书写注意事项. 这一点在书本上P154—P158有详细说明,但是叙述过于冗杂,所以借用“实验4.pdf”中的内容总结: (1)函数声明和函数定 ...
  • 实验四实验报告
    实验结论 Part 1 数组将类型相同的一组数据在内存中连续存放,由实验可看出数组中元素的内存地址是连续的,不同类型数据计算机为其分配的内存空间是不同的. Part 2 定义一维数组a,须指明它包含的元素个数和元素类型,通过数组名和下标的形 ...
  • 第四次上机实验
    实验结论: part 1-4:当数组名作为形式参数时,数组名后面要加[ ]:当数组名作为实际参数时,直接写数组名,后面不要加[ ]:              关于函数的调用及参数传递过程:在程序运行到函数调用这一步骤时,实参会将值赋值给调 ...
  • 实验四(数组)
    实验的总结: int型和float型都用4个字节,char型用一个字节,double型用8个字节.但是在输出5.00和5.000000的时候都没有区别的. 还有就是要注意数组元素的索引号是从零开始的,所以在用for语句的时候<注意它的 ...
  • 实验4
    Part1: #include <stdio.h> const int N=5; int main() { int a[N] = {1, 2, 3, 4, 5}; int i; for(i=0; i<N; i++) pri ...
  • 实验四
    Part1 #include <stdio.h> int main() { double a[5] = {1.0,2.0,3.0,4.0,5.0}; int i; for(i=0; i<5; i++) printf(&qu ...

2019 cecdns.com webmaster#cecdns.com
12 q. 0.077 s.
京ICP备10005923号