-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSJF.java
More file actions
160 lines (129 loc) · 5.43 KB
/
SJF.java
File metadata and controls
160 lines (129 loc) · 5.43 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
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
public class SJF {
static class Process {
int pid;
int at; // Arrival Time
int bt; // Burst Time
int ct; // Completion Time
int tat; // Turn Around Time
int wt; // Waiting Time
int remainingBt; // Remaining Burst Time for preemptive SJF
public Process(int pid, int at, int bt) {
this.pid = pid;
this.at = at;
this.bt = bt;
this.remainingBt = bt;
}
}
public static void main(String[] args) {
try {
String[] options = {"SJF Preemptive", "SJF Non-Preemptive"};
int choice = JOptionPane.showOptionDialog(
null,
"Choose the scheduling algorithm:",
"Scheduling Algorithm Selection",
JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
ArrayList<Process> processes = getUserInputForProcesses();
if (choice == 0) {
calculateTimesSJFPreemptive(processes);
} else {
calculateTimesSJFNonPreemptive(processes);
}
displayResultsInTable(processes);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid input. Please enter valid numbers.");
}
}
static ArrayList<Process> getUserInputForProcesses() {
ArrayList<Process> processes = new ArrayList<>();
try {
int n = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of processes:"));
for (int i = 0; i < n; i++) {
int pid = i + 1;
int at = Integer.parseInt(JOptionPane.showInputDialog("Enter Arrival Time for Process " + pid + ":"));
int bt = Integer.parseInt(JOptionPane.showInputDialog("Enter Burst Time for Process " + pid + ":"));
processes.add(new Process(pid, at, bt));
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid input. Please enter valid numbers.");
}
return processes;
}
static void calculateTimesSJFPreemptive(ArrayList<Process> processes) {
Collections.sort(processes, Comparator.comparingInt(p -> p.at));
boolean[] completed = new boolean[processes.size()];
int currentTime = 0;
int completedCount = 0;
while (completedCount != processes.size()) {
int shortest = -1;
int shortestBurst = Integer.MAX_VALUE;
for (int i = 0; i < processes.size(); i++) {
if (!completed[i] && processes.get(i).at <= currentTime && processes.get(i).remainingBt < shortestBurst) {
shortestBurst = processes.get(i).remainingBt;
shortest = i;
}
}
if (shortest == -1) {
currentTime++;
} else {
processes.get(shortest).remainingBt--;
if (processes.get(shortest).remainingBt == 0) {
processes.get(shortest).ct = currentTime + 1;
processes.get(shortest).tat = processes.get(shortest).ct - processes.get(shortest).at;
processes.get(shortest).wt = processes.get(shortest).tat - processes.get(shortest).bt;
completed[shortest] = true;
completedCount++;
}
currentTime++;
}
}
}
static void calculateTimesSJFNonPreemptive(ArrayList<Process> processes) {
processes.sort(Comparator.comparingInt(p -> p.at));
int currentTime = 0;
for (Process process : processes) {
if (process.at > currentTime) {
currentTime = process.at;
}
process.ct = currentTime + process.bt;
process.tat = process.ct - process.at;
process.wt = process.tat - process.bt;
currentTime = process.ct;
}
}
static void displayResultsInTable(ArrayList<Process> processes) {
JFrame frame = new JFrame("SJF Example");
String[] column = {"ProcessID", "Arrival Time", "Burst Time", "Completion Time", "Turn Around Time", "Waiting Time"};
DefaultTableModel model = new DefaultTableModel(null, column);
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
for (Process p : processes) {
model.addRow(new Object[]{p.pid, p.at, p.bt, p.ct, p.tat, p.wt});
}
JButton backButton = new JButton("Back");
backButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
main(null); // Restart the program to get new process details
}
});
JPanel panel = new JPanel();
panel.add(backButton);
frame.add(scrollPane);
frame.add(panel);
frame.setSize(600, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.setVisible(true);
}
}