Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void SJFSolution2()
- {
- ///Shortest Job First with preemption, mainly depends on CPU burst time
- ///variables
- int finishT[n];
- int actualArrivalT[n];
- int CPUrunT=0;
- int CPUidleT=0;
- int numProDone=0;
- int countV=0;
- int countN[n]={0};//used for counting the actual arrival time, to calculate the response time of each process
- ///make a copy of process pro, array
- for(unsigned int i=0;i<n;i++)
- {
- tempPro[i].processID=pro[i].processID;
- tempPro[i].arrivalT=pro[i].arrivalT;
- tempPro[i].CPUburstT=pro[i].CPUburstT;
- tempPro[i].ppriority=pro[i].ppriority;
- }
- while(numProDone<n)//checking if all the processes have done, if not, increment by 1.
- {
- ///Ready Queue Processes
- for(unsigned int i=0;i<n;i++)//get all the possible processes in ready queue, not include any process that has the CPUburstT of 0 or the remainingT of 0
- {
- if ((tempPro[i].arrivalT<=CPUrunT)&&(tempPro[i].CPUburstT>0))
- {
- tempProReadyQ.push_back(process());//creating a copy of ready queue processes, vector
- tempProReadyQ[countV].processID=tempPro[i].processID;
- tempProReadyQ[countV].arrivalT=tempPro[i].arrivalT;
- tempProReadyQ[countV].CPUburstT=tempPro[i].CPUburstT;
- tempProReadyQ[countV].ppriority=tempPro[i].ppriority;
- countV++;
- }
- }
- countV=0;
- if(tempProReadyQ.empty())//if ready queue is empty and no process is brought in, then it means the CPU is idle
- {
- CPUidleT++;
- CPUrunT++;//anything before this count from 0, anything else below this count from 1
- }
- else if (!tempProReadyQ.empty())//else ready queue has processes
- {
- if (tempProReadyQ.size()==1)//if there is only one process in the ready queue
- {
- tempPro[tempProReadyQ[0].processID-1].CPUburstT--;
- countN[tempProReadyQ[0].processID-1]++;
- if(countN[tempProReadyQ[0].processID-1]==1)
- {
- actualArrivalT[tempProReadyQ[0].processID-1]=CPUrunT;//only needs the 1st actual arrival time for each process
- }
- if(tempPro[tempProReadyQ[0].processID-1].CPUburstT==0)
- {
- finishT[tempProReadyQ[0].processID-1]=CPUrunT+1;
- numProDone++;
- }
- CPUrunT++;
- }
- else//there are more than one process in the ready queue
- {
- int minEle=INT_MAX;
- int tempProcessIndex=0;
- for(unsigned i=0; i<tempProReadyQ.size();i++)
- {
- if(tempProReadyQ[i].CPUburstT<minEle)
- {
- minEle=tempProReadyQ[i].CPUburstT;//smallest process ID
- tempProcessIndex=i;
- }
- }
- //sort(tempProReadyQ.begin(),tempProReadyQ.end(),checkbtvector);//sort the process based on their CPU burst time, smallest CPUburst time is the first element
- for(unsigned int i=0;i<tempProReadyQ.size();i++)//to collect the processes ID that has the same shortest CPUburstT
- {
- if(tempProReadyQ[i].CPUburstT==minEle)
- //if(tempProReadyQ[i].CPUburstT==tempProReadyQ[0].CPUburstT)//check if there are more than one process that has the same shortest CPUburstT
- {
- tempMin.push_back(tempProReadyQ[i].processID);//it stores process IDs
- }
- }
- if(!tempMin.empty())//there are processes with the same CPUburstT
- {
- int minEle=INT_MAX;
- for(unsigned i=0; i<tempMin.size();i++)
- {
- if(tempMin[i]<minEle)
- {
- minEle=tempMin[i];//smallest process ID
- }
- }
- tempPro[minEle-1].CPUburstT--;
- countN[minEle-1]++;
- if(countN[minEle-1]==1)
- {
- actualArrivalT[minEle-1]=CPUrunT;//only needs the 1st actual arrival time for each process
- }
- if(tempPro[minEle-1].CPUburstT==0)
- {
- finishT[minEle-1]=CPUrunT+1;
- numProDone++;
- //tempProReadyQ.erase(tempProReadyQ.begin()+(tempMin[0]-1));
- }
- CPUrunT++;
- }
- else if(tempMin.empty())//no processes with the same CPUburstT
- {
- tempPro[tempProcessIndex].CPUburstT--;
- countN[tempProcessIndex]++;
- if(countN[tempProcessIndex]==1)
- {
- actualArrivalT[tempProcessIndex]=CPUrunT;//only needs the 1st actual arrival time for each process
- }
- if(tempPro[tempProcessIndex].CPUburstT==0)
- {
- finishT[tempProcessIndex]=CPUrunT+1;
- numProDone++;
- }
- CPUrunT++;
- }
- tempMin.clear();
- }
- }
- tempProReadyQ.clear();//used it when it vector
- //while(!tempProReadyQ.empty())
- //{
- // tempProReadyQ.pop_back();
- //}
- tempProReadyQ.shrink_to_fit();
- }
- void SJFSolution3()
- {
- ///Shortest Job First with preemption, mainly depends on CPU burst time
- ///variables
- int finishT[n];
- int actualArrivalT[n];
- int CPUrunT=0;
- int CPUidleT=0;
- int numProDone=0;
- int countV=0;
- int countN[n]={0};//used for counting the actual arrival time, to calculate the response time of each process
- ///make a copy of process pro, array
- for(unsigned int i=0;i<n;i++)
- {
- tempPro[i].processID=pro[i].processID;
- tempPro[i].arrivalT=pro[i].arrivalT;
- tempPro[i].CPUburstT=pro[i].CPUburstT;
- tempPro[i].ppriority=pro[i].ppriority;
- }
- while(numProDone<n)//checking if all the processes have done, if not, increment by 1.
- {
- ///Ready Queue Processes
- for(unsigned int i=0;i<n;i++)//get all the possible processes in ready queue, not include any process that has the CPUburstT of 0 or the remainingT of 0
- {
- if ((tempPro[i].arrivalT<=CPUrunT)&&(tempPro[i].CPUburstT>0))
- {
- tempProReadyQ.push_back(process());//creating a copy of ready queue processes, vector
- tempProReadyQ[countV].processID=tempPro[i].processID;
- tempProReadyQ[countV].arrivalT=tempPro[i].arrivalT;
- tempProReadyQ[countV].CPUburstT=tempPro[i].CPUburstT;
- tempProReadyQ[countV].ppriority=tempPro[i].ppriority;
- countV++;
- }
- }
- countV=0;
- if(tempProReadyQ.empty())//if ready queue is empty and no process is brought in, then it means the CPU is idle
- {
- CPUidleT++;
- CPUrunT++;//anything before this count from 0, anything else below this count from 1
- }
- else if (!tempProReadyQ.empty())//else ready queue has processes
- {
- if (tempProReadyQ.size()==1)//if there is only one process in the ready queue
- {
- tempPro[tempProReadyQ[0].processID-1].CPUburstT--;
- countN[tempProReadyQ[0].processID-1]++;
- if(countN[tempProReadyQ[0].processID-1]==1)
- {
- actualArrivalT[tempProReadyQ[0].processID-1]=CPUrunT;//only needs the 1st actual arrival time for each process
- }
- if(tempPro[tempProReadyQ[0].processID-1].CPUburstT==0)
- {
- finishT[tempProReadyQ[0].processID-1]=CPUrunT+1;
- numProDone++;
- }
- CPUrunT++;
- }
- else//there are more than one process in the ready queue
- {
- int minEle=INT_MAX;
- int tempProcessIndex=0;
- for(unsigned i=0; i<tempProReadyQ.size();i++)
- {
- if(tempProReadyQ[i].CPUburstT<minEle)
- {
- minEle=tempProReadyQ[i].CPUburstT;//smallest process ID
- tempProcessIndex=i;
- }
- }
- sort(tempProReadyQ.begin(),tempProReadyQ.end(),checkbtvector);//sort the process based on their CPU burst time, smallest CPUburst time is the first element
- for(unsigned int i=0;i<tempProReadyQ.size();i++)//to collect the processes ID that has the same shortest CPUburstT
- {
- if(tempProReadyQ[i].CPUburstT==tempProReadyQ[0].CPUburstT)//check if there are more than one process that has the same shortest CPUburstT
- {
- tempMin.push_back(tempProReadyQ[i].processID);//it stores process IDs
- }
- }
- if(!tempMin.empty())//there are processes with the same CPUburstT
- {
- sort(tempMin.begin(),tempMin.end(),findMin);//find the smallest process ID
- tempPro[tempMin[0]-1].CPUburstT--;
- countN[tempMin[0]-1]++;
- if(countN[tempMin[0]-1]==1)
- {
- actualArrivalT[tempMin[0]-1]=CPUrunT;//only needs the 1st actual arrival time for each process
- }
- if(tempPro[tempMin[0]-1].CPUburstT==0)
- {
- finishT[tempMin[0]-1]=CPUrunT+1;
- numProDone++;
- }
- CPUrunT++;
- }
- else if(tempMin.empty())//no processes with the same CPUburstT
- {
- tempPro[tempProReadyQ[0].processID-1].CPUburstT--;
- countN[tempProReadyQ[0].processID-1]++;
- if(countN[tempProReadyQ[0].processID-1]==1)
- {
- actualArrivalT[tempProReadyQ[0].processID-1]=CPUrunT;//only needs the 1st actual arrival time for each process
- }
- if(tempPro[tempProReadyQ[0].processID-1].CPUburstT==0)
- {
- finishT[tempProReadyQ[0].processID-1]=CPUrunT+1;
- numProDone++;
- }
- CPUrunT++;
- }
- tempMin.clear();
- }
- }
- tempProReadyQ.clear();//used it when it vector
- tempProReadyQ.shrink_to_fit();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement