-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFIFO.java
121 lines (110 loc) · 3.42 KB
/
FIFO.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
import javax.swing.*;
import java.awt.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
public class FIFO extends Algorithms
{
private ArrayList<Process> root;
private JTextArea area;
private int Throughput;
private JButton button;
private JPanel jPanel;
private ArrayList<Button> list;
FIFO(JTextArea textArea,JButton jButton,JPanel panel){
root=new ArrayList<>();
this.button=jButton;
this.area=textArea;
this.jPanel=panel;
area.insert("",0);
Throughput=0;
jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
list=new ArrayList<>();
}
@Override
public void clear() {
root.clear();
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.add(node);
Throughput+=node.Time;
button.setText("ThroughPut:- "+String.valueOf(throughPut()));
}
@Override
public void update()
{
if(!root.isEmpty())
{
Process process = root.get(0);
addGantt();
process.TimeRemaining -= Main.BurstType;
if (process.TimeRemaining <= 0)
{
Throughput-=process.Time;
root.remove(0);
area.setText("");
for (Process node : root)
{
area.append("ID:- " + String.valueOf(node.Id) + " Time:- " + String.valueOf(node.TimeRemaining) + " Priority:- " + String.valueOf(node.Priority) + "\n");
}
button.setText("ThroughPut:- "+String.valueOf(throughPut()));
}
try{
FileWriter fstream = new FileWriter(MasterCpu.Files.get(process.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());
}
}
}
@Override
public void addGantt() {
Button button=new Button(""+root.get(0).Id);
button.setBackground(root.get(0).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() {
if(!root.isEmpty())
return root.get(0);
else return null;
}
@Override
public float throughPut() {
if(root.size()==0)return 0;
return (float)Throughput/root.size();
}
@Override
public void mergeProcess(ArrayList<Process> second) {
root.addAll(second);
}
@Override
public ArrayList<Process> getParent() {
return root;
}
@Override
public void addNewProcess() {
area.setText("");
for (Process node : root) {
area.append("ID:- " + String.valueOf(node.Id) + " Time:- " + String.valueOf(node.TimeRemaining) + " Priority:- " + String.valueOf(node.Priority) + "\n");
}
}
}