my desired output is this:

Input number of process to simulate SJF: 4
Input burst time for each process:
P1 = 7
P2 = 4
P3 = 1
P4 = 4

Input Arrival Time for each process:
P1 = 0
P2 = 2
P3 = 4
P4 = 5


Now simulating SJF:

Time Process Running

1 P1
2 P1
3 P1
4 P1
5 P1
6 P1
7 P1
8 P3
9 P2
1O P2
11 P2
12 P2
13 P4
14 P4
15 P4
16 P4

Waiting Time:

P1 = 0
P2 = 6
P3 = 3
P4 = 7

Average Waiting Time = 4


--------------------------------------------
is any one familiar with non preemptive SJF?


i'm having a problem computing for the waiting time for each process given the arrival time and burst time. Also, the time process running part, i really don't have a clue what
to do.. does anybody have an idea?

I have attached my full code.

Attachments
// rizza mae abad and irene bernadas

#include<iostream>
  
  using namespace std;
 
  struct process{
      int processId ;
      int burstTime ;
      int arrivalTime;
      int executionTime;
  };
  
  int main(){
      
      int numberOfProcesses;
      struct process p[numberOfProcesses];
      struct process temp;
      int i;
      int j;
      int z;
      int waitingTime = 0;
      int totalWaitingTime = 0;
      
      //number of process
      cout << "\nINPUT NUMBER OF PROCESS TO SIMULATE SJF: ";
      cin >> numberOfProcesses;
      
  
    //burst time
      cout << "\ninput BURST TIME for each process:\n";
      for(i=0; i<numberOfProcesses; i++){
          p[i].processId = i+1;
          cout << "P" << i+1 <<" = ";
          cin >> p[i].burstTime;
          totalWaitingTime += p[i].burstTime;
      }
     
      
      //arrival time
      cout << "\ninput ARRIVAL TIME for each process:\n";
      for(i=0; i<numberOfProcesses; i++){
          p[i].processId = i+1 ;
          cout << "P" << i+1 << " = ";
          cin >> p[i].arrivalTime;
          //totalWaitingTime += p[i].burstTime;
      }
      
 
      
      //simulation
      int arr[totalWaitingTime];
          for(z=0;z<numberOfProcesses; z++){
               if(p[z].burstTime < p[z+1].burstTime)
                   break;        
               else 
                    for(j=1; j<p[z].burstTime; j++){
                        cout << i << " P" << p[z].processId << "\n";
                 /* t = p[j].burstTime;
                  s = p[i].burstTime;
                  if(t < s){
                  int time = t;
                  p[j].executionTime = time;
                  cout << p[j].executionTime << "\n" << p[j].processId;
                  }*/
                //p[i] = p[j] ;
                  //p[j] = temp;
                  }  
          }
  
      //display
      cout << "\n===========================================================\n";
      cout << "PROCESS\t    ARRIVAL TIME     BURST TIME\t    WAITING TIME\n\n";
      for(i=0;i<numberOfProcesses;i++){
          cout <<"\P" << p[i].processId << "\t\t" << p[i].arrivalTime << "\t\t" << p[i].burstTime;
          cout << "\t\t" << waitingTime << "\n";
          waitingTime =  waitingTime - p[i].burstTime ;
      }
      cout << "=============================================================\n";
 
      //average waiting time
      cout << "AVERAGE WAITING TIME = ";
      cout << totalWaitingTime/numberOfProcesses << "\n\n" ;
      
      system("pause");
     return 0;
  }

I could be way off base here but could you not just have a management process of some kind. This process would keep track of the remaining execution time of the currently running process plus those that haven't been executed yet. For each process waiting, the management process could calculate the time for all processes that will be executed before the process for which the waiting time is being calculated.
e.g. if p1 is currently running then (based on burst time) the next to execute is p3. p4 and p2 have the same burst time so the first to execute will be whichever arrived first. Now you know the order of execution you can calculate that, lets say p2, will have to wait for the remainder of p1's execution (burst time) plus that of p3 because these will both run before p2.

Edited 6 Years Ago by Valaraukar: n/a

what do you mean manage process of somewhat sir?
I'm thinking of an array, its length as total burst time.. den the first array would be occupied the process 1.. 0-6.. since p1's burst time is 7... is that wat u min?

Well there are multiple solutions so yes an array could be used I guess. If you store the processes in the order in which they will run then you can calculate the wait time of each process based on the processes stored before.
e.g. the process stored in array index 2 would have a wait time equal to the burst time of index 0 plus index 1.

The limitation to using an array is that you would have to know the number of processes that will be run, prior to creating the array, so that enough memory can be allocated. Otherwise you will need to allocate memory dynamically which would be costly if processes are continually being added to and removed from the array.
A vector may provide a similar but tidier solution.

Edited 6 Years Ago by Valaraukar: n/a

This article has been dead for over six months. Start a new discussion instead.