通过ava语言实现以下要求:选择一个调度算法,实现处理机调度。 1、设计一个按下面三种调度算法实现处理机调度的程序。 1)先来先服务 2)短进程优先或优先权高优先 3)按时间片轮转
时间: 2023-06-20 09:10:29 浏览: 82
操作系统算法多语言实现Cpp,Java, Python 包括进程调度算法、页面置换算法、动态分区匹配算法、磁盘调度算法 .zip
以下是使用 Java 语言实现三种调度算法的示例代码:
1. 先来先服务(FCFS)调度算法:
```
import java.util.*;
public class FCFS {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of processes: ");
int n = input.nextInt();
int[] bt = new int[n];
int[] wt = new int[n];
int[] tat = new int[n];
int[] ct = new int[n];
int sum = 0;
float avgwt = 0, avgtat = 0;
System.out.println("Enter the Burst Time for each process: ");
for (int i = 0; i < n; i++) {
System.out.print("Process " + (i + 1) + ": ");
bt[i] = input.nextInt();
}
ct[0] = bt[0];
for (int i = 1; i < n; i++) {
ct[i] = ct[i - 1] + bt[i];
}
for (int i = 0; i < n; i++) {
tat[i] = ct[i];
wt[i] = tat[i] - bt[i];
avgwt += wt[i];
avgtat += tat[i];
}
System.out.println("\nFCFS Scheduling Algorithm:");
System.out.println("Process\tBT\tCT\tTAT\tWT");
for (int i = 0; i < n; i++) {
System.out.println((i + 1) + "\t" + bt[i] + "\t" + ct[i] + "\t" + tat[i] + "\t" + wt[i]);
}
avgwt /= n;
avgtat /= n;
System.out.println("Average Waiting Time: " + avgwt);
System.out.println("Average Turnaround Time: " + avgtat);
}
}
```
2. 短进程优先或优先权高优先调度算法:
```
import java.util.*;
public class SJF {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of processes: ");
int n = input.nextInt();
int[] bt = new int[n];
int[] wt = new int[n];
int[] tat = new int[n];
int[] ct = new int[n];
int sum = 0;
float avgwt = 0, avgtat = 0;
System.out.println("Enter the Burst Time for each process: ");
for (int i = 0; i < n; i++) {
System.out.print("Process " + (i + 1) + ": ");
bt[i] = input.nextInt();
}
int[] p = new int[n];
for (int i = 0; i < n; i++) {
p[i] = i + 1;
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (bt[i] > bt[j]) {
int temp = bt[i];
bt[i] = bt[j];
bt[j] = temp;
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
ct[0] = bt[0];
for (int i = 1; i < n; i++) {
ct[i] = ct[i - 1] + bt[i];
}
for (int i = 0; i < n; i++) {
tat[i] = ct[i];
wt[i] = tat[i] - bt[i];
avgwt += wt[i];
avgtat += tat[i];
}
System.out.println("\nSJF Scheduling Algorithm:");
System.out.println("Process\tBT\tCT\tTAT\tWT");
for (int i = 0; i < n; i++) {
System.out.println(p[i] + "\t" + bt[i] + "\t" + ct[i] + "\t" + tat[i] + "\t" + wt[i]);
}
avgwt /= n;
avgtat /= n;
System.out.println("Average Waiting Time: " + avgwt);
System.out.println("Average Turnaround Time: " + avgtat);
}
}
```
3. 按时间片轮转调度算法:
```
import java.util.*;
public class RoundRobin {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of processes: ");
int n = input.nextInt();
int[] bt = new int[n];
int[] wt = new int[n];
int[] tat = new int[n];
int[] ct = new int[n];
int[] rem_bt = new int[n];
int sum = 0, t = 0, tq;
float avgwt = 0, avgtat = 0;
System.out.println("Enter the Burst Time for each process: ");
for (int i = 0; i < n; i++) {
System.out.print("Process " + (i + 1) + ": ");
bt[i] = input.nextInt();
rem_bt[i] = bt[i];
}
System.out.print("Enter the time quantum: ");
tq = input.nextInt();
while (true) {
boolean done = true;
for (int i = 0; i < n; i++) {
if (rem_bt[i] > 0) {
done = false;
if (rem_bt[i] > tq) {
t += tq;
rem_bt[i] -= tq;
} else {
t += rem_bt[i];
ct[i] = t;
tat[i] = ct[i];
wt[i] = tat[i] - bt[i];
avgwt += wt[i];
avgtat += tat[i];
rem_bt[i] = 0;
}
}
}
if (done) {
break;
}
}
System.out.println("\nRound Robin Scheduling Algorithm:");
System.out.println("Process\tBT\tCT\tTAT\tWT");
for (int i = 0; i < n; i++) {
System.out.println((i + 1) + "\t" + bt[i] + "\t" + ct[i] + "\t" + tat[i] + "\t" + wt[i]);
}
avgwt /= n;
avgtat /= n;
System.out.println("Average Waiting Time: " + avgwt);
System.out.println("Average Turnaround Time: " + avgtat);
}
}
```
阅读全文