Created
July 11, 2018 23:23
-
-
Save abrarShariar/1126728fefa5c1d2e4501ae553abaf99 to your computer and use it in GitHub Desktop.
Round Robin Scheduling (pre-emptive)
This file contains hidden or 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
#include<iostream> | |
#include<fstream> | |
#include<deque> | |
#include <algorithm> | |
using namespace std; | |
//input | |
/* | |
6 2 //totalProcess timeQuantum | |
0 4 | |
1 5 | |
2 2 | |
3 1 | |
4 6 | |
6 3 | |
*/ | |
int main(){ | |
int totalProcess, timeQuantum; | |
//input from file | |
ifstream cin("input_4.txt"); | |
cin>>totalProcess>>timeQuantum; | |
int arrivalTime[totalProcess]; | |
int burstTime[totalProcess]; | |
int scheduler[totalProcess*2]; | |
deque<int> processQueue; | |
for(int i=0;i<totalProcess;i++){ | |
cin>>arrivalTime[i]>>burstTime[i]; | |
} | |
//initialize queue | |
int currentProcess = 0; | |
processQueue.push_back(currentProcess); | |
int index = 0; | |
int currentTime = 0; | |
int pIndex = 0; | |
while(!processQueue.empty()){ | |
//add into scheduler | |
scheduler[index] = processQueue.front(); | |
currentProcess = scheduler[index]; | |
processQueue.pop_front(); | |
if(burstTime[currentProcess] > timeQuantum){ | |
burstTime[currentProcess] -= timeQuantum; | |
currentTime += timeQuantum; | |
} else { | |
currentTime += burstTime[currentProcess]; | |
burstTime[currentProcess] = 0; | |
} | |
for(int i=currentProcess + 1;i<totalProcess;i++){ | |
int inScheduler = 0; | |
for(int j=0;j<index;j++){ | |
if(scheduler[j] == i){ | |
inScheduler = 1; | |
} | |
} | |
if(arrivalTime[i] <= currentTime && inScheduler == 0 && find(processQueue.begin(), processQueue.end(), i) == processQueue.end()){ | |
//cout<<"push "<<i<<endl; | |
processQueue.push_back(i); | |
} | |
} | |
if(burstTime[currentProcess] > 0){ | |
processQueue.push_back(currentProcess); | |
} | |
index++; | |
} | |
for(int i=0;i<index;i++){ | |
cout<<"P:"<<scheduler[i]+1<<endl; | |
} | |
/* | |
for(int i=0;i<totalProcess;i++){ | |
cout<<burstTime[i]<<endl; | |
} | |
*/ | |
//test print | |
/* | |
for(int i=0;i<totalProcess;i++){ | |
cout<<arrivalTime[i]<<" "<<burstTime[i]<<endl; | |
} | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment