SJF and SRTF algorithm and program in c with gantt chart

SJF and SRTF algorithm and program in c with gantt chart

Shortest Job first Scheduling Algorithm

1. Shortest Job First Scheduling Non Preemptive with Arrival time
Criteria: Brust time
Mode: Non Primitive

Process No Arrival Time (AT) Brust time (BT)/ CPU Time Complication Time(CT) Turn Around Time(TAT) Waiting time(WT) Response Time(RT)
P1 1 3 6 5 2 2
P2 2 4 10 8 4 4
P3 1 2 3 2 0 0
P4 4 4 14 10 6 6

Gantt Chart
 
 
 
   
P3 P1 P2 P4
0        1              3            6          10          14

Here,
Note: in empty house there have a black straight line
Turn Around Time(TAT) = Complication Time(CT) -  Arrival Time (AT)
Waiting time(WT) = Turn Around Time(TAT) - Brust time (BT)
Response Time(RT) = When first come to the process in Gantt Chart - Arrival Time (AT)


2. Shortest Job First Scheduling Preemptive with Arrival time
Criteria: Brust time
Mode: Primitive
Process No Arrival Time (AT) Brust time (BT)/ CPU Time Complication Time(CT) Turn Around Time(TAT) Waiting time(WT) Response Time(RT)
P1 2 6 20 18 12 12
P2 1 3 4 3 0 0
P3 4 2 6 2 0 0
P4 0 5 10 10 5 0
P5 6 4 14 8 4 4

Gantt Chart
P4 P2 P2 P2 P3 P3 P4 P5 P1
0     1       2      3        4       5      6     10      14      20

Here,
Turn Around Time(TAT) = Complication Time(CT) -  Arrival Time (AT)
Waiting time(WT) = Turn Around Time(TAT) - Brust time (BT)
Response Time(RT) = When first come to the process in Gantt Chart - Arrival Time (AT)

----------------------------x -------------------------
1. Shortest Job First Scheduling Non Preemptive with Arrival time in C programming language code
Here,



#include<iostream>
using namespace std;
int main()
{
    int n, process[10],cpu[10],w[10],t[10],At[10],sum_w=0,sum_t=0,i,j,temp=0,temp1=0,temp2=0;
    float avg_w, avg_t;
    printf("enter the number of process\n");
    scanf("%d", &n);
    for(i=0; i<n; i++)
    {
        printf("Enter cpu time of P%d:",i+1);
        scanf("%d", &cpu[i]);
        printf("\n");
    }
    for(i=0; i<n; i++)
    {
        printf("Enter Arrival time time of P%d:",i+1);
        scanf("%d", &At[i]);
        printf("\n");
    }
    process[0]=1;
    for(i=1; i<n; i++)
    {
        process[i]=i+1;
    }
    for(i=1; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(cpu[i]>cpu[j])
            {
                temp=cpu[i];
                cpu[i]=cpu[j];
                cpu[j]=temp;
                temp1=process[i];
                process[i]=process[j];
                process[j]=temp1;
                temp2=At[i];
                At[i]=At[j];
                At[j]=temp2;
            }
        }
    }
    w[0]=0-At[0];
    for(i=1; i<n; i++)
    {
        w[i]=w[i-1]+cpu[i-1];
    }
    for(i=1; i<n; i++)
    {
        w[i]=w[i]-At[i];
        sum_w=sum_w+w[i];
    }
    for(i=0; i<n; i++)
    {
        t[i]=w[i]+cpu[i];
        sum_t=sum_t+t[i];
    }
    printf("Process--CPU_time--Wait--Turnaround\n");
    for(i=0; i<n; i++)
    {
        printf(" P%d %d %d %d",process[i],cpu[i],w[i],t[i]);
        printf("\n");
    }
    avg_w=(float)sum_w/n;
    avg_t=(float)sum_t/n;
    printf("average waiting time=%.2f\n",avg_w);
    printf("average turnaround time=%.2f\n",avg_t);
    cout<<"\n";
    cout<<"===============================GrandChart====================================="<<endl<<"\n";
                printf("|");
    for(i=0; i<n; i++)
    {
        printf(" P%d |",process[i]);
    }
    printf("\n0");
    for(i=0; i<n; i++)
    {
        printf(" %d",t[i]+At[i]);
    }
}



2. Shortest Job First Scheduling Non Preemptive without Arrival time in C programming language code
Here,


#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    int n,
        process[10],cpu[10],w[10],t[10],At[10],sum_w=0,sum_t=0,i,j,temp=0,temp1=0;
    float avg_w, avg_t;
    printf("enter the number of process\n");
    scanf("%d", &n);
    for(i=0; i<n; i++)
    {
        printf("Enter cpu time of P%d:",i+1);
        scanf("%d", &cpu[i]);
        printf("\n");
    }
    process[0]=1;
    for(i=1; i<n; i++)
    {
        process[i]=i+1;
    }
    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(cpu[i]>cpu[j])
            {
                temp=cpu[i];
                cpu[i]=cpu[j];
                cpu[j]=temp;
                temp1=process[i];
                process[i]=process[j];
                process[j]=temp1;
            }
        }
    }
    w[0]=0;
    for(i=1; i<n; i++)
    {
        w[i]=w[i-1]+cpu[i-1];
    }
    for(i=0; i<n; i++)
    {
        sum_w=sum_w+w[i];
    }

    for(i=0; i<n; i++)
    {
        t[i]=w[i]+cpu[i];
        sum_t=sum_t+t[i];
    }
    printf("Process--CPU_time--Wait--Turnaround\n");
for(i=0; i<n; i++)
    {
        printf(" P%d   \t%d   \t%d  \t%d",process[i],cpu[i],w[i],t[i]);
        printf("\n");
    }
    avg_w=(float)sum_w/n;
    avg_t=(float)sum_t/n;
    printf("average waiting time=%.2f\n",avg_w);
    printf("average turnaround time=%.2f\n",avg_t);
    cout<<"\n";
    cout<<"===============================GrandChart====================================="<<endl<<"\n";
    printf("|");
    for(i=0; i<n; i++)
    {
        printf(" P%d |",process[i]);
    }
    printf("\n0");
    for(i=0; i<n; i++)
    {
        printf("   %d",t[i]);
    }
}



3. Shortest Job First Scheduling Preemptive with Arrival time in C programming language code
Here,



#include <iostream>

using namespace std;
 
int main(){
     int i,j,k,p,s=0, get=0, idle=0, t_burst, t_row, pre_process_row, final=0;
     float sum=0;
 
     cout<<"Please enter the number process : ";
     cin>>p;
 
     int a[p][5];
     int b[p][5];
 
     cout<<"\nProcess\tArrival\tBurst\n-------\t-------\t-----\n";
     for(i=0;i<p;i++){
          for(j=0;j<3;j++){
               cin>>a[i][j];
               }
          a[i][3]=a[i][2]; 
          }
 
     cout<<"\n\nTime-Line is as follows (Verticle View)....\n\n";
 
     i=a[0][1];
     while(final!=p){
          get=0;
          k=0;
          while(k<p){
               if(a[k][1]<=i){
                    if(a[k][2]!=0){
                         get=1;          
                         t_burst=a[k][2];
                         t_row=k;
                         idle=0;
                         break;
                         }
                    else
                         k++;             
                    }
               else{
                    if(idle==0)
                         printf("%5d-----------\n        |Idle  |\n",i);
                    idle=1;
                    break;
                    }
               }
          if(get!=0){
               k=0;
               while(a[k][1]<=i && k<p){
                    if(a[k][2]!=0){
                         if(t_burst>a[k][2]){
                              t_burst=a[k][2];
                              t_row=k;
                              }
                         }
                    k++;
                    }
 
               a[t_row][2]-=1;
                
               if(i==a[0][1])   
                    printf("%5d-----------\n        |p-%-4d|\n",i,a[t_row][0]);
               else{
                    if(pre_process_row!=t_row)
                         printf("%5d-----------\n        |p-%-4d|\n",i,a[t_row][0]);                   
                    }
 
               pre_process_row=t_row;
      
               if(a[t_row][2]==0){
                    final++;
                    b[s][0]=a[t_row][0];
                    b[s][1]=a[t_row][1];
                    b[s][2]=i;
                    b[s][3]=a[t_row][3];
                    b[s][4]=((i-a[t_row][1])-a[t_row][3])+1;        
                    sum+=((i-a[t_row][1])-a[t_row][3])+1;
                    s++;
                    }
               }
          i++;
          }
 
 
     printf("%5d-----------\n",i); 
 
     cout<<endl<<endl;
     cout<<"Table of processes with completion record as they were completed\n\n";
     cout<<"\n\nProcess\tArrival\tFin\tTotal\tWait\n-------\t-------\t---\t-----\t----\n";
 
     for(i=0;i<s;i++)
          cout<<b[i][0]<<"\t"<<b[i][1]<<"\t"<<b[i][2]<<"\t"<<b[i]     [3]<<"\t"<<b[i][4]<<"\n";
 
     cout<<"\nAvg. Wait time = "<<sum/p<<endl<<endl;
     cout<<" \n\n";
 
     return 0;
     }



Thank You...
SJF scheduling Program in C, SJF preemptive scheduling, SJF non preemptive scheduling Program in C, shortest job first scheduling algorithm, shortest job first scheduling program in c with arrival time non preemptive, shortest job first scheduling algorithm preemptive, shortest job first preemptive example, shortest job first non preemptive example, shortest job first non preemptive, SJF non preemptive scheduling Program in C, shortest job first scheduling example, preemptive shortest job first calculator, sjf non preemptive scheduling program in c with gantt chart and arrival time, sjf preemptive scheduling program in c with gantt chart and arrival time, sjf non preemptive scheduling program in c with gantt chart and without arrival time