一、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;
}
}

模拟FCFS调度算法(先来先服务)的更多相关文章

  1. 《操作系统_FCFS和SJF》

    先来先服务FCFS和短作业优先SJF进程调度 转自:https://blog.csdn.net/qq_34374664/article/details/73231072 一.概念介绍和案例解析 FCF ...

  2. python基础(31):进程(一)

    1. 什么是进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行 ...

  3. OS作业模拟SJF和FCFS

    一个OS的作业, 用于模拟短作业优先 和 先来先服务两种作业调度方式. #!/usr/bin/python3.5 ## Modify the SJF and FCFS algorithm in the ...

  4. FCFS,SSTF,SCAN,FIFO,LRO考点题解

    四.应用题 ( 本大题共5 小题,50 分 ) 1. 假设某系统中有五个进程,每个进程的执行时间(单位:ms)和优先数如下表所示(优先数越小,其优先级越高). 进程 执行时间 优先数 P1 P2 P3 ...

  5. Linux 常见的进程调度算法

    1.在介绍进程调度之前,先对进程的状态的概念应该有所了解,下面是关于进程状态的一些基本概念:进程的状态分为三种,分别为: 1).运行态:该状态表明进程在实际占用CPU 2).就绪态: 该状态下进程可以 ...

  6. Linux - 进程调度算法

    进程调度: 无论是在批处理系统还是分时系统中,用户进程数一般都多于处理机数.这将导致它们互相争夺处理机.另外,系统进程也同样需要使用处理机. 这就要求进程调度程序按一定的策略,动态地把处理机分配给处于 ...

  7. 操作系统常用调度算法(转载https://www.cnblogs.com/kxdblog/p/4798401.html)

    操作系统常用调度算法   在操作系统中存在多种调度算法,其中有的调度算法适用于作业调度,有的调度算法适用于进程调度,有的调度算法两者都适用.下面介绍几种常用的调度算法. 先来先服务(FCFS)调度算法 ...

  8. s5-2 Cpu调度算法

    调度程序采用什么算法选择一个进程(作业)? 如何评价调度算法的性能? 调度准则 CPU利用率 – 使CPU尽可能的忙碌 吞吐量 – 单位时间内运行完的进程数 周转时间 – 进程从提交到运行结束的全部时 ...

  9. Python 调度算法 死锁 静动态链接 分页分段

    1 select poll epoll的区别基本上select有3个缺点: 连接数受限查找配对速度慢数据由内核拷贝到用户态poll改善了第一个缺点 epoll改了三个缺点. (1)select,pol ...

随机推荐

  1. 七天接手react项目 —— 生命周期&受控和非受控组件&Dom 元素&Diffing 算法

    生命周期&受控和非受控组件&Dom 元素&Diffing 算法 生命周期 首先回忆一下 vue 中的生命周期: vue 对外提供了生命周期的钩子函数,允许我们在 vue 的各个 ...

  2. CodeUp Problem D: More is better

    根据题目意思,输入的每一对A.B都是直接朋友,并且最后只会得到一个集合,该集合就是Mr Wang选择的男孩. 因此很容易写出代码,甚至不需要自己构建一个并查集,只需要使用C++的set模板,每次读入一 ...

  3. K8S原来如此简单(七)存储

    emptyDir临时卷 有些应用程序需要额外的存储,但并不关心数据在重启后仍然可用. 例如,缓存服务经常受限于内存大小,将不常用的数据转移到比内存慢.但对总体性能的影响很小的存储中. 再例如,有些应用 ...

  4. 不会真有人还不会调用Excel吧?

    哈喽,大家好!我是指北君. 大家有没有过这样的经历:开发某个项目,需要调用Excel控件去生成Excel文件.填充数据.改变格式等等,常常在测试环境中一切正常,但在生产环境却无法正常调用Excel,不 ...

  5. VUE常见问题

    VUE常见问题 对于MVVM的理解 MVVM 是 Model-View-ViewModel 的缩写 Model代表数据模型,也可以在Model中定义数据修改和操作的业务逻辑 View 代表UI 组件, ...

  6. redis事务及相关命令介绍

    redis事务及相关命令介绍 一.概述:和众多其它数据库一样,Redis作为NoSQL数据库也同样提供了事务机制.在Redis中,MULTI/EXEC/DISCARD/WATCH这四个命令是我们实现事 ...

  7. 一条SQL语句执行得很慢的原因有哪些

    说实话,这个问题可以涉及到 MySQL 的很多核心知识,可以扯出一大堆,就像要考你计算机网络的知识时,问你"输入URL回车之后,究竟发生了什么"一样,看看你能说出多少了. 之前腾讯 ...

  8. Postman请求报错:Error:getaddrinfo ENOENT 50.88.88.88

    一.问题来源 今天发布一个新开发的项目到通州现场,内容是开放几个接口给第三方调用,需要现场部署的同事使用postman调用测试一下,现场同事使用postman调用后反馈有如下错误: 二.解决方法 发现 ...

  9. 【Java】这 35 个 Java 代码优化细节!

    前言 代码 优化 ,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没 ...

  10. 用 Java 写一个线程安全的单例模式(Singleton)?

    请参考答案中的示例代码,这里面一步一步教你创建一个线程安全的 Java 单例类.当我们说线程安全时,意思是即使初始化是在多线程环境中,仍然能保证单个实例.Java 中,使用枚举作为单例类是最简单的方式 ...