Sunday, November 25, 2012

Shortest Job First (Non-preemptive) SJF in OS

/* 2.simulate Shortest Job First (Non-preemptive)               */


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    int i,j,n,Bt[10],wt[10],At[10],Tt=0;
    char c[10][10],pn[10][10],s[10];
    int z[50];
    float Twt=0.0,Awt;
    int w=0,temp,t;
    clrscr();
    printf("\n Enter no. of processes ::");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("\n Enter the processname ::");
        scanf("%s",&pn[i]);
        printf("\n Enter the Burst time for process P%d::",i+1);
        scanf("%d",&Bt[i]);
        printf("\n Enter the Arrival time for process P%d::",i+1);
        scanf("%d",&At[i]);
        s[i]='T';
        Tt+=Bt[i];
    }
    for(i=0;i<n;i++)
    {
        for(j=2;j<n;j++)
        {
            if(Bt[j-1]>Bt[j])
            {
                temp=Bt[j];
                Bt[j]=Bt[j-1];
                Bt[j-1]=temp;
                temp=At[j];
                At[j]=At[j-1];
                At[j-1]=temp;
                strcpy(c[j-1],pn[j-1]);
                strcpy(pn[j-1],pn[j]);
                strcpy(pn[j],c[j-1]);
            }
        }
    }
    wt[0]=0;
    z[0]=0;
    w+=Bt[0];
    t=w;
    s[0]='F';
    while(w<Tt)
    {
        i=1;
        while(i<=n)
        {
            if(s[i]=='T' && At[i]<=t)
            {
                z[i]=w;
                wt[i]=w-At[i];
                s[i]='F';
                w+=Bt[i];
                t=w;
                i=1;
            }
            else
                i++;
        }
    }
    printf("\n..............................................\n");
    printf("\nPname\tBt\tAt\tWt");
    printf("\n..............................................\n");
    for(i=0;i<n;i++)
        printf("\n%s\t%d\t%d\t%d",pn[i],Bt[i],At[i],wt[i]);
    printf("\n..............................................\n");
    printf("\n\n Gannt Chart ::\n");
    for(i=0;i<n;i++)
        printf("%4d",z[i]);
    for(i=0;i<n;i++)
        Twt+=wt[i];
    printf("\n\n Total waiting Time ::%f",Twt);
    Awt=Twt/n;
    printf("\n\n Average waiting Time ::%f",Awt);
    getch();
}

/* Input and Output :-

 Enter no. of processes ::3
 Enter the processname ::dsk
 Enter the Burst time for process P1::24
 Enter the Arrival time for process P1::0
 Enter the processname ::pdn
 Enter the Burst time for process P2::3
 Enter the Arrival time for process P2::4
 Enter the processname ::mkk
 Enter the Burst time for process P3::3
 Enter the Arrival time for process P3::10
..............................................
Pname   Bt      At      Wt
..............................................

dsk     24      0       0
pdn     3       4       20
mkk     3       10      17
..............................................

 Gannt Chart ::

   0  24  27

 Total waiting Time ::37.000000

 Average waiting Time ::12.333333            */

No comments:

Post a Comment