FCFS调度算法的简单实现

FCFS——First Come First Serve,顾名思义,就是先来先服务算法,下面有在github上找的代码,改了一些东西,如下:

package org.cloudbus.cloudsim.examples;

import java.util.*;

public class Lab1 {

    public static int[] CreateRandom() {
        int min =2;
        int max =10;
        max-=min;
        int m = (int) (Math.random()*++max)+min;

        int[] mas = new int[m];
        System.out.println("The number of " + m + " processes was created");
        for (int i = 0; i < mas.length; i++) {
            double k = Math.random();
            int l = (int) (k * 100);

            System.out.println("The burst time of the process [" + i + "] is " + l);
            mas[i] = l;
        }
        return mas;
    }






    public static int[] procn(int mas[])
    {
        int n = mas.length;
        int[] k = new int[n];
        for(int i=1;i<=n;i++)
        {
            k[i-1]=i;
        }
        return k;
    }



    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        outer1:
        while (true) {
            System.out.println("Pls, enter the number of the operation you want to do");
            System.out.println("1. Create a new set of processes");
            System.out.println("2. Conc
好的,FCFS调度算法是先来先服务调度算法,其实现代码如下: ```c++ #include <iostream> using namespace std; struct Process { int pid; // 进程号 int burst; // 执行时间 int arrival; // 到达时间 }; void FCFS(Process processes[], int n) { int waitingTime[n], turnAroundTime[n], totalWaitingTime = 0, totalTurnAroundTime = 0; waitingTime[0] = 0; turnAroundTime[0] = processes[0].burst; for (int i = 1; i < n; i++) { waitingTime[i] = turnAroundTime[i - 1]; turnAroundTime[i] = waitingTime[i] + processes[i].burst; } for (int i = 0; i < n; i++) { totalWaitingTime += waitingTime[i]; totalTurnAroundTime += turnAroundTime[i]; } double avgWaitingTime = (double)totalWaitingTime / n; double avgTurnAroundTime = (double)totalTurnAroundTime / n; cout << "FCFS Scheduling Algorithm\n"; cout << "Process\tBurst Time\tArrival Time\tWaiting Time\tTurn-Around Time\n"; for (int i = 0; i < n; i++) { cout << "P" << processes[i].pid << "\t "; cout << processes[i].burst << "\t\t "; cout << processes[i].arrival << "\t\t "; cout << waitingTime[i] << "\t\t "; cout << turnAroundTime[i] << endl; } cout << "Average Waiting Time: " << avgWaitingTime << endl; cout << "Average Turn-Around Time: " << avgTurnAroundTime << endl; } int main() { Process processes[] = {{1, 10, 0}, {2, 5, 0}, {3, 8, 0}}; int n = sizeof(processes) / sizeof(Process); FCFS(processes, n); return 0; } ``` 以上是FCFS调度算法实现代码,可以计算出每个进程的等待时间和周转时间,最后输出平均等待时间和平均周转时间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值