操作系统课程设计调度算法:用于决定哪个进程可以获得 CPU 的使用权,包括先来先服务、短作业优先、优先级调度等算法。的、Java代码
时间: 2023-12-23 14:06:22 浏览: 76
操作系统 进程管理的调度算法 动态最高优先数优先、先来先服务调度算法、短进程优先调度算法、时间片轮转调度算法
以下是Java实现三种调度算法的代码示例:
1. 先来先服务(FCFS):
```java
import java.util.*;
class Process {
int pid; // 进程ID
int at; // 到达时间
int bt; // 执行时间
public Process(int pid, int at, int bt) {
this.pid = pid;
this.at = at;
this.bt = bt;
}
}
public class FCFS {
public static void main(String[] args) {
List<Process> processes = new ArrayList<>();
processes.add(new Process(1, 0, 24));
processes.add(new Process(2, 0, 3));
processes.add(new Process(3, 0, 3));
int total_time = 0;
for (Process p : processes) {
total_time += p.bt;
}
int[] wt = new int[processes.size()];
int[] tat = new int[processes.size()];
int curr_time = 0;
for (Process p : processes) {
tat[p.pid - 1] = curr_time + p.bt - p.at;
curr_time += p.bt;
wt[p.pid - 1] = tat[p.pid - 1] - p.bt;
}
System.out.println("Process\tAT\tBT\tWT\tTAT");
for (Process p : processes) {
System.out.println(p.pid + "\t\t" + p.at + "\t\t" + p.bt + "\t\t"
+ wt[p.pid - 1] + "\t\t" + tat[p.pid - 1]);
}
}
}
```
2. 短作业优先(SJF):
```java
import java.util.*;
class Process {
int pid; // 进程ID
int at; // 到达时间
int bt; // 执行时间
public Process(int pid, int at, int bt) {
this.pid = pid;
this.at = at;
this.bt = bt;
}
}
public class SJF {
public static void main(String[] args) {
List<Process> processes = new ArrayList<>();
processes.add(new Process(1, 0, 24));
processes.add(new Process(2, 0, 3));
processes.add(new Process(3, 0, 3));
int total_time = 0;
for (Process p : processes) {
total_time += p.bt;
}
int[] wt = new int[processes.size()];
int[] tat = new int[processes.size()];
int curr_time = 0;
while (curr_time < total_time) {
Process min_bt_process = null;
for (Process p : processes) {
if (p.at <= curr_time && (min_bt_process == null || p.bt < min_bt_process.bt)) {
min_bt_process = p;
}
}
tat[min_bt_process.pid - 1] = curr_time + min_bt_process.bt - min_bt_process.at;
curr_time += min_bt_process.bt;
wt[min_bt_process.pid - 1] = tat[min_bt_process.pid - 1] - min_bt_process.bt;
}
System.out.println("Process\tAT\tBT\tWT\tTAT");
for (Process p : processes) {
System.out.println(p.pid + "\t\t" + p.at + "\t\t" + p.bt + "\t\t"
+ wt[p.pid - 1] + "\t\t" + tat[p.pid - 1]);
}
}
}
```
3. 优先级调度:
```java
import java.util.*;
class Process {
int pid; // 进程ID
int at; // 到达时间
int bt; // 执行时间
int priority; // 优先级
public Process(int pid, int at, int bt, int priority) {
this.pid = pid;
this.at = at;
this.bt = bt;
this.priority = priority;
}
}
public class PriorityScheduling {
public static void main(String[] args) {
List<Process> processes = new ArrayList<>();
processes.add(new Process(1, 0, 24, 3));
processes.add(new Process(2, 0, 3, 1));
processes.add(new Process(3, 0, 3, 2));
int total_time = 0;
for (Process p : processes) {
total_time += p.bt;
}
int[] wt = new int[processes.size()];
int[] tat = new int[processes.size()];
int curr_time = 0;
while (curr_time < total_time) {
Process max_priority_process = null;
for (Process p : processes) {
if (p.at <= curr_time && (max_priority_process == null || p.priority > max_priority_process.priority)) {
max_priority_process = p;
}
}
tat[max_priority_process.pid - 1] = curr_time + max_priority_process.bt - max_priority_process.at;
curr_time += max_priority_process.bt;
wt[max_priority_process.pid - 1] = tat[max_priority_process.pid - 1] - max_priority_process.bt;
}
System.out.println("Process\tAT\tBT\tPriority\tWT\tTAT");
for (Process p : processes) {
System.out.println(p.pid + "\t\t" + p.at + "\t\t" + p.bt + "\t\t" + p.priority + "\t\t"
+ wt[p.pid - 1] + "\t\t" + tat[p.pid - 1]);
}
}
}
```
阅读全文