May 5, 2012

C program to implement SJF algorithm.

Write a C program to implement SJF algorithm.
Shortest Job First(SJF) is the CPU scheduling algorithm. In SJF, if the CPU is available it allocates the process which has smallest burst time, if the two process are same burst time it uses FCFS algorithm. Read more about C Programming Language . and read the C Programming Language (2nd Edition) by K and R.


/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>

void main()
{
    int i,j,n,brust_time[10],start_time[10],end_time[10],wait_time[10],temp,tot;
    float avg;
    clrscr();
    printf("Enter the No. of jobs:\n\n");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        printf("\n \n Enter %d process burst time:\n",i);
        scanf("%d",&brust_time[i]);
    }
    
    for(i=1;i<=n;i++)
    {
        for(j=i+1;j<=n;j++)
        {
            if(brust_time[i]>brust_time[j])
            {
                temp=brust_time[i];
                brust_time[i]=brust_time[j];
                brust_time[j]=temp;
            }
        }
        
        if(i==1)
        {
            start_time[1]=0;
            end_time[1]=brust_time[1];
            wait_time[1]=0;
        }
        
        else
        {
            start_time[i]=end_time[i-1];
            end_time[i]=start_time[i]+brust_time[i];
            wait_time[i]=start_time[i];
        }
    }
    printf("\n\n BURST TIME \t STARTING TIME \t END TIME \t   WAIT TIME\n");
    printf("\n ********************************************************\n");
    for(i=1;i<=n;i++)
    {
        printf("\n %5d %15d %15d %15d",brust_time[i],start_time[i],end_time[i],wait_time[i]);
    }
    printf("\n ********************************************************\n");
    for(i=1,tot=0;i<=n;i++)
    tot+=wait_time[i];
    avg=(float)tot/n;
    printf("\n\n\n AVERAGE WAITING TIME=%f",avg);
    for(i=1,tot=0;i<=n;i++)
    tot+=end_time[i];
    avg=(float)tot/n;
    printf("\n\n AVERAGE TURNAROUND TIME=%f",avg);
    for(i=1,tot=0;i<=n;i++)
    tot+=start_time[i];
    avg=(float)tot/n;
    printf("\n\n AVERAGE RESPONSE TIME=%f\n\n",avg);
    getch();
}
Read more Similar C Programs
C Basic

Search Algorithms.
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link