-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.java
More file actions
145 lines (126 loc) · 4.01 KB
/
Simulation.java
File metadata and controls
145 lines (126 loc) · 4.01 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
package Simulation;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Rectangle{
int x, y, w, h;
}
class MyPanel extends JPanel implements ActionListener{
JButton button1;
JButton button2;
MainMemory mainMemory = new MainMemory();
Process process = new Process();
int array_num = 0;
int hole_num = 0;
int processNumber = 4;
int squareX, squareY;
int squareW, squareH;
Queue<Process> processQueue = new LinkedList<>();
//holes에 start, end, holesize 저장
ArrayList<Hole> holes = mainMemory.getHoleArrayList();
ArrayList<Process> processList = new ArrayList<>();
public MyPanel() {
setLayout(new FlowLayout());
button1 = new JButton("생성");
button2 = new JButton("삭제");
button1.addActionListener(this);
button2.addActionListener(this);
add(button1);
add(button2);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1) {
//생성버튼 클릭시 프로세스 4개생성 후 프로세스큐에 할당.
for(int i = 0; i<processNumber; i++) {
Process newProcess = new Process();
processQueue.add(newProcess);
}
//프로세스 큐가 빌때까지 적절한 위치에 프로세스 삽입.
while(!processQueue.isEmpty()) {
Process removedProcess = processQueue.poll();
if(holes.size() == 0) {
//외부단편화 발생
}
else {
for(int i = 0; i < holes.size(); i++) {
if(removedProcess.size < holes.get(i).getSize()) {
holes.get(i).setStartAddress(holes.get(i).getStartAddress() + removedProcess.size);
holes.get(i).setSize(holes.get(i).getSize() + removedProcess.size);
//시작주소
removedProcess.setY(holes.get(i).getStartAddress());
//끝 주소
removedProcess.setH(removedProcess.getY() + removedProcess.size);
processList.add(removedProcess);
repaint();
}
else if(removedProcess.size == holes.get(i).getSize()) {
holes.remove(i);
removedProcess.setY(holes.get(i).getStartAddress());
removedProcess.setH(holes.get(i).getEndAddress());
processList.add(removedProcess);
repaint();
}
else if(removedProcess.size > holes.get(i).getSize()) {
continue;
}
}
}
}
}
if(e.getSource() == button2) {
Random random = new Random();
//나중에 2를 변수로 바꿔야됨 magicNumber위험
for(int i = 0; i < 2; i++) {
int removeIndex = random.nextInt(processList.size());
Process removedProcess1 = processList.get(removeIndex);
int start, end;
start = removedProcess1.startAddress;
end = removedProcess1.endAddress;
Hole newHole = new Hole(start, end);
holes.add(newHole);
processList.remove(removeIndex);
}
repaint();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
//mainMemory 그리기
squareX = mainMemory.mainSquareX;
squareY = mainMemory.mainSquareY;
squareW = mainMemory.mainSquareW;
squareH = mainMemory.mainSquareH;
g.drawRect(squareX, squareY, squareW, squareH);
for(Process process1 : processList) {
int processX = process1.getX();
int processY = process1.getY();
int processW = process1.getW();
int processH = process1.getH();
Color randomColor = new Color((int) (Math.random() * 0x1000000));
g.setColor(randomColor);
g.fillRect(processX, processY, processW, processH);
g.drawRect(processX, processY, processW, processH);
}
}
}
public class Simulation extends JFrame {
public Simulation() {
setSize(300, 300);
setTitle("램 시뮬레이션");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new MyPanel());
setVisible(true);
}
public static void main(String[] args) {
Simulation r = new Simulation();
}
}