Last active
November 28, 2015 02:43
-
-
Save nawb/d6be2df8cce5b4b771bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void checkHoldingQueue() { | |
// Check for procs in holding that have matured to priority of the ones in accepted | |
// In essence we should only check the largest proc in holding queue, but since we're not sorting it... | |
for (int i = 0; i < devices; i ++){ | |
if (holding[i] == accepted[i]) { | |
accepted[i] == holding[i]; //put it in accepted queue | |
holding[i] == -inf; //take it out of holding queue | |
} | |
} | |
} | |
/* BENEFIT OF SRR IS THAT PROCESSES DO NOT TAKE TOO LONG TO COMPLETE */ | |
void selfishRoundRobin(int timeSlice) //in seconds | |
{ | |
//fixed speeds | |
int holding_rate = 3; | |
int accepted_rate = 2; | |
int[] accepted = {0,0,0,0}; //accepted queue | |
int[] holding = {0,0,0,0}; //holding queue | |
init(0); | |
reOrderPorts(); | |
println("Init Selfish Round Robin Algorithm"); | |
int start_exec = 0; | |
int preempted = 0; | |
g_startTime = millis(); | |
while(ready[0] == true || ready[1] == true || ready[2] == true || ready[3] == true) | |
{ | |
int k=0; | |
for(int i=0;i<devices;i++) | |
{ | |
if(k==devices) | |
{ | |
k=0; | |
} | |
checkHoldingQueue(); | |
checkAcceptedQueue(k); //this should be ready queue | |
//checkReadyQueue(k); | |
if(ready[i]==true && hasArrived[i]==true) | |
{ | |
myPort[i].write('g'); | |
println("Process "+(i+1)+" is running..."); | |
start_exec = currentTime(); | |
//delay(1000); | |
while(true) | |
{ | |
delay(1000); | |
String a =myPort[i].readStringUntil('e'); | |
if(a!=null) | |
{ | |
//INCREMENT THE WAITING TIMES | |
for (j=0; j<devices; j++){ | |
accepted[j] += accepted_rate; | |
holding[j] += holding_rate; | |
} | |
//HANDLE PROCESS FINISHING | |
if(a.equals("e")) | |
{ | |
ready[i] = false; | |
println("Process "+(i+1)+" has ended."); | |
println("Current Time: "+ currentTime()); | |
preempted = currentTime(); | |
break; | |
} | |
} | |
//HANDLE END OF PROCESS TIMESLICE | |
else if((currentTime() - start_exec >= timeSlice)) | |
{ | |
myPort[i].write('s'); | |
println("Process "+(i+1)+" is Preempted..."); | |
println("Current Time: "+ currentTime()); | |
preempted = currentTime(); | |
break; | |
} | |
} | |
insertTime(i,start_exec,preempted); | |
} | |
k++; | |
} | |
} | |
println("A Linked List"); | |
for(int i=0;i<A.size();i++) | |
{ | |
print(A.get(i).start_time+ " "); | |
println(A.get(i).end_time); | |
} | |
println("B Linked List"); | |
for(int i=0;i<B.size();i++) | |
{ | |
print(B.get(i).start_time+ " "); | |
println(B.get(i).end_time); | |
} | |
println("C Linked List"); | |
for(int i=0;i<C.size();i++) | |
{ | |
print(C.get(i).start_time+ " "); | |
println(C.get(i).end_time); | |
} | |
stopAllConnections(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment