Shortest Job First Scheduling Algorithm -- Implementation in C++

Shortest Job First (SJF) is a scheduling policy that selects the waiting process with the smallest burst time to execute first. Below is my implementation in C++.

  1 #include<iostream>
  2 
  3 using namespace std;
  4 
  5 struct process{
  6     int processId ;
  7     int burstTime ;
  8 };
  9 
 10 int main(){
 11     int numberOfProcesses;
 12 
 13     //number of process
 14     cout << "\nNUMBER OF PROCESS: ";
 15     cin >> numberOfProcesses;
 16 
 17     struct process p[numberOfProcesses];
 18     struct process temp;
 19     int i;
 20     int j;
 21     int waitingTime = 0;
 22     float totalWaitingTime = 0;
 23 
 24     //burst time
 25     for(i=0; i<numberOfProcesses; i++){
 26         p[i].processId = i+1 ;
 27         cout << "******************************\n";
 28         cout << "PROCESS " << i+1 << "\n";
 29         cout << "\tBURST TIME: ";
 30         cin >> p[i].burstTime;
 31         totalWaitingTime += p[i].burstTime;
 32     }
 33 
 34     //sort
 35     for(i=0; i<numberOfProcesses; i++){
 36         for(j=0; j<numberOfProcesses; j++){
 37             if(p[j].burstTime>p[i].burstTime){
 38                 temp = p[i];
 39                 p[i] = p[j] ;
 40                 p[j] = temp;
 41             }
 42         }
 43     }
 44 
 45     //display
 46     cout << "\n========================================================\n";
 47     cout << "     PROCESS\t    BURST TIME\t   WAITING TIME\n\n";
 48     for(i=0;i<numberOfProcesses;i++){
 49         cout <<"\tP" << p[i].processId << "\t\t" << p[i].burstTime;
 50         cout << "\t\t" << waitingTime << "\n";
 51         waitingTime = waitingTime + p[i].burstTime ;
 52     }
 53     cout << "========================================================\n";
 54 
 55     //average waiting time
 56     cout << "AVERAGE WAITING TIME = ";
 57     cout << totalWaitingTime/numberOfProcesses << "\n\n" ;
 58     return 0;
 59 }

 

OUTPUT:

shortest job first output

y does the output exit after i put the process.???????????????

wat was that..?!

panu po nakuha ung number of process?
lahat po pala..pkielaborate po...TNX

kuya pano ko la2gyn ng turn around time?..or pano ung computation

totalWaitingTime? it should be totalBurstTime

use getch() before return 0.......

Kuya ClaUDz....Supah dupaH teNX jud KEu sa Imuha Help..it's A big heLp talaGA..Tenx KEu..BRENDZ:]

ei

bat gnun ung output ???
after the process 5 nag,aout na siya !!!

pnu ba ga2win ku ???

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.