-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessSchedulerGUI.java
More file actions
200 lines (174 loc) · 9.62 KB
/
ProcessSchedulerGUI.java
File metadata and controls
200 lines (174 loc) · 9.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class ProcessSchedulerGUI extends JFrame {
private JTextField processCountField;
private JButton startButton;
private JPanel inputPanel;
private List<Process> processes;
public ProcessSchedulerGUI() {
setTitle("Process Scheduling Simulator");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Input Panel
inputPanel = new JPanel(new GridLayout(0, 2, 5, 5));
inputPanel.add(new JLabel("Number of Processes (3-10):"));
processCountField = new JTextField(5);
inputPanel.add(processCountField);
add(inputPanel, BorderLayout.CENTER);
// Start Button
startButton = new JButton("Start");
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int processCount = Integer.parseInt(processCountField.getText());
if (processCount < 3 || processCount > 10) {
JOptionPane.showMessageDialog(null, "Number of processes must be between 3 and 10.");
return;
}
processes = new ArrayList<>();
int timeQuantum = Integer.parseInt(JOptionPane.showInputDialog("Enter Time Quantum for RR:"));
for (int i = 0; i < processCount; i++) {
String arrival = JOptionPane.showInputDialog("Enter Arrival Time for Process " + (i + 1) + ":");
String burst = JOptionPane.showInputDialog("Enter Burst Time for Process " + (i + 1) + ":");
String priority = JOptionPane.showInputDialog("Enter Priority for Process " + (i + 1) + ":");
processes.add(new Process(i, Integer.parseInt(arrival), Integer.parseInt(burst), Integer.parseInt(priority)));
}
showSRTResults();
showSJNResults();
showNPPResults();
showRRResults(timeQuantum);
}
});
add(startButton, BorderLayout.SOUTH);
}
private void showSRTResults() {
// Create a new window to display SRT results
JFrame resultFrame = new JFrame("Shorstest Remaining Time Scheduling Results");
resultFrame.setSize(800, 600);
resultFrame.setLayout(new BorderLayout());
// Run SRT algorithm
List<String> ganttChart = ShortestRemainingTime.schedule(new ArrayList<>(processes));
// Display Gantt Chart
GanttChartPanel ganttChartPanel = new GanttChartPanel(ganttChart);
resultFrame.add(ganttChartPanel, BorderLayout.CENTER);
// Display Process Table and Averages
JTextArea tableOutput = new JTextArea();
tableOutput.setEditable(false);
tableOutput.append("Process Table:\nPID\tBurst\tArrival\tFinish\tTurnaround\tWaiting\n");
double totalTurnaround = 0;
double totalWaiting = 0;
for (Process p : processes) {
tableOutput.append(p.id + "\t" + p.burstTime + "\t" + p.arrivalTime + "\t" + p.finishTime + "\t" + p.turnaroundTime + "\t" + p.waitingTime + "\n");
totalTurnaround += p.turnaroundTime;
totalWaiting += p.waitingTime;
}
double avgTurnaround = totalTurnaround / processes.size();
double avgWaiting = totalWaiting / processes.size();
tableOutput.append("\nTotal Turnaround Time: " + String.format("%.2f", totalTurnaround)+"ms" + "\n");
tableOutput.append("Total Waiting Time: " + String.format("%.2f", totalWaiting)+"ms" + "\n");
tableOutput.append("Average Turnaround Time: " + String.format("%.2f", avgTurnaround)+"ms" + "\n");
tableOutput.append("Average Waiting Time: " + String.format("%.2f", avgWaiting)+"ms" + "\n");
resultFrame.add(new JScrollPane(tableOutput), BorderLayout.SOUTH);
resultFrame.setVisible(true);
}
private void showSJNResults() {
// Create a new window to display SJNresults
JFrame resultFrame = new JFrame("Shortest Job Next Scheduling Results");
resultFrame.setSize(800, 600);
resultFrame.setLayout(new BorderLayout());
// Run SJN algorithm
List<String> ganttChart = ShortestJobNext.schedule(new ArrayList<>(processes));
// Display Gantt Chart
GanttChartPanel ganttChartPanel = new GanttChartPanel(ganttChart);
resultFrame.add(ganttChartPanel, BorderLayout.CENTER);
// Display Process Table and Averages
JTextArea tableOutput = new JTextArea();
tableOutput.setEditable(false);
tableOutput.append("Process Table:\nPID\tBurst\tArrival\tFinish\tTurnaround\tWaiting\n");
double totalTurnaround = 0;
double totalWaiting = 0;
for (Process p : processes) {
tableOutput.append(p.id + "\t" + p.burstTime + "\t" + p.arrivalTime + "\t" + p.finishTime + "\t" + p.turnaroundTime + "\t" + p.waitingTime + "\n");
totalTurnaround += p.turnaroundTime;
totalWaiting += p.waitingTime;
}
double avgTurnaround = totalTurnaround / processes.size();
double avgWaiting = totalWaiting / processes.size();
tableOutput.append("\nTotal Turnaround Time: " + String.format("%.2f", totalTurnaround)+"ms" + "\n");
tableOutput.append("Total Waiting Time: " + String.format("%.2f", totalWaiting)+"ms" + "\n");
tableOutput.append("Average Turnaround Time: " + String.format("%.2f", avgTurnaround)+"ms" + "\n");
tableOutput.append("Average Waiting Time: " + String.format("%.2f", avgWaiting)+"ms" + "\n");
resultFrame.add(new JScrollPane(tableOutput), BorderLayout.SOUTH);
resultFrame.setVisible(true);
}
private void showNPPResults() {
// Create a new window to display NPPresults
JFrame resultFrame = new JFrame("Non-Preemptive Priority Scheduling Results");
resultFrame.setSize(800, 600);
resultFrame.setLayout(new BorderLayout());
// Run NPP algorithm
List<String> ganttChart = NonPreemptivePriority.schedule(new ArrayList<>(processes));
// Display Gantt Chart
GanttChartPanel ganttChartPanel = new GanttChartPanel(ganttChart);
resultFrame.add(ganttChartPanel, BorderLayout.CENTER);
// Display Process Table and Averages
JTextArea tableOutput = new JTextArea();
tableOutput.setEditable(false);
tableOutput.append("Process Table:\nPID\tBurst\tPriority\tArrival\tFinish\tTurnaround\tWaiting\n");
double totalTurnaround = 0;
double totalWaiting = 0;
for (Process p : processes) {
tableOutput.append(p.id + "\t" + p.burstTime+ "\t" + p.priority + "\t" + p.arrivalTime + "\t" + p.finishTime + "\t" + p.turnaroundTime + "\t" + p.waitingTime + "\n");
totalTurnaround += p.turnaroundTime;
totalWaiting += p.waitingTime;
}
double avgTurnaround = totalTurnaround / processes.size();
double avgWaiting = totalWaiting / processes.size();
tableOutput.append("\nTotal Turnaround Time: " + String.format("%.2f", totalTurnaround)+"ms" + "\n");
tableOutput.append("Total Waiting Time: " + String.format("%.2f", totalWaiting)+"ms" + "\n");
tableOutput.append("Average Turnaround Time: " + String.format("%.2f", avgTurnaround)+"ms" + "\n");
tableOutput.append("Average Waiting Time: " + String.format("%.2f", avgWaiting)+"ms" + "\n");
resultFrame.add(new JScrollPane(tableOutput), BorderLayout.SOUTH);
resultFrame.setVisible(true);
}
private void showRRResults(int timeQuantum) {
// Create a new window to display RRresults
JFrame resultFrame = new JFrame("Round Robin Scheduling Results");
resultFrame.setSize(800, 600);
resultFrame.setLayout(new BorderLayout());
// Run RR algorithm
List<String> ganttChart = RoundRobin.schedule(new ArrayList<>(processes), timeQuantum);
// Display Gantt Chart
GanttChartPanel ganttChartPanel = new GanttChartPanel(ganttChart);
resultFrame.add(ganttChartPanel, BorderLayout.CENTER);
// Display Process Table and Averages
JTextArea tableOutput = new JTextArea();
tableOutput.setEditable(false);
tableOutput.append("Process Table:\nPID\tBurst\tPriority\tArrival\tFinish\tTurnaround\tWaiting\n");
double totalTurnaround = 0;
double totalWaiting = 0;
for (Process p : processes) {
tableOutput.append(p.id + "\t" + p.burstTime+ "\t" + p.priority + "\t" + p.arrivalTime + "\t" + p.finishTime + "\t" + p.turnaroundTime + "\t" + p.waitingTime + "\n");
totalTurnaround += p.turnaroundTime;
totalWaiting += p.waitingTime;
}
double avgTurnaround = totalTurnaround / processes.size();
double avgWaiting = totalWaiting / processes.size();
tableOutput.append("\nTotal Turnaround Time: " + String.format("%.2f", totalTurnaround)+"ms" + "\n");
tableOutput.append("Total Waiting Time: " + String.format("%.2f", totalWaiting)+"ms" + "\n");
tableOutput.append("Average Turnaround Time: " + String.format("%.2f", avgTurnaround)+"ms" + "\n");
tableOutput.append("Average Waiting Time: " + String.format("%.2f", avgWaiting)+"ms" + "\n");
resultFrame.add(new JScrollPane(tableOutput), BorderLayout.SOUTH);
resultFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new ProcessSchedulerGUI().setVisible(true);
});
}
}