-
Notifications
You must be signed in to change notification settings - Fork 2
/
SJF.java
155 lines (144 loc) · 3.87 KB
/
SJF.java
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
import javax.swing.*;
import java.awt.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
public class SJF extends Algorithms
{
private Process root;
private int size;
private JTextArea area;
private int Throughput;
private JButton button;
private JPanel jPanel;
private ArrayList<Button> list;
SJF(JTextArea textArea,JButton jButton,JPanel panel){
root=null;
size=0;
this.area=textArea;
Throughput=0;
this.button=jButton;
this.jPanel=panel;
jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
list=new ArrayList<>();
}
@Override
public void clear() {
root=null;
size=0;
Throughput=0;
area.setText("");
button.setText("ThroughPut:- 0");
for(Button b:list){
jPanel.remove(b);
}
list.clear();
}
@Override
public void insert(Process node) {
area.append("ID:- "+String.valueOf(node.Id)+" Time:- "+String.valueOf(node.TimeRemaining)+" Priority:- "+String.valueOf(node.Priority)+"\n");
root = merge(node, root);
Throughput+=node.Time;
size++;
button.setText("ThroughPut:- "+String.valueOf(throughPut()));
}
@Override
public void update() {
root.TimeRemaining-=Main.BurstType;
addGantt();
try{
FileWriter fstream = new FileWriter(MasterCpu.Files.get(root.Id)+".txt",true);
BufferedWriter fbw = new BufferedWriter(fstream);
fbw.write(Main.append);
fbw.newLine();
fbw.close();
}catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
if(root.TimeRemaining<=0)
{
Throughput-=root.Time;
delete();
size--;
button.setText("ThroughPut:- "+String.valueOf(throughPut()));
}
}
@Override
public void addGantt() {
Button button=new Button(""+root.Id);
button.setBackground(root.color);
jPanel.add(button);
list.add(button);
if(list.size()>10){
Button button1=list.get(0);
list.remove(0);
jPanel.remove(button1);
}
}
@Override
public Process getRoot()
{
return root;
}
private void delete()
{
if (root==null)
return;
root = merge(root.left, root.right);
area.setText("");
BFS(root);
}
private void BFS(Process node)
{
if(node!=null)
{
area.append("ID:- "+String.valueOf(node.Id)+" Time:- "+String.valueOf(node.TimeRemaining)+" Priority:- "+String.valueOf(node.Priority)+"\n");
BFS(node.left);
BFS(node.right);
}
}
private Process merge(Process Left, Process Right)
{
if (Left == null)
return Right;
if (Right == null)
return Left;
if (Left.TimeRemaining > Right.TimeRemaining)
{
Process temp = Left;
Left = Right;
Right = temp;
}
Left.right = merge(Left.right, Right);
if(Left.left == null)
{
Left.left = Left.right;
Left.right = null;
}
else
{
if(Left.left.sValue < Left.right.sValue)
{
Process temp = Left.left;
Left.left = Left.right;
Left.right = temp;
}
Left.sValue = Left.right.sValue + 1;
}
return Left;
}
@Override
public float throughPut() {
if(size==0)return 0;
return (float)Throughput/size;
}
@Override
public void mergeProcess(Process second) {
root= merge(root,second);
}
@Override
public void addNewProcess() {
area.setText("");
BFS(root);
}
}