进程调度算法实现【先来先服务FCFS】【短进程优先SJF】

#include
#include
#include
#define MAX_DURANCE 1e6
using namespace std;
/*
 * codeBy: slience_me
 * time: 2022-5-10 14:00
 */

/**
 * 全局参数
 */
int processCounts;            //进程数
int *comingTimes;            //达到时间
int *serveTimes;             //服务时间
int *finishedTimes;          //完成时间
int *turnoverTimes;          //周转时间
int *waitingTimes;           //等待时间
float *turnoverTimesWeight; //带权周转时间
int methodChoosen;           //所选调度算法

 /**
 * 函数声明
 */
void InitProcessCount();    //输入进程个数
void InitComingTime();   //输入进程到达时间
void InitServeTime();    //输入进程服务时间
void PrintInformation();     //打印输入信息
void ChooseMethod();       //选择调度算法
void Initialize();          //数组初始化
void SJF();                 //短作业优先
void FCFS();                //先来先服务
void PrintSchedule();   //打印调度信息

/**
 * 主方法
 * @return
 */
int main() {
    // 1. 输入进程个数
    InitProcessCount();
    // 2. 输入进程到达时间
    InitComingTime();
    // 3. 输入进程服务时间
    InitServeTime();
    // 4. 打印输入信息
    PrintInformation();
    // 5. 数组初始化
    Initialize();
    // 6. 选择调度算法
    ChooseMethod();
    switch (methodChoosen) {
        case 1:
            FCFS();
            break;
        default:
            SJF();
            break;
    }
    PrintSchedule();
    system("pause");
    return 0;
}

/**
 * 初始化进程数量
 */
void InitProcessCount() {
    cout << "==>请输入进程个数:";
    cin >> processCounts;
}

/**
 * 初始化到来时间
 */
void InitComingTime() {
    comingTimes = new int[processCounts];
    cout << "==>请输入各个进程的到达时间:";
    for (int i = 0; i < processCounts; i++) {
        cin >> comingTimes[i];
    }
}

/**
 * 初始化服务时间
 */
void InitServeTime() {
    serveTimes = new int[processCounts];
    cout << "==>请输入各个进程的要求服务时间:";
    for (int i = 0; i < processCounts; i++) {
        cin >> serveTimes[i];
    }
}

/**
 * 打印信息
 */
void PrintInformation() {
    cout << "==>被进程输入的进程数量: [ " << processCounts << " ] " << endl;
    cout << "==>进程到来的时间展示: ";
    for (int i = 0; i < processCounts; i++) {
        cout << comingTimes[i] << " ";
    }
    cout << endl;
    cout << "==>进程服务的时间展示: ";
    for (int i = 0; i < processCounts; i++) {
        cout << serveTimes[i] << " ";
    }
    cout << endl;
}

/**
 * 选择算法类型
 */
void ChooseMethod() {
    cout << "请选择一种调度方式[ 1-FCFS,  2-SJF]: ";
    cin >> methodChoosen;
}

/**
 * 先来先服务算法
 */
void FCFS() {
    // 1. 设置初始时间0
    int current = 0;
    // 2. 复制一份到达时间的副本copy_comingTimes[]
    int copy_comingTimes[processCounts];
    for (int i = 0; i < processCounts; i++) {
        copy_comingTimes[i] = comingTimes[i];
    }
    // 3. 遍历查找最早到达的进程
    for (int j = 0; j < processCounts; j++) {
        // 先设置默认最早到达的进程earliest是0,默认第1个进程到来时间最早
        int earliestProcess = 0, min = copy_comingTimes[0];
        //先找到当前最先到达的进程,需要使用到达时间的副本来找
        //设min为所有进程中到来最早的时间
        for (int i = 1; i < processCounts; i++) {
            if (copy_comingTimes[i] < min) {
                //遍历找到到来最早的时间与进程
                min = copy_comingTimes[i];
                earliestProcess = i;
            }
        }
        //找到后,设置进程对应的时间禁用
        copy_comingTimes[earliestProcess] = MAX_DURANCE;
        //上一个进程执行完时有可能下个进程还没到达
        if (comingTimes[earliestProcess] > current) {
            current = comingTimes[earliestProcess];
        }
        //更新其完成时间 = 到来时间+服务时间
        finishedTimes[earliestProcess] = current + serveTimes[earliestProcess];
        //等待时间 等待时间 = 当前时间 - 到来时间
        waitingTimes[earliestProcess] = current - comingTimes[earliestProcess];
        //更新当前时间 当前时间 = 当前时间 + 服务时间
        current += serveTimes[earliestProcess];
        //更新周转时间 周转时间 = 服务时间 + 等待时间
        turnoverTimes[earliestProcess] = serveTimes[earliestProcess] + waitingTimes[earliestProcess];
        //更新带权周转时间 带权周转时间 = 周转时间 /服务时间
        turnoverTimesWeight[earliestProcess] =
                (float) turnoverTimes[earliestProcess] / (float) serveTimes[earliestProcess];
    }
}

/**
 * 短进程优先办法
 */
void SJF() {
    // 1. 设置初始时间0
    int current = 0;
    // 2. 复制一份服务时间copy_serveTimes[],到达时间的副本copy_comingTimes[]
    int copy_serveTimes[processCounts], copy_comingTimes[processCounts];
    for (int i = 0; i < processCounts; i++) {
        copy_serveTimes[i] = serveTimes[i];
    }
    for (int i = 0; i < processCounts; i++) {
        copy_comingTimes[i] = comingTimes[i];
    }
    //3. flag 标识当前时间下有无已经到达的进程 1有0无
    int flag = 0;
    for (int i = 0; i < processCounts; i++) {
        // min_p: 当前调度进程的下标
        // min: 当前调度进程的服务时间
        // early:当前调度进程的到达时间
        int min_p = 0, min = copy_serveTimes[0], early = copy_comingTimes[0];
        for (int j = 1; j < processCounts; j++) {
            //进程到来而且该进程服务时间小
            if (copy_comingTimes[j] 进程调度信息打印:" << endl;
    cout << "P_name(ID)    arrived_time    served_time    finished_time    turnover_time    turnover_time_weight"
         << endl;
    for (int i = 0; i < processCounts; i++) {
        printf("%10d    %12d    %11d    %14d    %13d    %20f\n", i, comingTimes[i], serveTimes[i], finishedTimes[i],
               turnoverTimes[i], turnoverTimesWeight[i]);
    }
    //平均周转时间和总周转时间
    float average_turnover_time, sum_turnover_time = 0;
    //平均带权周转时间和总带权周转时间
    float average_turnover_time_weight, sum_turnover_time_weight = 0;
    for (int i = 0; i < processCounts; i++) {
        sum_turnover_time += turnoverTimes[i];
        sum_turnover_time_weight += turnoverTimesWeight[i];
    }
    average_turnover_time = sum_turnover_time / processCounts;
    average_turnover_time_weight = sum_turnover_time_weight / processCounts;
    cout << "==>平均周转时间为:" << average_turnover_time << endl;
    cout << "==>带权平均周转时间为:" << average_turnover_time_weight << endl;
}

/**
 * 初始化数组
 */
void Initialize() {
    // 1. 完成时间
    finishedTimes = new int[processCounts];
    // 2. 周转时间
    turnoverTimes = new int[processCounts];
    // 3. 等待时间
    waitingTimes = new int[processCounts];
    // 4. 带权周转时间
    turnoverTimesWeight = new float[processCounts];
}

Original: https://www.cnblogs.com/Slience-me/p/16341835.html
Author: Slience_me
Title: 进程调度算法实现【先来先服务FCFS】【短进程优先SJF】

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/585861/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Postman 正确使用姿势

    前言: 请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i 简介: Postman是一个接口测试工具,在做接口测试的时候,Postman相当于一个客户端,它可以模拟用户发起的…

    Linux 2023年6月14日
    074
  • [20220303]oracle如何定位使用library cache mutex 3.txt

    [20220303]oracle如何定位使用library cache mutex 3.txt –//这个问题实际上困扰我很久,我开始以为library cache b…

    Linux 2023年6月13日
    071
  • K8S的apiVersion版本详解

    1. 背景 Kubernetes的官方文档中并没有对apiVersion的详细解释,而且因为K8S本身版本也在快速迭代,有些资源在低版本还在beta阶段,到了高版本就变成了stab…

    Linux 2023年6月14日
    075
  • Keytool配置 Tomcat的HTTPS双向认证

    keytool 简介 Keytool 是一个 Java数据证书的管理工具, Keytool将密钥(key)和证书(certificates)存在一个称为 keystore的文件中。…

    Linux 2023年6月6日
    0125
  • redis

    redis 慢 开启 AOF 1、多加服务器 2、增加写的能力 +ssdb Original: https://www.cnblogs.com/y896926473/p/96929…

    Linux 2023年5月28日
    077
  • Centos7 安装部署Kubernetes(k8s)集群

    一.系统环境 二.前言 三.Kubernetes 3.1 概述 3.2 Kubernetes 组件 3.2.1 控制平面组件 3.2.2 Node组件 四.安装部署Kubernet…

    Linux 2023年6月7日
    097
  • 软件科学概论复习

    软件的内在特性 系统的三种类型 S系统:有规范定义,可从规范派生 P系统:需求基于问题的近似解,但现实世界保持稳定 什么是设计模式 基于面向对象设计原则总结出的经验模型。 按照模块…

    Linux 2023年6月8日
    097
  • fake-useragent库自动生成User-Agent

    安装方法 pip(3) install fake-useragent 使用方法如下: import requests from fake_useragent import User…

    Linux 2023年6月13日
    0107
  • [20220909]bbed关于删除记录恢复的问题.txt

    [20220909]bbed关于删除记录恢复的问题.txt –//快下班被别人问的关于删除记录使用bbed恢复的问题,我开始以为很快讲解完,删除记录oracle仅仅打上…

    Linux 2023年6月13日
    076
  • shell 获取变量是什么数据类型

    bash;gutter:true; function check(){ local a="$1" printf "%d" "$a&…

    Linux 2023年5月28日
    079
  • Jmeter 使用Json提取请求数据-2

    在接口测试中有一个这样的场景:业务接口需要用到登录token;下个接口需要用到前个接口返回值作为参数,该怎么实现? 首先先看下登录、业务接口,本文用的jmeter版本为5.4.1 …

    Linux 2023年6月8日
    098
  • SQL错题集

    查找最晚入职员工的所有信息 查找入职员工时间排名倒数第三的员工所有信息 获取所有部门中当前员工薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary 从ti…

    Linux 2023年6月14日
    089
  • vue过滤器和生命周期——day02

    vue之过滤器和生命周期——day02 过滤器: 概念:Vue.js 允许你自定义过滤器, 可被用作一些常见的文本格式化。过滤器可以用在两个地方: mustache 插值和 v-b…

    Linux 2023年6月7日
    0124
  • 深入Go Map的使用技巧

    原文链接:https://www.zhoubotong.site/post/60.html之前写过一篇文章,Go map定义的几种方式以及修改技巧,今天发现还可以深入探讨下开发中容…

    Linux 2023年6月6日
    0116
  • Python eval()函数

    The eval() takes three parameters: expression – this string as parsed and evaluated …

    Linux 2023年6月8日
    089
  • Question06-查询”李”姓老师的数量

    问题比较简单,一个单表查询就可以解决,这里就不过多地讲解 Original: https://www.cnblogs.com/OnlyOnYourself-lzw/p/165738…

    Linux 2023年6月7日
    078
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球