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 [email protected]
* 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:nn");
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("nn BURST TIME t STARTING TIME t END TIME t WAIT TIMEn");
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("nnn AVERAGE WAITING TIME=%f",avg);
for(i=1,tot=0;i<=n;i++)
tot+=end_time[i];
avg=(float)tot/n;
printf("nn AVERAGE TURNAROUND TIME=%f",avg);
for(i=1,tot=0;i<=n;i++)
tot+=start_time[i];
avg=(float)tot/n;
printf("nn AVERAGE RESPONSE TIME=%fnn",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 Follow @c_program

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
(c) www.c-program-example.com

C Program to implement FCFS algorithm.

Write a C Program to implement FCFS algorithm.
First Come First Served(FCFS) algorithm is the CPU scheduling algorithm. In FCFS process is served when they are arrived in order, i.e First In First Out(FIFO). FCFS is the simple scheduling algorithm, but it takes typically long/varying waiting time.
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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,sum=0;
int arrv[10],ser[10],start[10];
int finish[10],wait[10],turn[10];
float avgturn=0.0,avgwait=0.0;
start[0]=0;
clrscr();
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the arriwal and service time of %d process:",i+1);
scanf("%d%d",&arrv[i],&ser[i]);
}
for(i=0;i<n;i++)
{
sum=0;
for(j=0;j<i;j++)
sum=sum+ser[j];
start[i]=sum;
}
for(i=0;i<n;i++)
{
finish[i]=ser[i]+start[i];
wait[i]=start[i];
turn[i]=ser[i]+wait[i];
}
for(i=0;i<n;i++)
{
avgwait+=wait[i];
avgturn+=turn[i];
}
avgwait/=n;
avgturn/=n;
printf("narraival service Start Finish Wait Turnn");
for(i=0;i<n;i++)
printf("%dt%dt%dt%dt%dt%dn",arrv[i],ser[i],start[i],
finish[i],wait[i],turn[i]);
printf("nAverage waiting time=%f",avgwait);
printf("nAverage turn around time=%f",avgturn);
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 Follow @c_program

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
(c) www.c-program-example.com