Skip to main content

Program of SJF - shortest job first (sjf) | Program in C++

Program of SJF - shortest job first (sjf) | Program in C++  || Dot clu




Shortest job next (SJN), also known as shortest job first (SJF) or shortest process next (SPN), is a scheduling policy that selects for execution the waiting process with the smallest execution time. SJN is a non-preemptive algorithm. Shortest remaining time is a preemptive variant of SJN.

Shortest job first depends on the average running time of the processes. The accurate estimates of these measures help in the implementation of the shortest job first in an environment, which otherwise makes the same nearly impossible to implement. This is because often the execution burst of processes does not happen beforehand. It can be used in interactive environments where past patterns are available to determine the average time between the waiting time and the commands. Although it is disadvantageous to use the shortest-job-first concept in short-term CPU scheduling, it is considered highly advantageous in long-term CPU scheduling. Moreover, the throughput is high in the case of shortest job first.

Shortest job first also has its share of disadvantages. For one, it can cause process starvation for longer jobs if there are a large number of shorter processes. Another is the need to know the execution time for each process beforehand. Often, this is almost impossible in many environments.

Source Code of SJF

  1. //shortest job First >> SJF

  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
  9.     cout<<"Enter Total Number of Process:";
  10.     cin>>n;
  11.  
  12.     cout<<"\nEnter Burst Time and Priority\n";
  13.     for(i=0;i<n;i++)
  14.     {
  15.         cout<<"\nP["<<i+1<<"]\n";
  16.         cout<<"Burst Time:";
  17.         cin>>bt[i];
  18.         cout<<"Priority:";
  19.         cin>>pr[i];
  20.         p[i]=i+1;           //contains process number
  21.     }
  22.  
  23.     //sorting burst time, priority and process number in ascending order using selection sort
  24.     for(i=0;i<n;i++)
  25.     {
  26.         pos=i;
  27.         for(j=i+1;j<n;j++)
  28.         {
  29.             if(pr[j]<pr[pos])
  30.                 pos=j;
  31.         }
  32.  
  33.         temp=pr[i];
  34.         pr[i]=pr[pos];
  35.         pr[pos]=temp;
  36.  
  37.         temp=bt[i];
  38.         bt[i]=bt[pos];
  39.         bt[pos]=temp;
  40.  
  41.         temp=p[i];
  42.         p[i]=p[pos];
  43.         p[pos]=temp;
  44.     }
  45.  
  46.     wt[0]=0;            //waiting time for first process is zero
  47.  
  48.     //calculate waiting time
  49.     for(i=1;i<n;i++)
  50.     {
  51.         wt[i]=0;
  52.         for(j=0;j<i;j++)
  53.             wt[i]+=bt[j];
  54.  
  55.         total+=wt[i];
  56.     }
  57.  
  58.     avg_wt=total/n;      //average waiting time
  59.     total=0;
  60.  
  61.     cout<<"\nProcess\t    Burst Time    \tWaiting Time\tTurnaround Time";
  62.     for(i=0;i<n;i++)
  63.     {
  64.         tat[i]=bt[i]+wt[i];     //calculate turnaround time
  65.         total+=tat[i];
  66.         cout<<"\nP["<<p[i]<<"]\t\t  "<<bt[i]<<"\t\t    "<<wt[i]<<"\t\t\t"<<tat[i];
  67.     }
  68.  
  69.     avg_tat=total/n;     //average turnaround time
  70.     cout<<"\n\nAverage Waiting Time="<<avg_wt;
  71.     cout<<"\nAverage Turnaround Time="<<avg_tat;
  72.  
  73.     return 0;
  74. }

Also Watch Video for DEMO



dotclu
dot clu

=====Also Watch Our Video========
Program sum of "n" number in c |  n number program in c || dot clu

Program of sum of 2 integer number | 2nd Program of C tutorial | Program of input integer  | dot clu

Get started With C || First Program in C || C tutorial || Hello world program in C || dot clu

Install Virtual BOX in your PC | create Virtual Machine | downloading link in description || dot clu

How to Install Hyper V in windows 7?

What is Bitcoin? || Earn Bitcoin money with your Android phone || 100% working || dot clu || Hindi

Comments

Popular posts from this blog

Shortest Remaining Time First (SRTF) Program in C++ || dot clu

Shortest Remaining Time First (SRTF) Program in C++ || dot clu Shortest Remaining Time First (SRTF) Shortest Remaining Time First (SRTF) Algorithm is preemptive version of Shortest Job First Algorithm. In this current process is executed until it is completed or a new process is added having lower burst time compare to the the remaining time for current process. SRTF algorithm may lead to starvation, if processes with lower burst time continues to add to cpu scheduler then the current process will never get a chance to get executed. For example consider the following table Process Arrival Time Burst Time P1 0 10 P2 1 6 P3 2 9 P4 3 4 At time t=0, Process P1 will start get executing as it is only the process present at that time. Then at t=1, Process P2 added to the CPU scheduler, at this time remaining time(Burst time) for Process P1 gets 9, as Burst time of P2 is less than the remaining time of other processes (for now there is only process P1) therefore pro...

First Fit Program and Algorithm in C++ || dot clu

First Fit Program and Algorithm in C++ || dot clu First Fit Algorithm in C and C++ Here you will learn about first fit algorithm in C and C++ with program examples. There are various memory management schemes in operating system like first fit, best fit and worst fit. In this section we will talk about first fit scheme. Suggestion: 1. First Fit 2.  Best Fit 3.  Worst Fit What is First Fit Memory Management Scheme? In this scheme we check the blocks in a sequential manner which means we pick the first process then compare it’s size with first block size if it is less than size of block it is allocated otherwise we move to second block and so on. First Fit Algorithm Get no. of Processes and no. of blocks. After that get the size of each block and process requests. Now allocate processes if(block size >= process size) //allocate the process else //move on to next block Display the processes with the blocks that are allocated to a respective...

Round Robin Scheduling Program in C++ || dot clu

Round Robin Scheduling Program in C++ Process scheduling is an important component for process management. In a multi-user and a time-sharing system, response time is one of the most important objective to be accomplished. Round Robin Scheduling Algorithm 1. The queue structure in ready queue is of First In First Out (FIFO) type. 2. A fixed time is allotted to every process that arrives in the queue. This fixed time is known as time slice or time quantum. 3. The first process that arrives is selected and sent to the processor for execution. If it is not able to  complete its execution within the time quantum provided, then an interrupt is generated using an automated timer. 4. The process is then stopped and is sent back at the end of the queue. However, the state is saved and context is thereby stored in memory. This helps the process to resume from the point where it was interrupted. 5. The scheduler selects another process from the ready queue and dispa...