public class Project1 {
static class MyArray<T> {
Object[] arr = new Object[1000];
int size = 0;
void add(T value) {
arr[size++] = value;
}
void remove(T value) {
for (int i = 0; i < size; i++) {
if (arr[i] == (value)) {
for (int j = i; j < size - 1; j++) {
arr[j] = arr[j + 1];
}
size--;
return;
}
}
}
@SuppressWarnings("unchecked")
T getIndex(int i) {
return (T) arr[i];
}
int size() {
return size;
}
boolean isEmpty() {
return size == 0;
}
}
static class Process {
int priority;
int ticks;
String name;
int ticksDone;
int signalsReceived;
Process(String name, int priority, int ticks, int ticksDone) {
this.name = name;
this.priority = priority;
this.ticks = ticks;
this.ticksDone = ticksDone;
this.signalsReceived = 0;
}
}
static class ProcessA extends Process {
ProcessA(String name) {
super(name, 2, 10, 0);
}
}
static class ProcessB extends Process {
ProcessB(String name) {
super(name, 3, 7, 0);
}
}
static class ProcessC extends Process {
ProcessC(String name) {
super(name, 1, 5, 0);
}
}
public static void main(String[] args) {
int timeQuantum = 4;
int currentTime = 0;
MyArray<Process> readyQueue = new MyArray<>();
MyArray<Process> ganttChart = new MyArray<>();
MyArray<Process> allProcesses = new MyArray<>();
MyArray<Integer> timeStamps = new MyArray<>();
timeStamps.add(currentTime);
ProcessA p1 = new ProcessA("P1");
ProcessB p2 = new ProcessB("P2");
readyQueue.add(p1);
readyQueue.add(p2);
allProcesses.add(p1);
allProcesses.add(p2);
int pid = 2;
while (!readyQueue.isEmpty()) {
Process highestPriority = null;
for (int i = 0; i < readyQueue.size(); i++) {
Process p = readyQueue.getIndex(i);
if (highestPriority == null || p.priority < highestPriority.priority) {
highestPriority = p;
}
}
int runTime;
if (timeQuantum < highestPriority.ticks) {
runTime = timeQuantum;
} else {
runTime = highestPriority.ticks;
}
for (int i = 1; i <= runTime; i++) {
highestPriority.ticks--;
highestPriority.ticksDone++;
if (currentTime % 3 == 0 && currentTime != 0) {
highestPriority.signalsReceived++;
}
currentTime++;
if (highestPriority.ticksDone % 3 == 0 && highestPriority.priority == 2) {
pid++;
ProcessB newPB = new ProcessB("P" + pid);
readyQueue.add(newPB);
allProcesses.add(newPB);
} else if (highestPriority.ticksDone % 3 == 0 && highestPriority.priority == 3) {
pid++;
ProcessC newPC = new ProcessC("P" + pid);
readyQueue.add(newPC);
allProcesses.add(newPC);
}
}
if (highestPriority.ticks <= 0) {
readyQueue.remove(highestPriority);
}
ganttChart.add(highestPriority);
timeStamps.add(currentTime);
}
int boxWidth = 6;
int totalBoxes = ganttChart.size();
for (int i = 0; i < totalBoxes; i++) {
System.out.print("+");
for (int j = 0; j < boxWidth; j++)
System.out.print("-");
}
System.out.println("+");
for (int i = 0; i < totalBoxes; i++) {
Process p = ganttChart.getIndex(i);
System.out.print("|");
int padding = boxWidth - p.name.length();
int padLeft = padding / 2;
int padRight = padding - padLeft;
for (int j = 0; j < padLeft; j++)
System.out.print(" ");
System.out.print(p.name);
for (int j = 0; j < padRight; j++)
System.out.print(" ");
}
System.out.println("|");
for (int i = 0; i < totalBoxes; i++) {
System.out.print("+");
for (int j = 0; j < boxWidth; j++)
System.out.print("-");
}
System.out.println("+");
for (int i = 0; i < timeStamps.size(); i++) {
System.out.print(timeStamps.getIndex(i));
int padding;
if (timeStamps.getIndex(i) > 9) {
padding = 5;
} else {
padding = 6;
}
for (int j = 0; j < padding; j++)
System.out.print(" ");
}
System.out.println();
System.out.println("\nTotal number of signals received by each individual process:");
for (int i = 0; i < allProcesses.size(); i++) {
Process p = allProcesses.getIndex(i);
System.out.println(p.name + " - " + p.signalsReceived);
}
}
}