文章目录

  • 一、FCFS的介绍
  • 二、代码演示
  • 三、代码分析
    • 1.使用节点模拟进程
    • 2.SimulateFCFS(核心模拟FCFS类)
    • 3.创建一个节点为n的队列(模拟就绪队列)
    • 4.核心计算分析
    • 5.输入到达时间和服务时间(模拟进程到达和服务)
    • 6.出队列(模拟完成所有进程工作)

一、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;            //timeOfArrivalint serviceTime;        //timeOfServiceint finishTime;         //timeOfComplishint turnTime;           //timeOfTurnarounddouble weightTurnTime;     //timeOfWeightTurnaroundString processName;      //process numberQueue 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;            //timeOfArrivalint serviceTime;        //timeOfServiceint finishTime;         //timeOfComplishint turnTime;           //timeOfTurnarounddouble weightTurnTime;     //timeOfWeightTurnaroundString processName;      //process numberQueue 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;}}

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

  1. 先来先服务(FCFS)调度算法(Java实现)

    文章目录 前言 一.先来先服务(FCFS)是什么? 二.先来先服务(FCFS)算法分析 三.实现代码 1.作业数据类 2.作业调度类 3.运行结果 总结 前言 在操作系统中作业调度的主要任务是根据PC ...

  2. Java操作系统进程调度算法——先来先服务(FCFS)算法

    Java操作系统进程调度算法--先来先服务(FCFS)算法 Java操作系统进程调度算法--先来先服务(FCFS)算法 文章目录 Java操作系统进程调度算法--先来先服务(FCFS)算法 前言 一. ...

  3. fcfs调度算法_FCFS:先来先服务调度算法

    fcfs调度算法 The FCFS, which stands for First Come First Serve Scheduling Algorithm, is a non-preemptive ...

  4. FCFS调度算法(操作系统)先来先服务

    FCFS调度算法(FCFS,First Come First Serve) 算法思想: 主要从"公平的角度考虑"(类似于我们生活中排队买东西) 算法规则: 按照作业/进程到达的先后 ...

  5. java实现fcfs_模拟实现FCFS(先来先服务)算法

    模拟实现FCFS(先来先服务)算法 FCFS类(先来先服务算法类): package com.fjnu.JavaSubject03; import java.io.BufferedReader; im ...

  6. 操作系统实验 作业调度算法 先来先服务FCFS调度算法

    作业调度算法 先来先服务FCFS调度算法 作业调度的原理: 非抢占调度 把作业从外存调入内存 作业调度算法: 先来先服务FCFS 短作业优先SJF 静态优先级调度 高响应比优先调度 实验原理 作业调度 ...

  7. 操作系统大作业 基于Linux的模拟进程调度算法 运用c++语言编程 在VMware虚拟机里 centos 亲自写亲自测试 代码 说明书

    发布文章 博文管理我的博客退出 Trash Temp 操作系统大作业 基于Linux的模拟进程调度算法 运用c++语言编程 在VMware虚拟机里 centos 亲自写亲自测试 代码 说明书 @[TO ...

  8. 计算机操作原理进程调度算法---先来先服务,短进程优先(C语言)

    目录 先来先服务调度算法: 短进程优先调度算法: 两种进程调度算法优缺点 思维导图 程序代码: 先来先服务调度算法: 先来先服务(FCFS)调度算法是一种最简单的调度算法,该算法既可用于作业调度,也可 ...

  9. fcfs调度算法_C / C ++程序用于先到先得(FCFS)调度算法

    fcfs调度算法 Here you will get C/C++ program for first come first served (fcfs) scheduling algorithm. 在这 ...

最新文章

  1. linux基础(2)-网卡配置
  2. 黑客零基础入门 | 网络安全
  3. 直播 | EMNLP 2020论文解读:从上下文学习还是从实体名字学习?
  4. CentOS 初体验十二:wget下载文件
  5. VTK:超树网格源用法实战
  6. SAP Spartacus键盘按下tab键之后,出现的focus state border是如何实现的
  7. 如何找到Partner 相关设置里哪些是SAP 标准deliver的,哪些是我们自己创建的
  8. session过期时间控制的一些常用方法
  9. lvs工作在第几层_LVS 原理(调度算法、四种模式、四层负载均衡和七层 的区别)...
  10. AudioBufferSourceNode
  11. 笨方法学Python(一)
  12. ipad iphone横屏竖屏
  13. Xero系列之 中小企业会计宝
  14. 软件测试工程师成长之路 掌握软件测试九大技术主题
  15. Word文档怎样添加图片?技巧分享!怎么在Word文档中加入图片?
  16. access随系统启动的宏_Access 中启动带宏的excel
  17. DirectoryInfo 类
  18. SpringBoot监控
  19. 如何判断合法的立即数
  20. 探探提醒对方账号异常_探探账号异常不能回复消息怎么办

热门文章

  1. Windows下用cmd命令安装及卸载服务[转]
  2. 安卓 ce linux,手持移动数据终端的操作系统有哪些?Windows CE,Mobile和安卓各有什么优缺点?...
  3. 数组查找———二分(折半)查找法
  4. 英特尔核显自定义分辨率_核显性能大提升 11代酷睿核显畅玩《战地5》
  5. 统计消息总数_和公牛一战,库里创三个记录,耀眼的还是三分球总数
  6. 淮阳一高2021高考成绩查询,周口教育网2021年淮阳中招成绩查询系统
  7. python 程序运行插件_如何使Python插件在Pluma中运行?
  8. 群晖218J安装mysql_ds216(群晖218j可以换内存吗)
  9. 二十二、 深入Python的进程和线程(上篇)
  10. 二十一、深入Python强大的装饰器