用java语言实现实现银行家算法、进程调度过程的模拟、读者-写者问题的写者优先算法。
时间: 2023-07-15 20:13:43 浏览: 43
银行家算法的Java实现:
```java
import java.util.Scanner;
public class BankersAlgorithm {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of processes: ");
int n = sc.nextInt();
System.out.print("Enter the number of resources: ");
int m = sc.nextInt();
int[][] max = new int[n][m];
int[][] allocation = new int[n][m];
int[][] need = new int[n][m];
int[] available = new int[m];
System.out.println("Enter the maximum matrix:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
max[i][j] = sc.nextInt();
}
}
System.out.println("Enter the allocation matrix:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
allocation[i][j] = sc.nextInt();
}
}
System.out.println("Enter the available matrix:");
for (int i = 0; i < m; i++) {
available[i] = sc.nextInt();
}
// Calculate the need matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
// Check if the system is in a safe state
if (isSafe(n, m, allocation, need, available)) {
System.out.println("The system is in a safe state.");
} else {
System.out.println("The system is not in a safe state.");
}
}
public static boolean isSafe(int n, int m, int[][] allocation, int[][] need, int[] available) {
boolean[] visited = new boolean[n];
int[] work = new int[m];
// Initialize the work matrix
for (int i = 0; i < m; i++) {
work[i] = available[i];
}
// Initialize the visited matrix
for (int i = 0; i < n; i++) {
visited[i] = false;
}
// Find an unvisited process that can be completed
for (int i = 0; i < n; i++) {
if (!visited[i] && canComplete(i, m, allocation, need, work)) {
visited[i] = true;
// Add the allocated resources to the work matrix
for (int j = 0; j < m; j++) {
work[j] += allocation[i][j];
}
// Start over from the beginning of the process list
i = -1;
}
}
// Check if all processes have been visited
for (int i = 0; i < n; i++) {
if (!visited[i]) {
return false;
}
}
return true;
}
public static boolean canComplete(int i, int m, int[][] allocation, int[][] need, int[] work) {
for (int j = 0; j < m; j++) {
if (need[i][j] > work[j]) {
return false;
}
}
return true;
}
}
```
进程调度过程的模拟的Java实现:
```java
import java.util.Arrays;
import java.util.Scanner;
public class ProcessScheduling {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of processes: ");
int n = sc.nextInt();
int[] arrivalTime = new int[n];
int[] burstTime = new int[n];
int[] completionTime = new int[n];
int[] waitingTime = new int[n];
int[] turnaroundTime = new int[n];
int[] remainingTime = new int[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter the arrival time of process " + (i + 1) + ": ");
arrivalTime[i] = sc.nextInt();
System.out.print("Enter the burst time of process " + (i + 1) + ": ");
burstTime[i] = sc.nextInt();
remainingTime[i] = burstTime[i];
}
// Sort the processes by arrival time
sortProcesses(n, arrivalTime, burstTime);
int time = 0;
int completed = 0;
while (completed < n) {
int i = getNextProcess(n, arrivalTime, remainingTime, time);
if (i == -1) {
time++;
} else {
remainingTime[i]--;
if (remainingTime[i] == 0) {
completionTime[i] = time + 1;
waitingTime[i] = completionTime[i] - arrivalTime[i] - burstTime[i];
turnaroundTime[i] = completionTime[i] - arrivalTime[i];
completed++;
}
time++;
}
}
double avgWaitingTime = getAverage(waitingTime);
double avgTurnaroundTime = getAverage(turnaroundTime);
System.out.println("Process\tArrival Time\tBurst Time\tCompletion Time\tWaiting Time\tTurnaround Time");
for (int i = 0; i < n; i++) {
System.out.println((i + 1) + "\t\t" + arrivalTime[i] + "\t\t" + burstTime[i] + "\t\t" + completionTime[i] + "\t\t" + waitingTime[i] + "\t\t" + turnaroundTime[i]);
}
System.out.println("Average waiting time: " + avgWaitingTime);
System.out.println("Average turnaround time: " + avgTurnaroundTime);
}
public static void sortProcesses(int n, int[] arrivalTime, int[] burstTime) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arrivalTime[i] > arrivalTime[j]) {
swap(arrivalTime, i, j);
swap(burstTime, i, j);
}
}
}
}
public static int getNextProcess(int n, int[] arrivalTime, int[] remainingTime, int time) {
int minRemainingTime = Integer.MAX_VALUE;
int nextProcess = -1;
for (int i = 0; i < n; i++) {
if (arrivalTime[i] <= time && remainingTime[i] > 0 && remainingTime[i] < minRemainingTime) {
minRemainingTime = remainingTime[i];
nextProcess = i;
}
}
return nextProcess;
}
public static double getAverage(int[] arr) {
double sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum / arr.length;
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
```
读者-写者问题的写者优先算法的Java实现:
```java
import java.util.concurrent.Semaphore;
public class ReaderWriter {
private static int readCount = 0;
private static Semaphore rMutex = new Semaphore(1);
private static Semaphore wMutex = new Semaphore(1);
private static Semaphore resource = new Semaphore(1);
public static void main(String[] args) {
Thread[] readers = new Thread[3];
Thread[] writers = new Thread[2];
for (int i = 0; i < readers.length; i++) {
readers[i] = new Thread(new Reader(i));
readers[i].start();
}
for (int i = 0; i < writers.length; i++) {
writers[i] = new Thread(new Writer(i));
writers[i].start();
}
}
static class Reader implements Runnable {
private int id;
public Reader(int id) {
this.id = id;
}
public void run() {
while (true) {
try {
// Acquire the rMutex semaphore to ensure mutual exclusion
rMutex.acquire();
// Increment the read count
readCount++;
// If this is the first reader, acquire the resource semaphore to prevent writers from accessing the resource
if (readCount == 1) {
resource.acquire();
}
// Release the rMutex semaphore to allow other readers to access the read count
rMutex.release();
// Read the resource
System.out.println("Reader " + id + " is reading the resource.");
// Acquire the rMutex semaphore to ensure mutual exclusion
rMutex.acquire();
// Decrement the read count
readCount--;
// If this is the last reader, release the resource semaphore to allow writers to access the resource
if (readCount == 0) {
resource.release();
}
// Release the rMutex semaphore to allow other readers to access the read count
rMutex.release();
// Sleep for a random amount of time
Thread.sleep((long)(Math.random() * 100));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
static class Writer implements Runnable {
private int id;
public Writer(int id) {
this.id = id;
}
public void run() {
while (true) {
try {
// Acquire the wMutex semaphore to ensure mutual exclusion
wMutex.acquire();
// Acquire the resource semaphore to prevent other writers and readers from accessing the resource
resource.acquire();
// Write to the resource
System.out.println("Writer " + id + " is writing to the resource.");
// Release the resource semaphore to allow other writers and readers to access the resource
resource.release();
// Release the wMutex semaphore to allow other writers to access the resource
wMutex.release();
// Sleep for a random amount of time
Thread.sleep((long)(Math.random() * 100));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
```