Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bc1050b

Browse files
round robin cpu scheduling
1 parent c07bfdd commit bc1050b

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

‎os/roundrobin/CPU.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package os.roundrobin;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
*
7+
* @author rafiul islam
8+
*/
9+
public class CPU implements Runnable{
10+
11+
private ArrayList<Process> totalProcess;
12+
private boolean isIdle;
13+
private int timeSlot;
14+
private int currentlyRunning;
15+
16+
private CPU(int timeSlot){
17+
totalProcess = new ArrayList<>();
18+
isIdle = false;
19+
this.timeSlot = timeSlot;
20+
currentlyRunning = 0;
21+
}
22+
23+
public static CPU init(int timeSlot){
24+
return new CPU(timeSlot);
25+
}
26+
27+
public void newProcess(Process p){
28+
totalProcess.add(p);
29+
if(isIdle){
30+
isIdle = false;
31+
}
32+
currentlyRunning +=1;
33+
}
34+
35+
@Override
36+
public void run(){
37+
while(!isIdle){
38+
for(Process process : totalProcess){
39+
if(process.isExecuting()){
40+
process.execute(timeSlot);
41+
}
42+
else{
43+
currentlyRunning-=1;
44+
}
45+
if(currentlyRunning == 0){
46+
System.out.println("CPU IDLE");
47+
isIdle = true;
48+
break;
49+
}
50+
}
51+
}
52+
53+
for(Process process : totalProcess){
54+
System.out.println(process +" need "+process.getWaitingTime()
55+
+" millisec to executed");
56+
}
57+
}
58+
59+
60+
}

‎os/roundrobin/Main.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package os.roundrobin;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public class Main {
8+
public static void main(String[] args) {
9+
CPU cpu = CPU.init(2);
10+
11+
Process p1,p2,p3;
12+
p1 = Process.create(3, "Process 1");
13+
p2 = Process.create(1, "Process 2");
14+
p3 = Process.create(2, "Process 3");
15+
16+
cpu.newProcess(p1);
17+
cpu.newProcess(p2);
18+
cpu.newProcess(p3);
19+
20+
new Thread(cpu).start();
21+
22+
}
23+
}

‎os/roundrobin/Process.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package os.roundrobin;
2+
3+
import java.util.logging.Level;
4+
import java.util.logging.Logger;
5+
6+
/**
7+
*
8+
* @author rafiul islam
9+
*/
10+
public class Process {
11+
12+
private long arrivalTime,exitTime;
13+
private int neededTime,remainingTime,waitingTime;
14+
15+
private String name;
16+
17+
private Process(int neededTime, String name){
18+
this.neededTime = neededTime;
19+
this.name = name;
20+
remainingTime = neededTime;
21+
arrivalTime = System.currentTimeMillis();
22+
exitTime = -1;
23+
}
24+
25+
public static Process create(int neededTime, String name){
26+
return new Process(neededTime,name);
27+
}
28+
29+
public long getArrivalTime() {
30+
return arrivalTime;
31+
}
32+
33+
public long getExitTime() {
34+
return exitTime;
35+
}
36+
37+
public int getNeededTime() {
38+
return neededTime;
39+
}
40+
41+
public int getRemainingTime() {
42+
return remainingTime;
43+
}
44+
45+
public int getWaitingTime() {
46+
return waitingTime;
47+
}
48+
49+
public boolean isExecuting() {
50+
return remainingTime != 0;
51+
}
52+
53+
54+
public void execute(int timeSlot){
55+
Logger.getLogger("running: ").log(Level.SEVERE, name);
56+
57+
if(timeSlot < remainingTime){
58+
try{
59+
Thread.sleep(timeSlot*1000);
60+
}catch(InterruptedException ex){
61+
62+
}
63+
remainingTime -= timeSlot;
64+
}
65+
else{
66+
try{
67+
Thread.sleep(remainingTime*1000);
68+
}catch(InterruptedException ex){
69+
70+
}
71+
remainingTime = 0;
72+
exitTime = System.currentTimeMillis();
73+
waitingTime = (int) ((exitTime-arrivalTime));
74+
}
75+
}
76+
77+
public String toString(){
78+
return name;
79+
}
80+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /