用Java完成操作系统实验进程控制

发布时间:2023年12月28日

本文主要使用了Java来实现进程控制,一共有三种调度算法:先来先服务(FCFS)、短进程优先(SPF)和非抢占式优先级调度(HFP)?

进程类Process

主要包含了到达时间,实际执行时间,完成时间,周转时间,等待时间

class Process {
    private int id;
    private int arrivalTime;
    private int burstTime;
    private int completionTime;
    private int turnaroundTime;
    private double waitingTime;

    public int getPriority() {
        return Priority;
    }

    public void setPriority(int priority) {
        Priority = priority;
    }

    private int Priority;

    public Process(int id, int arrivalTime, int burstTime) {
        this.id = id;
        this.arrivalTime = arrivalTime;
        this.burstTime = burstTime;
    }

    public void setCompletionTime(int completionTime) {
        this.completionTime = completionTime;
    }

    public void calculateTurnaroundTime() {
        this.turnaroundTime = completionTime - arrivalTime;
    }

    public void calculateWaitingTime() {
        this.waitingTime = (double) turnaroundTime / burstTime;
    }

    public int getId() {
        return id;
    }

    public int getArrivalTime() {
        return arrivalTime;
    }

    public int getBurstTime() {
        return burstTime;
    }

    public int getCompletionTime() {
        return completionTime;
    }

    public int getTurnaroundTime() {
        return turnaroundTime;
    }

    public double getWaitingTime() {
        return waitingTime;
    }
}

?进程集类 ProcessBook

用于存储多个进程,并实现初始化进程集、添加进程、显示进程、各种排序算法和展示调度结果等功能

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


class ProcessBook {
    private List<Process> processes;

    public ProcessBook() {
        this.processes = new ArrayList<>();
    }

    public void addProcess(Process process) {
        processes.add(process);
    }

    public void displayProcesses() {
        for (Process process : processes) {
            System.out.println("Process ID: " + process.getId() +
                    ", Arrival Time: " + process.getArrivalTime() +
                    ", Burst Time: " + process.getBurstTime());
        }
    }

    public void sortProcessesByArrivalTime() {
        Collections.sort(processes, Comparator.comparingInt(Process::getArrivalTime));
    }

    public void sortProcessesByBurstTime() {
        Collections.sort(processes, Comparator.comparingInt(Process::getBurstTime));
    }

    public void sortProcessesByPriority() {
        Collections.sort(processes, Comparator.comparingInt(Process::getPriority));
    }

    public void scheduleFCFS() {
        int completionTime = 0;
        for (Process process : processes) {
            completionTime += process.getBurstTime();
            process.setCompletionTime(completionTime);
            process.calculateTurnaroundTime();
            process.calculateWaitingTime();
        }
        displaySchedule("First-Come, First-Served (FCFS)");
    }

    public void scheduleSPF() {
        int currentTime = 0;
        for (Process process : processes) {
            currentTime += process.getArrivalTime();
            process.setCompletionTime(currentTime);
            process.calculateTurnaroundTime();
            process.calculateWaitingTime();
        }
        displaySchedule("Shortest Process First (SPF)");
    }

    public void scheduleHFP() {
        sortProcessesByPriority();
        int currentTime = 0;
        for (Process process : processes) {
            currentTime += process.getArrivalTime();
            process.setCompletionTime(currentTime);
            process.calculateTurnaroundTime();
            process.calculateWaitingTime();
        }
        displaySchedule("Highest Priority First (HFP)");
    }

    private void displaySchedule(String title) {
        System.out.println("\n" + title + " Schedule:");
        System.out.println("--------------------------------------------------");
        System.out.println("Process ID\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time");
        System.out.println("--------------------------------------------------");
        for (Process process : processes) {
            System.out.println(process.getId() +
                    "\t\t" + process.getArrivalTime() +
                    "\t\t" + process.getBurstTime() +
                    "\t\t" + process.getCompletionTime() +
                    "\t\t" + process.getTurnaroundTime() +
                    "\t\t" + process.getWaitingTime());
        }
        System.out.println("--------------------------------------------------");
    }
}

主函数,可以选择添加进程,展示进程,以及三种调度算法FCFS、SPF、HFP

import java.util.Scanner;

public class ProcessSchedule {public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    ProcessBook processBook = new ProcessBook();

    while (true) {
        System.out.println("\nProcess Scheduling Simulation Menu:");
        System.out.println("1. Add Process");
        System.out.println("2. Display Processes");
        System.out.println("3. Schedule using FCFS");
        System.out.println("4. Schedule using SPF");
        System.out.println("5. Schedule using HFP");
        System.out.println("0. Exit");

        System.out.print("\nEnter your choice: ");
        int choice = scanner.nextInt();

        switch (choice) {
            case 1:
                System.out.print("Enter Process ID: ");
                int id = scanner.nextInt();
                System.out.print("Enter Arrival Time: ");
                int arrivalTime = scanner.nextInt();
                System.out.print("Enter Burst Time: ");
                int burstTime = scanner.nextInt();

                Process process = new Process(id, arrivalTime, burstTime);
                processBook.addProcess(process);
                break;
            case 2:
                processBook.displayProcesses();
                break;
            case 3:
                processBook.sortProcessesByArrivalTime();
                processBook.scheduleFCFS();
                break;
            case 4:
                processBook.sortProcessesByBurstTime();
                processBook.scheduleSPF();
                break;
            case 5:
                processBook.scheduleHFP();
                break;
            case 0:
                System.out.println("Exiting...");
                System.exit(0);
            default:
                System.out.println("Invalid choice! Please try again.");
        }
    }
}

}

文章来源:https://blog.csdn.net/m0_62840971/article/details/135241943
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。