SJF

PHOTO EMBED

Tue May 07 2024 06:04:55 GMT+0000 (Coordinated Universal Time)

Saved by @proxylabs

//SJF                                                                                                                                                 #include<stdio.h>
#define size 20
int main(void)
{
      int i , j, n , t, p[size],bt[size],wt[size],tat[size];
      float awt=0,atat=0;
      printf("Enter the number of processes:");
      scanf(" %d",&n);
      printf("Enter the process numbers:");
      for(i=0;i<n;i++)
      {
            scanf(" %d",&p[i]);
      }
      printf("Enter burst time of the processes:");
      for(i=0;i<n;i++)
      {
            scanf(" %d",&bt[i]);
      }
      //Applying bubble sort
      for(i=1;i<n;i++)
      {
            for(j=0;j<n-i;j++)
            {
                  t=bt[j];
                  bt[j]=bt[j+1];
                  bt[j+1]=t;
                  t=p[j];
                  p[j]=p[j+1];
                  p[j+1]=t;
            }
      }
      printf("Process\tBT\tWt\tTAT\n");
      for(i=0;i<n;i++)
      {
            wt[i]=0;
            tat[i]=0;
            for(j=0;j<i;j++)
            {
                  wt[i]=wt[i]+bt[j];
            }
            tat[i]=wt[i]+bt[i];
            awt=awt+wt[i];
            atat=atat+tat[i];
            printf("%d\t%d\t%d\t%d\n",p[i],bt[i],wt[i],tat[i]);
      }
      printf("Average waiting time:%0.2f\n",awt/n);
      printf("Average turn around time:%0.2f\n",atat/n);
      return 0;
}

//Our SJF                                                                                                                                       #include<stdio.h>
int main()
{
                              int at[10],bt[10],p[10];
            int n,i,j,temp,time=0,count,over=0,sum_wait=0,sum_turnaround=0,start;
            float avgwait,avgturn;
            printf("Enter the number of processes\n");
            scanf("%d",&n);
            for(i=0;i<n;i++)
            {
                                    printf("Enter the arrival time and execution time for process %d\n",i+1);
                scanf("%d%d",&at[i],&bt[i]);
                p[i]=i+1;
            }
            for(i=0;i<n-1;i++)
            {
                for(j=i+1;j<n;j++)
                {
                   if(at[i]>at[j])
                   {
                    temp=at[i];
                    at[i]=at[j];
                    at[j]=temp;
                    temp=bt[i];
                    bt[i]=bt[j];
                    bt[j]=temp;
                    temp=p[i];
                    p[i]=p[j];
                    p[j]=temp;     
                   }
                        
                 }
            }
            printf("\nProcess\tAT\tBT\tWT\tTAT\n");
            while(over<n)
            {
               count=0;
               for(i=over;i<n;i++)
               {
                  if(at[i]<=time)
                     count++;
                  else
                     break;
               }
               if(count>1)
               {
                 for(i=over;i<over+count-1;i++)
                 {
                    for(j=i+1;j<over+count;j++)
                    {
                      if(bt[i]>bt[j])
                      {
                        temp=at[i];
                        at[i]=at[j];
                        at[j]=temp;
                        temp=bt[i];
                        bt[i]=bt[j];
                        bt[j]=temp;
                        temp=p[i];
                        p[i]=p[j];
                        p[j]=temp;     
                       }
                     }                     
                   }
                 }
                start=time;
                time+=bt[over];
                printf("p[%d]\t%d\t%d\t%d\t%d\n",p[over],at[over],bt[over],time-at[over]-bt[over],time-at[over]);
                sum_wait+=time-at[over]-bt[over];
                                                                                                sum_turnaround+=time-at[over];
                over++;
            }
            avgwait=(float)sum_wait/(float)n;
            avgturn=(float)sum_turnaround/(float)n;
            printf("Average waiting time is %f\n",avgwait);
            printf("Average turnaround time is %f\n",avgturn);
            return 0;
}  
content_copyCOPY