Java语言实现银行家算法、进程调度过程的模拟、读者-写者问题的写者优先算法。
时间: 2024-01-12 08:05:51 浏览: 23
银行家算法Java实现:
```java
import java.util.Scanner;
public class BankerAlgorithm {
private int need[][], allocate[][], max[][], available[][], np, nr;
private void input() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter no of processes and resources: ");
np = sc.nextInt(); // no of processes
nr = sc.nextInt(); // no of resources
need = new int[np][nr]; // initializing arrays
max = new int[np][nr];
allocate = new int[np][nr];
available = new int[1][nr];
System.out.println("Enter allocation matrix: ");
for (int i = 0; i < np; i++)
for (int j = 0; j < nr; j++)
allocate[i][j] = sc.nextInt(); // allocation matrix
System.out.println("Enter max matrix: ");
for (int i = 0; i < np; i++)
for (int j = 0; j < nr; j++)
max[i][j] = sc.nextInt(); // max matrix
System.out.println("Enter available matrix: ");
for (int j = 0; j < nr; j++)
available[0][j] = sc.nextInt(); // available matrix
sc.close();
}
private int[][] calc_need() {
for (int i = 0; i < np; i++)
for (int j = 0; j < nr; j++) // calculating need matrix
need[i][j] = max[i][j] - allocate[i][j];
return need;
}
private boolean check(int i) {
// checking if all resources for ith process can be allocated
for (int j = 0; j < nr; j++)
if (available[0][j] < need[i][j])
return false;
return true;
}
public void isSafe() {
input();
calc_need(); // calculating need matrix
boolean done[] = new boolean[np];
int j = 0;
while (j < np) { // until all process allocated
boolean allocated = false;
for (int i = 0; i < np; i++)
if (!done[i] && check(i)) { // trying to allocate
for (int k = 0; k < nr; k++)
available[0][k] = available[0][k] - need[i][k] + max[i][k];
System.out.println("Allocated process : " + i);
allocated = done[i] = true;
j++;
}
if (!allocated)
break; // if no allocation
}
if (j == np) // if all processes are allocated
System.out.println("\nSafely allocated");
else
System.out.println("All process cant be allocated safely");
}
public static void main(String[] args) {
new BankerAlgorithm().isSafe();
}
}
```
进程调度过程的模拟Java实现:
```java
import java.util.*;
public class ProcessSchedulingSimulation {
static String processNames[];
static int arrivalTimes[];
static int burstTimes[];
static int completionTimes[];
static int turnaroundTimes[];
static int waitingTimes[];
static int numberOfProcesses;
static float averageWaitingTime;
static float averageTurnaroundTime;
static int totalWaitingTime;
static int totalTurnaroundTime;
public static void main(String args[]) {
acceptInput();
computeOutput();
printOutput();
}
static void acceptInput() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of processes: ");
numberOfProcesses = scanner.nextInt();
processNames = new String[numberOfProcesses];
arrivalTimes = new int[numberOfProcesses];
burstTimes = new int[numberOfProcesses];
completionTimes = new int[numberOfProcesses];
turnaroundTimes = new int[numberOfProcesses];
waitingTimes = new int[numberOfProcesses];
for (int i = 0; i < numberOfProcesses; i++) {
System.out.print("Enter the name of process " + (i + 1) + ": ");
processNames[i] = scanner.next();
System.out.print("Enter the arrival time of process " + (i + 1) + ": ");
arrivalTimes[i] = scanner.nextInt();
System.out.print("Enter the burst time of process " + (i + 1) + ": ");
burstTimes[i] = scanner.nextInt();
}
}
static void computeOutput() {
for (int i = 0; i < numberOfProcesses; i++) {
if (i == 0) {
completionTimes[i] = arrivalTimes[i] + burstTimes[i];
} else {
if (arrivalTimes[i] > completionTimes[i - 1]) {
completionTimes[i] = arrivalTimes[i] + burstTimes[i];
} else {
completionTimes[i] = completionTimes[i - 1] + burstTimes[i];
}
}
turnaroundTimes[i] = completionTimes[i] - arrivalTimes[i];
waitingTimes[i] = turnaroundTimes[i] - burstTimes[i];
totalWaitingTime += waitingTimes[i];
totalTurnaroundTime += turnaroundTimes[i];
}
averageWaitingTime = (float) totalWaitingTime / numberOfProcesses;
averageTurnaroundTime = (float) totalTurnaroundTime / numberOfProcesses;
}
static void printOutput() {
System.out.println("\nProcess Name\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time");
for (int i = 0; i < numberOfProcesses; i++) {
System.out.println(
processNames[i] + "\t\t" + arrivalTimes[i] + "\t\t" + burstTimes[i] + "\t\t" + completionTimes[i]
+ "\t\t" + turnaroundTimes[i] + "\t\t" + waitingTimes[i]);
}
System.out.println("\nAverage Turnaround Time: " + averageTurnaroundTime);
System.out.println("Average Waiting Time: " + averageWaitingTime);
}
}
```
读者-写者问题的写者优先算法Java实现:
```java
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;
public class WriterPriorityReaderWriter {
public static void main(String[] args) {
Semaphore resourceAccess = new Semaphore(1);
Semaphore readCountAccess = new Semaphore(1);
Semaphore serviceQueue = new Semaphore(1);
int readCount = 0;
Writer writer = new Writer(resourceAccess, readCountAccess, serviceQueue);
Reader reader = new Reader(resourceAccess, readCountAccess, serviceQueue, readCount);
writer.start();
reader.start();
}
}
class Writer extends Thread {
Semaphore resourceAccess;
Semaphore readCountAccess;
Semaphore serviceQueue;
public Writer(Semaphore resourceAccess, Semaphore readCountAccess, Semaphore serviceQueue) {
super("Writer");
this.resourceAccess = resourceAccess;
this.readCountAccess = readCountAccess;
this.serviceQueue = serviceQueue;
}
public void run() {
while (true) {
try {
serviceQueue.acquire();
resourceAccess.acquire();
System.out.println("Writing");
resourceAccess.release();
serviceQueue.release();
sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Writer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
class Reader extends Thread {
Semaphore resourceAccess;
Semaphore readCountAccess;
Semaphore serviceQueue;
int readCount;
public Reader(Semaphore resourceAccess, Semaphore readCountAccess, Semaphore serviceQueue, int readCount) {
super("Reader");
this.resourceAccess = resourceAccess;
this.readCountAccess = readCountAccess;
this.serviceQueue = serviceQueue;
this.readCount = readCount;
}
public void run() {
while (true) {
try {
serviceQueue.acquire();
readCountAccess.acquire();
readCount++;
if (readCount == 1) {
resourceAccess.acquire();
}
readCountAccess.release();
serviceQueue.release();
System.out.println("Reading");
readCountAccess.acquire();
readCount--;
if (readCount == 0) {
resourceAccess.release();
}
readCountAccess.release();
sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
```