java - 如何在 Java 中实现非抢占式优先级调度?
问题描述
我想首先输入具有突发时间和优先级的进程。根据优先级对进程、突发时间进行排序。然后计算等待时间和周转时间。最后计算平均 WT 和 TAT。
解决方案
import java.util.Arrays;
import java.util.Scanner;
public class Priority {
public static void main(String[] args) {
System.out.println("*** Priority Scheduling ***");
System.out.print("Enter Number of Process: ");
Scanner sc = new Scanner(System.in);
int numberOfProcess = sc.nextInt();
String process[] = new String[numberOfProcess];
int p = 1;
for (int i = 0; i < numberOfProcess; i++) {
process[i] = "P" + p;
p++;
}
System.out.println(Arrays.toString(process));
System.out.print("Enter Burst Time for " + numberOfProcess + " process: ");
int burstTime[] = new int[numberOfProcess];
for (int i = 0; i < numberOfProcess; i++) {
burstTime[i] = sc.nextInt();
}
System.out.println(Arrays.toString(burstTime));
System.out.print("Enter Priority for " + numberOfProcess + " process: ");
int priority[] = new int[numberOfProcess];
for (int i = 0; i < numberOfProcess; i++) {
priority[i] = sc.nextInt();
}
System.out.println(Arrays.toString(priority));
// Sorting process & burst time by priority
int temp;
String temp2;
for (int i = 0; i < numberOfProcess - 1; i++) {
for (int j = 0; j < numberOfProcess - 1; j++) {
if (priority[j] > priority[j + 1]) {
temp = priority[j];
priority[j] = priority[j + 1];
priority[j + 1] = temp;
temp = burstTime[j];
burstTime[j] = burstTime[j + 1];
burstTime[j + 1] = temp;
temp2 = process[j];
process[j] = process[j + 1];
process[j + 1] = temp2;
}
}
}
int TAT[] = new int[numberOfProcess + 1];
int waitingTime[] = new int[numberOfProcess + 1];
// Calculating Waiting Time & Turn Around Time
for (int i = 0; i < numberOfProcess; i++) {
TAT[i] = burstTime[i] + waitingTime[i];
waitingTime[i + 1] = TAT[i];
}
int totalWT = 0;
int totalTAT = 0;
double avgWT;
double avgTAT;
System.out.println("Process BT WT TAT");
for (int i = 0; i < numberOfProcess; i++) {
System.out.println(process[i] + " " + burstTime[i] + " " + waitingTime[i] + " " + (TAT[i]));
totalTAT += (waitingTime[i] + burstTime[i]);
totalWT += waitingTime[i];
}
avgWT = totalWT / (double) numberOfProcess;
avgTAT = totalTAT / (double) numberOfProcess;
System.out.println("\n Average Wating Time: " + avgWT);
System.out.println(" Average Turn Around Time: " + avgTAT);
}
}
推荐阅读
- arrays - Powershell脚本将列表分解为多个数组
- php - 使用 PHP 或 Bash 查找当前用户的公共 SSH 密钥是否被授权用于远程主机上的 SSH
- r - purrr 相当于 lapply 返回多元素列表?
- python - 给定两个 NxD 矩阵,以更有效的实现方式生成 NxDxD 矩阵
- javascript - 我需要能够在 5 列中显示 2 行文本,并且每列文本需要不同,全部基于 1 个下拉列表?
- java - 如何存储 1000 行及之后执行批处理?
- objective-c - UITextField 正在编辑时,调用 setText 会挂一会
- r - 使用 dplyr 对随机事务列表进行排序
- javascript - 如何在 xdk 层中显示未读消息的数量?
- xamarin - UIKit.UIBarButtonItem 使用 Xamarin.Forms 4.0 构建失败