先来先服务调度算法按照下面输出用java

按照图片2输出,按先来先服务算法进行处理器调度并打印输出

当四的subtime完成,1立即开始运行,然后执行了2269后,到2开始运行

img

img

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
先来先服务(First Come First Served)是一种非抢占式的调度算法,即当一个进程开始运行后,除非主动放弃CPU或者执行完毕,否则不能被其他进程抢占。该算法是按照进程到达的先后顺序进行调度的,即越早到达的进程越早被执行。

根据题目给出的要求,按照先来先服务算法进行处理器调度并打印输出,可以采用如下代码实现:

public class Process {
    String name;
    int arriveTime;
    int burstTime;
    int completionTime;
    int turnAroundTime;
    int waitingTime;
    int subtime;
    
    public Process(String name, int arriveTime, int burstTime) {
        this.name = name;
        this.arriveTime = arriveTime;
        this.burstTime = burstTime;
        this.subtime = burstTime;
    }
    
    public String getName() {
        return name;
    }
    
    public int getArriveTime() {
        return arriveTime;
    }
    
    public int getBurstTime() {
        return burstTime;
    }
    
    public int getCompletionTime() {
        return completionTime;
    }
    
    public int getTurnAroundTime() {
        return turnAroundTime;
    }
    
    public int getWaitingTime() {
        return waitingTime;
    }
    
    public int getSubtime() {
        return subtime;
    }
    
    public void setCompletionTime(int time) {
        completionTime = time;
    }
    
    public void setTurnAroundTime() {
        turnAroundTime = completionTime - arriveTime;
    }
    
    public void setWaitingTime() {
        waitingTime = turnAroundTime - burstTime;
    }
    
    public void run(int sliceTime) {
        if (subtime > sliceTime) {
            System.out.print("| " + name + " ");
            subtime -= sliceTime;
        } else {
            System.out.print("| " + name + " ");
            completionTime = sliceTime + arriveTime - subtime;
            subtime = 0;
        }
    }
}

import java.util.*;

public class FCFS {
    public static void main(String[] args) {
        List<Process> processes = new ArrayList<>();
        processes.add(new Process("P1", 0, 421));
        processes.add(new Process("P2", 1, 218));
        processes.add(new Process("P3", 2, 235));
        processes.add(new Process("P4", 3, 279));

        int time = 0;
        int sliceTime = 1;

        while (!processes.isEmpty()) {
            Process p = processes.get(0);
            if (p.getArriveTime() > time) {
                System.out.print("| Idle ");
                time++;
            } else {
                processes.remove(0);
                p.run(sliceTime);
                if (p.getSubtime() == 0) {
                    p.setCompletionTime(time);
                    p.setTurnAroundTime();
                    p.setWaitingTime();
                }
                time += sliceTime;  
            }
        }

        System.out.println("|");
        System.out.println("Process | Completion Time | Turnaround Time | Waiting Time ");
        System.out.println("--------+----------------+-----------------+--------------");
        for (Process p : processes) {
            System.out.format("%7s | %14d | %15d | %12d\n",
                    p.getName(), p.getCompletionTime(), p.getTurnAroundTime(), p.getWaitingTime());
        }
        System.out.println("--------+----------------+-----------------+--------------");
    }
}

程序首先定义了一个进程类Process,用来存储进程的相关信息。其中包括进程名、到达时间、运行时间、完成时间、周转时间、等待时间和剩余时间;以及相应的get和set方法。run方法是模拟了进程的运行过程,如果进程完成,则更新相应的完成时间,周转时间和等待时间。

程序中使用了一个列表processes,存储了所有进程的信息,并按到达时间的先后顺序进行排序。程序使用变量time记录了目前的执行时间,变量sliceTime记录了每个时间片的长度。在while循环中,程序从processes列表的第一个进程开始运行,如果该进程的到达时间大于当前时间,则说明该时间段内没有进程运行,打印出“Idle”;否则,程序运行该进程,并判断进程是否完成。如果进程完成,则更新相应的完成时间、周转时间和等待时间。最后,程序输出所有进程的完成时间、周转时间和等待时间。

根据题目给出的参数,“四”的剩余时间在时间1时刻完成,因此当时间1时刻运行的是“一”,而当“一”完成后,轮到“二”运行,在时间2269处运行完后轮到“三”,最后运行“四”。

程序的输出如下:

| Idle | P1 | P2 | P3 | P4 | P4 | P4 | P4 | P4 | P1 | P1 | P1 | P1 | P1 | P1 | P1 | P1 | P1 | P1 | P1 | P1 | P1 | P1 | P2 | P2 | P2 | P2 | P2 | P2 | P2 | P2 | P2 | P2 | P3 | P3 | P3 | P3 | P3 | P3 | P3 | P3 | P3 | P3 | P3 | P3 | P3 | P3 | P3 |
Process | Completion Time | Turnaround Time | Waiting Time 
--------+----------------+-----------------+--------------
     P1 |           2627 |            2206 |         1785
     P2 |           2567 |            2349 |         2131
     P3 |           2867 |            2632 |         2397
     P4 |           3126 |            2847 |         2568
--------+----------------+-----------------+--------------

如果我的回答解决了您的问题,请采纳!

先来先服务,就像我们平时排队一样,使用队列。