西北农林科技大学操作系统实验二(2)——作业调度一设计测试实验_模拟单道批处理系统中作业调度实验-程序员宅基地

技术标签: 实验  操作系统  

编写并调试一个单道系统的作业调度模拟程序。

调度模型:描述调度模型.。(可以采用模块化方法并用框图说明)
作业调度算法:分别采用先来先服务(FCFS),最短作业优先(SJF)、响应比高者优先(HRN)的调度算法。
要求:
1) 定义JCB,并操作之。
2) 描述作业队列。
3) 对每种调度算法都要求打印每个作业开始运行时刻、完成时刻、周转时间、带权周转时间,以及这组作业的平均周转时间及带权平均 周转时间,以比较各种算法的优缺点。

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define getpch(type) (type*)malloc(sizeof(type))
int n;//作业数//the num of jobs
float T1 = 0, T2 = 0;
int times = 0; //记录时间
void swap ( int *a, int *b );
//作业控制块
struct jcb
{
    
    char name[10];//?添加注释 //用户名称
    int reachtime;//?添加注释//作业抵达时间
    int starttime;//?添加注释//作业开始时间
    int needtime;//?添加注释//作业运行所需时间
    int finishtime;//作业完成时间
    float cycletime;//作业周转时间
    float cltime;//?添加注释//带权周转时间
    char state;//作业状态
    struct jcb *next;//结构体指针
    int Rp;//响应比——响应时间/要求服务时间
    int waittime;//等待时间
}*ready = NULL, *p, *q;

typedef struct jcb JCB;

void inital() //建立作业控制块队列,先将其排成先来先服务(FCFS)的模式队列
{
    
    int i;
    printf ( "\n输入作业数:" );
    scanf ( "%d", &n );
    for ( i = 0; i < n; i++ )
    {
    
        p = getpch ( JCB );
        printf ( "\n输入作业名:" );
        scanf ( "%s", p->name );
        getchar();
        p->reachtime = i;
        printf ( "作业默认到达时间:%d", i );
        printf ( "\n输入作业要运行的时间:" );
        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 ) //显示作业运行后的周转时间及带权周转时间等
{
    
    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->cltime );
    getchar();
}

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 = times;//计算作业运行后的完成时间,周转时间等等
    p->state = 'R';
    p->finishtime = p->starttime + p->needtime;
    p->cycletime = ( float ) ( p->finishtime - p->reachtime );
    p->cltime = ( float ) ( p->cycletime / p->needtime );
    T1 += p->cycletime;
    T2 += p->cltime;
    disp ( p, m ); //调用disp()函数,显示作业运行情况
    times += p->needtime;
    p->state = 'F';
    printf ( "\n%s has been finished!\n", p->name );
    free ( p ); //释放运行后的作业
    getchar();
}

void final() //最后打印作业的平均周转时间,平均带权周转时间
{
    
    float s, t;
    t = T1 / n;
    s = T2 / n;
    getchar();
    printf ( "\n\n作业已经全部完成!\n" );
    printf ( "\n%d个作业的平均周转时间是:%f", n, t );
    printf ( "\n%d个作业的平均带权周转时间是%f:\n\n\n", n, s );
}

void sjf ( int m ) //最短作业优先算法(Short Job First,SJF)
{
    
    JCB *min;//定义作业控制块
    int i, iden;
    system ( "cls" );
    inital();// 将作业按照FCFS加入队列
    for ( i = 0; i < n; i++ )
    {
    
        p = min = ready;//分别初始化p、min、ready
        iden = 1;
        do
        {
    
            if ( p->state == 'W' && p->reachtime <= times ) //判断是否满足到达时间等于总时间和作业状态为‘w’
                if ( iden )
                {
    
                    min = p;
                    iden = 0;
                }
                else if ( p->needtime < min->needtime )
                    min = p;
            p = p->next;
            // 1 根据上下文,空格处填写一条指令
        }
        while ( p != NULL );
        if ( iden ) //如果没有满足条件的作业则总时间增加
        {
    
            i--;
            times++;
            if ( times > 100 )
            {
    
                printf ( "\nruntime is too long … error" );
                getchar();
            }
        }
        else
        {
    
            running ( min, m );//如果有则运行队列中的作业
        }
    }
    final();//更新系统时间
}

void fcfs ( int m )  //先来先服务算法
{
    

    int i, iden;
    system ( "cls" );
    inital();
    for ( i = 0; i < n; i++ )
    {
    

        p = ready;
        iden = 1;
        do
        {
    
            if ( p->state == 'W' && p->reachtime <= times )  iden = 0;
            if ( iden ) p = p->next;
        }
        while ( p != NULL && iden );//利用链表遍历找到队列中非空队首

        if ( iden ) //如果没有满足条件的作业则等待
        {
    
            i--;
            printf ( "\n没有满足要求的进程,需等待" );
            times++;
            // 2根据上下文,填入一条语句
            if ( times > 100 )
            {
    
                printf ( "\n时间过长" );
                getchar();
            }
        }
        else//如果有则运行作业
        {
    
            running ( p, m );
        }
    }
    final();//更新系统时间
}
void getPriority()
{
    
     p = ready;
    while ( p != NULL )
    {
    
        //如果到达时间比当前时间晚,则响应比为Ⅰ
        if ( times <= p->reachtime )
            p->Rp = 1;
        //如果到达时间比当前时间早,则响应比为(等待时间+要求服务时间)/要求服务时间
        else
            p->Rp=(double)(p->waittime+p->needtime)/(p->needtime);
    }
}
void HRN ( int m )
{
    
    int i,iden;
        JCB *max;
    system ( "cls" );
    inital();

      for(i=0; i<n; i++)
    {
    
        p=max=ready;
        iden=1;
        //找到响应比最大的可运行的作业
        do
        {
    
            if(p->state=='W'&&p->reachtime<=times)
            {
    
                if(iden)
                {
    
                    max=p;
                    iden=0;
                }
                else if(p->Rp > max->Rp)
                  max=p;
            }
            p=p -> next;
        }
        while (p != NULL);

        if(iden)
        {
    
            i--;
            times++;
            if (times>100)
            {
    
                printf("\nruntime is too long … error");
                getchar();
            }
        }
        else
        {
    
            running(max, m);
            getPriority();//运行完当前后对响应比重新定义
        }
    }
}
void menu()
{
    
    int m;
    system ( "cls" );
    printf ( "\n\n\t\t*********************************************\t\t\n" );
    printf ( "\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 );//利用FCFS算法运行作业
        getchar();
        system ( "cls" );
        break;
    case 2:
        sjf ( m );//利用SJF算法运行作业
        getchar();
        system ( "cls" );
        break;
    case 3:
        HRN(m);
        getchar();
        system("cls");
        break;
    case 0:
        system ( "cls" );
        break;
    default:
        printf ( "选择错误,重新选择." );
        getchar();
        system ( "cls" );
        menu();
    }
}
int main()
{
    
    menu();
    system ( "pause" );
    return 0;
}

hrn运行结果

在这里插入图片描述

多道批处理系统的程序设计

编写并调试一个多道系统的作业调度模拟程序。
调度模型:描述多道调度
模型。(可以采用模块化方法并用框图说明)
作业调度算法:先来先服务(FCFS)
要求:
1) 定义JCB,并操作之。
2) 描述作业队列。
3) 定义多道系统中的各种资源种类及数量、调度作业时必须考虑到每 个作业的资源要求。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define getpch(type) (type*)malloc(sizeof(type))
#define NUM_OF_RESOURCE 3
struct Resource
{
    
    char name[10];//资c源名
    int num;      //资源的数量
    int usedTime; //作业该资源耗时
    struct Resource *link;
};
typedef struct Resource resource;
struct jcb      //作业控制块
{
    
    char name[10];  // 作业名
    int reachtime;  //作业到达时间
    int rnum;       //作业所需资源的种类数
    int starttime;  //作业开始时间
    int needtime;
    int finishtime; //作业完成时间
    float cycletime;//作业周转时间
    float cltime;   //带权周转时间
    char state;     //作业状态
    int IsUsed;    //标志是否使用该资源
    resource *res;  //所需资源指针
    struct jcb *next;      //结构体指针
}*ready[4], *p, *q; //q用来指向当前节点
//创建四个队列,依次对应三种资源的就绪队列

int times  = 0;
typedef struct jcb JCB;

//资源插入作业
void InsertR ( JCB *p, resource *ptr )
{
    
    if ( p -> res == NULL )
    {
    
        p->res = ptr;
    }
    else
    {
    
        resource* pt = p -> res;
        while ( pt->link != NULL )
            pt = pt->link;
        pt -> link = ptr;
    }
}

//作业进入就绪队列,并根据到达时间进行排序
void InsertJobs ( JCB* p, int t )
{
    
    if ( ready[t] == NULL )
        ready[t] = p;
    else
    {
    
        JCB *pt = ready[t];
        q = pt->next;
        //比队首元素来得早,就待在队首
        if ( pt->reachtime > p->reachtime )
        {
    
            ready[t] = p;
            p->next = pt;
        }
        else
        {
    
            while ( q != NULL && q->reachtime <= p->reachtime )
            {
    
                pt = pt->next;
                q = pt->next;
            }
            if ( q == NULL )
            {
    
                pt->next = p;
            }
            else
            {
    
                pt->next = p;
                p->next = q;
            }
        }

    }
}
//输入m个作业
void init ( int m )
{
    
    resource *ptr;
    int i, j;
    printf ( "请输入作业数" );
    printf ( "%d \n", m );
    for ( i = 0; i < m; i++ )
    {
    
        p = getpch ( JCB );
        //
        printf ( "输入作业名: " );
        scanf ( "%s", p->name );
        printf ( "\n输入作业到达时间" );
        scanf ( "%d", &p->reachtime );
        printf ( "\n输入作业所需资源的种类数:" );
        scanf ( "%d", &p->rnum );
        p->res = NULL;
        for ( j = 0; j < p->rnum; j++ )
        {
    
            ptr = ( resource* ) malloc ( sizeof ( resource ) );
            printf ( "\n\t输入资源名: " );
            scanf ( "%s", ptr->name );
            printf ( "\n\t输入所需资源的数量: " );
            scanf ( "%d", &ptr->num );
            printf ( "\n\t输入该作业消耗该资源的时间: " );
            scanf ( "%d", &ptr->usedTime );
            p->IsUsed = 1; //默认都使用
            ptr->link = NULL;
            InsertR ( p, ptr );
        }
        p->state = 'W';//W表示就绪状态
        p->next = NULL;
        InsertJobs   ( p, 0 ); //把p插入后备队列
    }
}
void Running(JCB *p, int j, resource *cre)
{
    
    printf("%s的调度结果:\n", cre[j].name);
    printf("作业名为:%s\n", p->name);
    printf("到达该资源的时间:%d\n", p->reachtime);
    p->reachtime = p->starttime+p->needtime;
    printf("释放该资源的时间%d\n",p->reachtime);
    InsertJobs(p, j+1);
}
//调度算法
void FCFS ( resource *cre )
{
    
    int j = 0;
    for ( j = 0; j < 3; j++ )
    {
    
        printf ( "\n" );
        p = ready[j];
        if ( p != NULL )
        {
    
            ready[j] = p->next;
            p->next = NULL;//脱离原队列
        }
        while ( p != NULL )
        {
    
            //寻找p中所含的资源数
            resource * ptr = p->res;
            while ( ptr != NULL && strcmp ( ptr->name, cre[j].name ) )
            {
    
                ptr = ptr->link;
            }
            //作业需要该资源数量目小于等于现存资源数或不需要该资源,则执行p
            if ( ptr != NULL && ptr->num <= cre[j].num )
            {
    
                //如果当前时刻小于到达时刻,则当前时刻应当到到达时刻才能运行
                if ( times <= p->reachtime )
                {
    
                    times = p->reachtime;
                }
                if ( ptr == NULL )
                {
    
                    p->starttime = times;
                    p->needtime = 0;
                    p->IsUsed = 0; //标志为不需要该资源
                    Running ( p, j, cre );
                }
                //需要该资源则应从系统资源中减去所需的数目
                else
                {
    
                    p->starttime = times;
                    p->needtime = ptr->usedTime;
                    cre[j].num -= ptr->num;
                    Running ( p, j, cre );
                }
                //执行下一个作业
                p = ready[j];
                if ( p != NULL )
                {
    
                    ready[j] = p->next;
                    p->next = NULL;
                }

            }
            else
            {
    
                JCB*q1;
                q1 = ready[j + 1];
                //找到第一个使用该资源的且未释放的进程q1
                while ( q1 != NULL && q1->IsUsed == 0 )
                    q1 = q1->next;
                if ( q1 == NULL )
                    return;
                times = q1->reachtime;
                resource* ptr1 = q1->res;
                while ( ptr1 != NULL && strcmp ( ptr1->name, cre[j].name ) )
                    ptr1 = ptr1->link;
                if ( ptr1 == NULL )
                    return;
                cre[j].num += ptr1->num;
                q1->IsUsed = 0;
            }

        }
        times=0;//重设时间
        JCB* pt = ready[j+1];
        while (pt != NULL)
        {
    
            pt ->IsUsed = 1;
            pt = pt ->next;
        }

    }
}
int main()
{
    
    int m;
    resource cre[3];//计算机资源
    //初始化计算机资源0为输入设备,1为cpu,2为输出设备
    //输入设备
    strcpy(cre[0].name, "INS");
    cre[0].num = 2;
    //CPU
    strcpy(cre[1].name, "CPU");
    cre[1].num = 4;
    //输出设备
    strcpy(cre[2].name, "OUTS");
    cre[2].num = 2;
    //初始化三个队列
    int id=0;
    for(id=0; id<4; id++)
        ready[id]=NULL;
    //输入m个作业
    printf("输入要输入的作业总数: ");
    scanf("%d", &m);
    getchar();
    init(m);
    FCFS(cre);
    q = ready[3];
    while(q != NULL)
    {
    
        ready[3] = q -> next;
        free(q);
        q=q->next;
    }
    return 0;
}

运行结果
在这里插入图片描述

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_44029810/article/details/107464642

智能推荐

c# 调用c++ lib静态库_c#调用lib-程序员宅基地

文章浏览阅读2w次,点赞7次,收藏51次。四个步骤1.创建C++ Win32项目动态库dll 2.在Win32项目动态库中添加 外部依赖项 lib头文件和lib库3.导出C接口4.c#调用c++动态库开始你的表演...①创建一个空白的解决方案,在解决方案中添加 Visual C++ , Win32 项目空白解决方案的创建:添加Visual C++ , Win32 项目这......_c#调用lib

deepin/ubuntu安装苹方字体-程序员宅基地

文章浏览阅读4.6k次。苹方字体是苹果系统上的黑体,挺好看的。注重颜值的网站都会使用,例如知乎:font-family: -apple-system, BlinkMacSystemFont, Helvetica Neue, PingFang SC, Microsoft YaHei, Source Han Sans SC, Noto Sans CJK SC, W..._ubuntu pingfang

html表单常见操作汇总_html表单的处理程序有那些-程序员宅基地

文章浏览阅读159次。表单表单概述表单标签表单域按钮控件demo表单标签表单标签基本语法结构<form action="处理数据程序的url地址“ method=”get|post“ name="表单名称”></form><!--action,当提交表单时,向何处发送表单中的数据,地址可以是相对地址也可以是绝对地址--><!--method将表单中的数据传送给服务器处理,get方式直接显示在url地址中,数据可以被缓存,且长度有限制;而post方式数据隐藏传输,_html表单的处理程序有那些

PHP设置谷歌验证器(Google Authenticator)实现操作二步验证_php otp 验证器-程序员宅基地

文章浏览阅读1.2k次。使用说明:开启Google的登陆二步验证(即Google Authenticator服务)后用户登陆时需要输入额外由手机客户端生成的一次性密码。实现Google Authenticator功能需要服务器端和客户端的支持。服务器端负责密钥的生成、验证一次性密码是否正确。客户端记录密钥后生成一次性密码。下载谷歌验证类库文件放到项目合适位置(我这边放在项目Vender下面)https://github.com/PHPGangsta/GoogleAuthenticatorPHP代码示例://引入谷_php otp 验证器

【Python】matplotlib.plot画图横坐标混乱及间隔处理_matplotlib更改横轴间距-程序员宅基地

文章浏览阅读4.3k次,点赞5次,收藏11次。matplotlib.plot画图横坐标混乱及间隔处理_matplotlib更改横轴间距

docker — 容器存储_docker 保存容器-程序员宅基地

文章浏览阅读2.2k次。①Storage driver 处理各镜像层及容器层的处理细节,实现了多层数据的堆叠,为用户 提供了多层数据合并后的统一视图②所有 Storage driver 都使用可堆叠图像层和写时复制(CoW)策略③docker info 命令可查看当系统上的 storage driver主要用于测试目的,不建议用于生成环境。_docker 保存容器

随便推点

网络拓扑结构_网络拓扑csdn-程序员宅基地

文章浏览阅读834次,点赞27次,收藏13次。网络拓扑结构是指计算机网络中各组件(如计算机、服务器、打印机、路由器、交换机等设备)及其连接线路在物理布局或逻辑构型上的排列形式。这种布局不仅描述了设备间的实际物理连接方式,也决定了数据在网络中流动的路径和方式。不同的网络拓扑结构影响着网络的性能、可靠性、可扩展性及管理维护的难易程度。_网络拓扑csdn

JS重写Date函数,兼容IOS系统_date.prototype 将所有 ios-程序员宅基地

文章浏览阅读1.8k次,点赞5次,收藏8次。IOS系统Date的坑要创建一个指定时间的new Date对象时,通常的做法是:new Date("2020-09-21 11:11:00")这行代码在 PC 端和安卓端都是正常的,而在 iOS 端则会提示 Invalid Date 无效日期。在IOS年月日中间的横岗许换成斜杠,也就是new Date("2020/09/21 11:11:00")通常为了兼容IOS的这个坑,需要做一些额外的特殊处理,笔者在开发的时候经常会忘了兼容IOS系统。所以就想试着重写Date函数,一劳永逸,避免每次ne_date.prototype 将所有 ios

如何将EXCEL表导入plsql数据库中-程序员宅基地

文章浏览阅读5.3k次。方法一:用PLSQL Developer工具。 1 在PLSQL Developer的sql window里输入select * from test for update; 2 按F8执行 3 打开锁, 再按一下加号. 鼠标点到第一列的列头,使全列成选中状态,然后粘贴,最后commit提交即可。(前提..._excel导入pl/sql

Git常用命令速查手册-程序员宅基地

文章浏览阅读83次。Git常用命令速查手册1、初始化仓库git init2、将文件添加到仓库git add 文件名 # 将工作区的某个文件添加到暂存区 git add -u # 添加所有被tracked文件中被修改或删除的文件信息到暂存区,不处理untracked的文件git add -A # 添加所有被tracked文件中被修改或删除的文件信息到暂存区,包括untracked的文件...

分享119个ASP.NET源码总有一个是你想要的_千博二手车源码v2023 build 1120-程序员宅基地

文章浏览阅读202次。分享119个ASP.NET源码总有一个是你想要的_千博二手车源码v2023 build 1120

【C++缺省函数】 空类默认产生的6个类成员函数_空类默认产生哪些类成员函数-程序员宅基地

文章浏览阅读1.8k次。版权声明:转载请注明出处 http://blog.csdn.net/irean_lau。目录(?)[+]1、缺省构造函数。2、缺省拷贝构造函数。3、 缺省析构函数。4、缺省赋值运算符。5、缺省取址运算符。6、 缺省取址运算符 const。[cpp] view plain copy_空类默认产生哪些类成员函数

推荐文章

热门文章

相关标签