模拟FCFS调度算法(先来先服务)没错,是篇好文章!


一、FCFS的介绍

先来先服务的调度算法:最简单的调度算法,既可以用于作业调度 ,也可以用于程序调度,当作业调度中采用该算法时,系统将按照作业到达的先后次序来进行调度,优先从后备队列中,选择一个或多个位于队列头部的作业,把他们调入内存,分配所需资源、创建进程,然后放入“就绪队列”,直到该进程运行到完成或发生某事件堵塞后,进程调度程序才将处理机分配给其他进程。

简单了说就是如同名字 “先来先服务” ;

二、代码演示

package com.zsh.blog;

import java.util.Scanner;

/**
 * @author:抱着鱼睡觉的喵喵
 * @date:2021/3/19
 * @description:
 */
public class SimulateSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        SimulateFCFS simulateFCFS = new SimulateFCFS();
        boolean flag = true;
        char at = ' ';
        System.out.println("a:Simulate multiple processes to form a queue");
        System.out.println("b:Assign a process to the queue");
        System.out.println("d:Complete all process work");
        System.out.println("e:Exit the simulated system");
        while (flag) {
            System.out.println("Please enter your instructions:");
            at = scanner.next().charAt(0);
            switch (at) {
                case 'a':
                    simulateFCFS.createQueue();
                    break;
                case 'b':
                    simulateFCFS.assignProcess();
                    break;
                case 'd':
                    simulateFCFS.finishAllProcessTask();
                    return;
                case 'e':
                    System.out.println("Simulated is end~");
                    return;
                default:
                    System.out.println("Your input is wrong, please re-enter!");
                    break;
            }
        }
    }

}

class Queue {
    int arrTime;            //timeOfArrival
    int serviceTime;        //timeOfService
    int finishTime;         //timeOfComplish
    int turnTime;           //timeOfTurnaround
    double weightTurnTime;     //timeOfWeightTurnaround
    String processName;      //process number
    Queue next;

    public Queue(int arrTime, int serviceTime, String processName) {
        this.arrTime = arrTime;
        this.serviceTime = serviceTime;
        this.processName = processName;
    }

    public Queue() {
    }
}

/**
 * Simulate FCFS algorithm class
 */
class SimulateFCFS {
    private Queue head = new Queue(-1, -1, null);
    private int timer = 0;
    private Queue tail = head;

    public void createQueue() {
        Queue arr = null;
        Queue temp = head;
        Scanner scanner = new Scanner(System.in);
        System.out.printf("Please enter the number of process tasks to initialize the simulation:");
        int n = scanner.nextInt();
        for (int i = 1; i <= n; i++) {
            System.out.printf("Please enter the process number, start time, and service time of the %d process:",i);
            arr = new Queue();
            keyBordInput(arr, scanner);
            calTime(arr);
            temp.next = arr;
            temp = arr;

        }
        this.tail = arr;
        System.out.println("Simulation allocation is successful!");
    }

    /**
     * Completion time of calculation process - Turnaround time - Weighted turnaround time
     * @param arr
     */
    public void calTime(Queue arr) {
        Queue temp = arr;
        if (this.timer < temp.arrTime) {
            this.timer = arr.arrTime;
        } else {
            if (timer == 0) {
                this.timer = temp.arrTime;
            }
        }
        temp.finishTime = temp.serviceTime + this.timer;
        temp.turnTime = temp.finishTime - temp.arrTime;
        temp.weightTurnTime = (temp.turnTime * 1.0) / (temp.serviceTime * 1.0);
        this.timer += temp.serviceTime;
    }
    /**
     * Process number,arrival time,service time entered from the keyboard
     * @param arr
     * @param scanner
     */
    public void keyBordInput(Queue arr, Scanner scanner) {
        arr.processName = scanner.next();
        arr.arrTime = scanner.nextInt();
        arr.serviceTime = scanner.nextInt();
    }

    /**
     * Assign a process to the queue
     */
    public void assignProcess() {
        Queue newProcess = new Queue();
        Scanner scanner = new Scanner(System.in);
        System.out.printf("Please enter the add process number,start time,and service time of the process:");
        keyBordInput(newProcess, scanner);
        calTime(newProcess);
        this.tail.next = newProcess;
        this.tail = newProcess;
    }

    /**
     * Complish a task of process from the queue
     */
//    public void finishProcessTask() {
//        Queue workingProcess = head;
//
//    }
    /**
     * Complish all task of process from the queue
     */
    public void finishAllProcessTask() {
        if (isEmpty()) {
            return;
        }
        Queue cur = head.next;
        System.out.println("Process number========Arrive time======Service time=======finish Time=======Turn time======WeightTurn time");
        while (true) {
            System.out.printf("\t\t%s\t\t\t\t%d\t\t\t\t\t%d\t\t\t\t\t%d\t\t\t\t%d\t\t\t\t%f",cur.processName,cur.arrTime,cur.serviceTime,cur.finishTime,cur.turnTime,cur.weightTurnTime);
            System.out.println();
            if (cur.next == null) {
                break;
            }
            cur = cur.next;
        }
    }

    public boolean isEmpty() {
        if (head.next == null) {
            System.out.println("Queue is null!");
            return true;
        }
        return false;
    }
}

在这里插入图片描述

三、代码分析


1.使用节点模拟进程

因为需要计算完成时间、周转时间、带权周转时间,所以需要事先给出每个进程到达时间和服务时间

模拟时至少需要以下几个属性(Queue类对象模拟进程)

class Queue {
    int arrTime;            //timeOfArrival
    int serviceTime;        //timeOfService
    int finishTime;         //timeOfComplish
    int turnTime;           //timeOfTurnaround
    double weightTurnTime;     //timeOfWeightTurnaround
    String processName;      //process number
    Queue next;

    public Queue(int arrTime, int serviceTime, String processName) {
        this.arrTime = arrTime;
        this.serviceTime = serviceTime;
        this.processName = processName;
    }

    public Queue() {
    }
}

2.SimulateFCFS(核心模拟FCFS类)

在这里插入图片描述


以下分析核心模拟类

3.创建一个节点为n的队列(模拟就绪队列)

    public void createQueue() {
        Queue arr = null;
        Queue temp = head;	
        Scanner scanner = new Scanner(System.in);
        System.out.printf("Please enter the number of process tasks to initialize the simulation:");
        int n = scanner.nextInt();	//创建节点数为n的队列
        for (int i = 1; i <= n; i++) {
            System.out.printf("Please enter the process number, start time, and service time of the %d process:",i);
            arr = new Queue();
            keyBordInput(arr, scanner);//这个自定义的函数主要用来输入进程的到达时间和服务时间
            calTime(arr); //该自定义函数用来计算完成时间、周转时间、带权周转时间
            temp.next = arr;
            temp = arr;		//进行节点连接

        }
        this.tail = arr;
        System.out.println("Simulation allocation is successful!");
    }

4.核心计算分析

(如果是第一个进程) 完成时间 = 服务时间 + 到达时间


如果是n>1的进程就要考虑前面进程所花费的时间和该进程到达时间的长短问题。如果前面所花费的完成时间大于该进程的到达进程,则(完成时间 = 服务时间+上一个进程的完成时间)
反之则是 (完成时间= 服务时间+到达时间)

//timer是全局变量,用来计算完成时间(解决上面的问题)
public void calTime(Queue arr) {
        Queue temp = arr;
        if (this.timer < temp.arrTime) {
            this.timer = arr.arrTime;
        } else {
            if (timer == 0) {
                this.timer = temp.arrTime;
            }
        }
        temp.finishTime = temp.serviceTime + this.timer;
        temp.turnTime = temp.finishTime - temp.arrTime;
        temp.weightTurnTime = (temp.turnTime * 1.0) / (temp.serviceTime * 1.0);
        this.timer += temp.serviceTime;
    }

5.输入到达时间和服务时间(模拟进程到达和服务)

public void keyBordInput(Queue arr, Scanner scanner) {
        arr.processName = scanner.next();
        arr.arrTime = scanner.nextInt();
        arr.serviceTime = scanner.nextInt();
    }

6.出队列(模拟完成所有进程工作)

public void finishAllProcessTask() {
        if (isEmpty()) {
            return;
        }
        Queue cur = head.next;
        System.out.println("Process number========Arrive time======Service time=======finish Time=======Turn time======WeightTurn time");
        while (true) {
            System.out.printf("\t\t%s\t\t\t\t%d\t\t\t\t\t%d\t\t\t\t\t%d\t\t\t\t%d\t\t\t\t%f",cur.processName,cur.arrTime,cur.serviceTime,cur.finishTime,cur.turnTime,cur.weightTurnTime);
            System.out.println();
            if (cur.next == null) {
                break;
            }
            cur = cur.next;
        }
    }
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Thecoastlines

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值