daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest Jul 11th, 2018 58 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  1. #include<iostream>
  2. #include<fstream>
  3. #include<deque>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. //input
  9. /*
  10. 6 2 //totalProcess timeQuantum
  11. 0 4
  12. 1 5
  13. 2 2
  14. 3 1
  15. 4 6
  16. 6 3
  17. */
  18.  
  19. int main(){
  20.     int totalProcess, timeQuantum;
  21.  
  22.     //input from file
  23.     ifstream cin("input_4.txt");
  24.     cin>>totalProcess>>timeQuantum;
  25.  
  26.     int arrivalTime[totalProcess];
  27.     int burstTime[totalProcess];
  28.     int scheduler[totalProcess*2];
  29.     deque<int> processQueue;
  30.  
  31.     for(int i=0;i<totalProcess;i++){
  32.         cin>>arrivalTime[i]>>burstTime[i];
  33.     }
  34.  
  35.     //initialize queue
  36.     int currentProcess = 0;
  37.     processQueue.push_back(currentProcess);
  38.  
  39.     int index = 0;
  40.     int currentTime = 0;
  41.     int pIndex = 0;
  42.  
  43.     while(!processQueue.empty()){
  44.         //add into scheduler
  45.         scheduler[index] = processQueue.front();
  46.         currentProcess = scheduler[index];
  47.         processQueue.pop_front();
  48.         if(burstTime[currentProcess] > timeQuantum){
  49.             burstTime[currentProcess] -= timeQuantum;
  50.             currentTime += timeQuantum;
  51.         } else {
  52.             currentTime += burstTime[currentProcess];
  53.             burstTime[currentProcess] = 0;
  54.         }
  55.  
  56.         for(int i=currentProcess + 1;i<totalProcess;i++){
  57.             int inScheduler = 0;
  58.             for(int j=0;j<index;j++){
  59.                 if(scheduler[j] == i){
  60.                     inScheduler = 1;
  61.                 }
  62.             }
  63.             if(arrivalTime[i] <= currentTime && inScheduler == 0 && find(processQueue.begin(), processQueue.end(), i) == processQueue.end()){
  64.                 //cout<<"push "<<i<<endl;
  65.                 processQueue.push_back(i);
  66.             }
  67.         }
  68.  
  69.         if(burstTime[currentProcess] > 0){
  70.             processQueue.push_back(currentProcess);
  71.         }
  72.  
  73.         index++;
  74.     }
  75.  
  76.  
  77.     for(int i=0;i<index;i++){
  78.         cout<<"P:"<<scheduler[i]+1<<endl;
  79.     }
  80.  
  81.     /*
  82.     for(int i=0;i<totalProcess;i++){
  83.         cout<<burstTime[i]<<endl;
  84.     }
  85.     */
  86.  
  87.  
  88.     //test print
  89.     /*
  90.     for(int i=0;i<totalProcess;i++){
  91.         cout<<arrivalTime[i]<<" "<<burstTime[i]<<endl;
  92.     }
  93.     */
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top