Saturday, 1 July 2017

Shortest Job First in c program - SJF in operating system

Aim: To Implement Shortest Job First cpu scheduling algorithm.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
                int n,bt[10],wt[10],tt[10],stt,swt=0,i,j,temp;
                float awt,att;
                clrscr();
                printf("enter no of process \n");
                scanf("%d",&n);
                printf("enter process cpu time \n");
                for(i=0;i<n;i++)
                                scanf("%d",&bt[i]);
                printf("\n cpu time before sorting \n");
                for(i=0;i<n;i++)
                                printf("%d \t",bt[i]);
                for(i=0;i<n;i++)
                                for(j=i;j<=n-1;j++)
                                {
                                                if(bt[i]>bt[j])
                                                {
                                                                temp=bt[i];
                                                                bt[i]=bt[j];
                                                                bt[j]=temp;
                                                }
                                }
                printf("\n cpu time after sorting \n");
                for(i=0;i<n;i++)
                                printf("%d \t",bt[i]);
                wt[0]=0;
                tt[0]=stt=bt[0];
                for(i=1;i<n;i++)
                {
                                wt[i]=tt[i-1];
                                tt[i]=wt[i]+bt[i];
                                swt+=wt[i];
                                stt+=tt[i];
                }
                awt=(float)swt/n;
                att=(float)stt/n;
                printf("cpu time \t waiting \t turn around \n");
                for(i=0;i<n;i++)
                                printf("%d\t\t%d\t\t%d\n",bt[i],wt[i],tt[i]);
                printf("\n average waiting time is %f turn around time is %f \n",awt,att);
                getch();
}

Output:





No comments:

Post a Comment