欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

操作系统实验之进程调度--先来先服务、优先级、时间片轮转

程序员文章站 2022-07-05 09:03:53
...

本实验使用java实现


1. PCB进程控制块

import lombok.Data;

/**
 * PCB  进程控制块
 *
 * @author 
 * @date 2020/12/13
 */
@Data
public class PCB {
    /**
     * 名称
     */
    private String name;
    /**
     * 到达时间
     */
    private int arrivalTime;
    /**
     * 服务时间
     */
    private int servicesTime;
    /**
     * 开始时间
     */
    private int startTime;
    /**
     * 完成时间
     */
    private int completionTime;
    /**
     * 周转时间
     */
    private int turnTime;
    /**
     * 平均周转时间
     */
    private float averTurnTime;

    /**
     * 优先级
     */
    private int priority;
    /**
     * 剩余时间
     */
    private int remainTime;

}

2. 先来先服务算法

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * FCFS 先进先出算法
 *
 * @author 
 * @date 2020/12/13
 */
public class FCFS {
    public static void main(String[] args) {
        int processNumber;
        int currentTime = 0;
        LinkedList<PCB> pcbLinkedList = new LinkedList<PCB>();
        LinkedList<PCB> waitPCBsList = new LinkedList<PCB>();
        LinkedList<PCB> completionPCBList = new LinkedList<PCB>();


        processNumber = inputVariable(pcbLinkedList);

        while (!pcbLinkedList.isEmpty()) {
            waitPCBsList.clear();
            for (PCB pcb : pcbLinkedList) {
                if (pcb.getArrivalTime() <= currentTime) {
                    waitPCBsList.add(pcb);
                }
            }

            if (waitPCBsList.isEmpty()) {
                currentTime++;
                continue;
            }

            PCB pcb = Collections.min(waitPCBsList, new Comparator<PCB>() {
                @Override
                public int compare(PCB o1, PCB o2) {
                    int o1ArrivalTime = o1.getArrivalTime();
                    int o2ArrivalTime = o2.getArrivalTime();
                    if (o1ArrivalTime == o2ArrivalTime) {
                        return 0;
                    }
                    return o1ArrivalTime > o2ArrivalTime ? 1 : -1;
                }
            });
            pcb.setStartTime(currentTime);
            currentTime += pcb.getServicesTime();
            pcb.setCompletionTime(currentTime);
            pcb.setTurnTime(currentTime - pcb.getArrivalTime());

            float averTime = (float) pcb.getTurnTime() / pcb.getServicesTime();
            pcb.setAverTurnTime(Float.parseFloat(
                    String.format("%.3f", averTime)
            ));

            System.out.println(pcb.getName() + "正在执行,开始时间为:" + (currentTime - pcb.getServicesTime())
                    + "    完成时间为:" + currentTime);

            Iterator<PCB> iterator = pcbLinkedList.iterator();
            while (iterator.hasNext()) {
                PCB next = iterator.next();
                if (next.getName().equals(pcb.getName())) {
                    iterator.remove();
                    completionPCBList.add(next);
                    break;
                }
            }
        }

        wholePrint(processNumber, completionPCBList);
    }

    private static int inputVariable(LinkedList<PCB> pcbLinkedList) {
        int processNumber;
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入进程数");
        processNumber = Integer.parseInt(scanner.nextLine());
        System.out.println("请输入进程名");
        for (int i = 0; i < processNumber; i++) {
            PCB pcb = new PCB();
            pcb.setName(scanner.nextLine());
            pcbLinkedList.add(pcb);
        }
        System.out.println("请依次输入进程的    到达时间 服务时间\n");
        for (int i = 0; i < processNumber; i++) {
            PCB pcb = pcbLinkedList.get(i);
            System.out.print(pcb.getName() + "        ");
            pcb.setArrivalTime(scanner.nextInt());
            pcb.setServicesTime(scanner.nextInt());
        }
        return processNumber;
    }

    private static void wholePrint(int processNumber, LinkedList<PCB> completionPCBList) {
        System.out.println("\n\n\n");
        System.out.print("总览    " +
                "name    arrivalTime    servicesTime    " +
                "startTime    completionTime     turnTime    " +
                "averTurnTime\n");
        for (int i = 0; i < processNumber; i++) {
            PCB pcb = completionPCBList.get(i);
            System.out.printf("       %s     %d     %d     %d     %d     %d     %.4f\n",
                    pcb.getName(), pcb.getArrivalTime(), pcb.getServicesTime(),
                    pcb.getStartTime(), pcb.getCompletionTime(), pcb.getTurnTime(),
                    pcb.getAverTurnTime());
        }
    }
}

3. 优先级

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * PSA 优先级调度算法
 *
 * @author 
 * @date 2020/12/13
 */
public class PSA {
    public static void main(String[] args) {
        int processNumber;
        int currentTime = 0;
        LinkedList<PCB> pcbLinkedList = new LinkedList<PCB>();
        LinkedList<PCB> waitPCBsList = new LinkedList<PCB>();
        LinkedList<PCB> completionPCBList = new LinkedList<PCB>();


        processNumber = inputVariable(pcbLinkedList);

        while (!pcbLinkedList.isEmpty()) {
            waitPCBsList.clear();
            for (PCB pcb : pcbLinkedList) {
                if (pcb.getArrivalTime() <= currentTime) {
                    waitPCBsList.add(pcb);
                }
            }

            PCB pcb = Collections.min(waitPCBsList, new Comparator<PCB>() {
                @Override
                public int compare(PCB o1, PCB o2) {
                    int o1Priority = o1.getPriority();
                    int o2Priority = o2.getPriority();
                    if (o1Priority == o2Priority) {
                        return 0;
                    }
                    return o1Priority > o2Priority ? 1 : -1;
                }
            });
            pcb.setStartTime(currentTime);
            currentTime += pcb.getServicesTime();
            pcb.setCompletionTime(currentTime);
            pcb.setTurnTime(currentTime - pcb.getArrivalTime());

            float averTime = (float) pcb.getTurnTime() / pcb.getServicesTime();
            pcb.setAverTurnTime(Float.parseFloat(
                    String.format("%.3f", averTime)
            ));

            System.out.println(pcb.getName() + "正在执行,开始时间为:" + (currentTime - pcb.getServicesTime())
                    + "    完成时间为:" + currentTime);

            Iterator<PCB> iterator = pcbLinkedList.iterator();
            while (iterator.hasNext()) {
                PCB next = iterator.next();
                if (next.getName().equals(pcb.getName())) {
                    iterator.remove();
                    completionPCBList.add(next);
                    break;
                }
            }
        }

        wholePrint(processNumber, completionPCBList);
    }

    private static int inputVariable(LinkedList<PCB> pcbLinkedList) {
        int processNumber;
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入进程数");
        processNumber = Integer.parseInt(scanner.nextLine());
        System.out.println("请输入进程名");
        for (int i = 0; i < processNumber; i++) {
            PCB pcb = new PCB();
            pcb.setName(scanner.nextLine());
            pcbLinkedList.add(pcb);
        }
        System.out.println("请依次输入进程的    到达时间 服务时间 优先级\n");
        for (int i = 0; i < processNumber; i++) {
            PCB pcb = pcbLinkedList.get(i);
            System.out.print(pcb.getName() + "        ");
            pcb.setArrivalTime(scanner.nextInt());
            pcb.setServicesTime(scanner.nextInt());
            pcb.setPriority(scanner.nextInt());
        }
        return processNumber;
    }

    private static void wholePrint(int processNumber, LinkedList<PCB> completionPCBList) {
        System.out.println("\n\n\n");
        System.out.print("总览    " +
                "name    arrivalTime    servicesTime    " +
                "startTime    completionTime     turnTime    " +
                "averTurnTime   priority\n");
        for (int i = 0; i < processNumber; i++) {
            PCB pcb = completionPCBList.get(i);
            System.out.printf("       %s     %d     %d     %d     %d     %d     %.4f    %d\n",
                    pcb.getName(), pcb.getArrivalTime(), pcb.getServicesTime(),
                    pcb.getStartTime(), pcb.getCompletionTime(), pcb.getTurnTime(),
                    pcb.getAverTurnTime(), pcb.getPriority());
        }
    }
}

4. RR时间片轮转

import java.util.LinkedList;
import java.util.Scanner;

/**
 * RR   轮盘调度算法
 *
 * 0 4
 * 1 3
 * 2 4
 * 3 2
 * 4 4
 * @author 
 * @date 2020/12/13
 */
public class RR {
    public static void main(String[] args) {
        int processNumber;
        int currentTime = 0;
        LinkedList<PCB> pcbLinkedList = new LinkedList<>();
        LinkedList<PCB> waitPcbsList = new LinkedList<>();
        LinkedList<PCB> completionPcbsList = new LinkedList<>();

        processNumber = inputVariable(pcbLinkedList);

        while (completionPcbsList.size() < processNumber) {
            readyPcb(currentTime, pcbLinkedList, waitPcbsList);

            if (waitPcbsList.isEmpty()) {
                currentTime += 1;
                continue;
            }
            while (waitPcbsList.size() > 0) {
                PCB pcb = waitPcbsList.getFirst();
                pcb.setRemainTime(pcb.getRemainTime() - 1);
                currentTime++;
                pcb.setCompletionTime(currentTime);

                waitPcbsList.removeFirst();
                if (pcb.getRemainTime() > 0) {
                    waitPcbsList.addLast(pcb);
                } else {
                    pcb.setTurnTime(pcb.getCompletionTime() - pcb.getArrivalTime());
                    pcb.setAverTurnTime((float) pcb.getTurnTime() / pcb.getServicesTime());
                    completionPcbsList.add(pcb);
                }
                readyPcb(currentTime, pcbLinkedList, waitPcbsList);
            }
        }

        wholePrint(processNumber, completionPcbsList, pcbLinkedList);
    }

    private static void readyPcb(int currentTime, LinkedList<PCB> pcbLinkedList, LinkedList<PCB> waitPcbsList) {
        for (PCB pcb : pcbLinkedList) {
            if (pcb.getArrivalTime() <= currentTime
                    && pcb.getRemainTime() > 0
                    && !waitPcbsList.contains(pcb)) {
                waitPcbsList.addFirst(pcb);
            }
        }
    }

    private static int inputVariable(LinkedList<PCB> pcbLinkedList) {
        int processNumber;
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入进程数");
        processNumber = Integer.parseInt(scanner.nextLine());
        System.out.println("请输入进程名");
        for (int i = 0; i < processNumber; i++) {
            PCB pcb = new PCB();
            pcb.setName(scanner.nextLine());
            pcbLinkedList.add(pcb);
        }
        System.out.println("请依次输入进程的    到达时间 服务时间\n");
        for (int i = 0; i < processNumber; i++) {
            PCB pcb = pcbLinkedList.get(i);
            System.out.print(pcb.getName() + "        ");
            pcb.setArrivalTime(scanner.nextInt());
            int serverTime = scanner.nextInt();
            pcb.setServicesTime(serverTime);
            pcb.setRemainTime(serverTime);
        }
        return processNumber;
    }

    private static void wholePrint(int processNumber, LinkedList<PCB> completionPcbList, LinkedList<PCB> pcbLinkedList) {
        System.out.println("\n\n\n");
        System.out.print("总览    " +
                "name    arrivalTime    servicesTime    " +
                "completionTime     turnTime    " +
                "averTurnTime\n");

        for (int i = 0; i < processNumber; i++) {
            PCB pcb1 = pcbLinkedList.get(i);
            int indexOf = completionPcbList.indexOf(pcb1);

            PCB pcb = completionPcbList.get(indexOf);
            System.out.printf("       %s     %d     %d     " +
                            "%d     %d     %.4f\n",
                    pcb.getName(), pcb.getArrivalTime(), pcb.getServicesTime(),
                    pcb.getCompletionTime(), pcb.getTurnTime(),
                    pcb.getAverTurnTime());
        }
    }
}