public class Job {
int id;
int size;
public Job(int id, int size){
this.id = id;
this.size = size;
}
public int getId() {
return id;
}
public int getSize() {
return size;
}
}
1. FIFO
First In First Out
먼저 들어온 것이 먼저 나간다.
Queue 자료구조를 이용한다.
public class FIFO {
Queue<Job> queue;
public FIFO(){
queue = new LinkedList<>();
}
public void push(Job job){
queue.add(job);
}
public void run(){
System.out.println("-- FIFO run --");
while(!queue.isEmpty()){
Job job = queue.poll();
System.out.println("id: " + job.getId() + ", size: " + job.getSize());
}
System.out.println("-- FIFO end --");
}
}
FIFO Test
class FIFOTest {
@Test
void run() {
FIFO fifo = new FIFO();
for (int i = 0; i < 5; i ++){
fifo.push(new Job(i, 10 - i));
}
fifo.run();
}
}
2. LIFO
Last In First Out
나중에 들어온 것이 먼저 나간다.
Stack 자료구조를 이용한다.
public class LIFO {
Stack<Job> stack;
public LIFO(){
stack = new Stack<>();
}
public void push(Job job){
stack.add(job);
}
public void run(){
System.out.println("-- LIFO run --");
while(!stack.isEmpty()){
Job job = stack.pop();
System.out.println("id: " + job.getId() + ", size: " + job.getSize());
}
System.out.println("-- LIFO end --");
}
}
LIFO Test
class LIFOTest {
@Test
void run() {
LIFO lifo = new LIFO();
for (int i = 0; i < 5; i ++){
lifo.push(new Job(i, 10 - i));
}
lifo.run();
}
}
3. RR
Round Robin
우선순위를 두지 않고 순서대로 시간단위로 CPU를 할당하는 방식이다.
시간 단위동안 수행한 프로세스는 준비 큐의 끝으로 밀려나게 된다.
할당되는 시간이 클 경우 비선점 FIFO기법과 같다.
public class RR {
Queue<Job> queue;
int part;
public RR(int part){
queue = new LinkedList<Job>();
this.part = part;
}
public void push(Job job){
queue.add(job);
}
public void run(){
System.out.println("-- RR run --");
while(!queue.isEmpty()){
Job job = queue.poll();
int id = job.getId();
int size = job.getSize();
int resize = size - part;
System.out.println("id: " + id + ", size: " + size + " -> size: " + resize);
if (resize > 0){
queue.add(new Job(id, resize));
}
}
System.out.println("-- RR end --");
}
}
RR Test
class RRTest {
@Test
void run() {
RR rr = new RR(4);
for (int i = 0; i < 5; i ++){
rr.push(new Job(i, (int)(Math.random()*10)));
}
rr.run();
}
}