Resources » Articles/Knowledge Sharing » Computer & Technology

Program in 'C' for implementing SJF CPU Scheduling


Last Updated:   Category: Computer & Technology    
Author: Member Level: Bronze    Points: 5


This program explains a scheduling policy known as SJF(Shortest Job First) used by CPU(Central Processing Unit)of computers that selects the waiting process with smallest execution time to execute first from a number of processes to be executed by the CPU at same time.



Introduction :



This program do this job of deciding which process to be executed first in a number of programs using SJF(Shortest Job First) using three steps:-


  • It first determines the number of processes to be executed by the CPU(Central Processing Unit) simultaneously.


  • Then it reads the name, burst time & arrival time of each process.


  • It then evaluates the TAT(Turn Around Time) and WT(Waiting Time)of each process.


  • Then by calculating the average burst time & turn around time it decides which of the process to be executed first among a number of processes.


This methodology was used by older CPU's to perform a number of task's simultaneously. They used to select the task which needs the lesser time to finish and then perform it. After that they used to select the next shortest job & so on. Now a days, this methodology is not used by CPU's. They now use time sharing methodology i.e. sharing time between process like 1 second for each task in 1 cycle.

Program:






#include
#include

void sjf(int,int[],int[],char p[10][10]);

void main()
{
char p[10][10];
int bt[10],i,at[10],n;
clrscr();
printf("OUTPUT:\n\n");
printf("Enter the number of processes:");
scanf("%d",&n);
printf("Enter the name, burst time & arrival time for processes\n");
for(i=0;i {
printf("Enter the name of the process:");
scanf("%s",&p[i]);
printf("Enter Arrival time for process P%d in ms:",i+1);
scanf("%d",&at[i]);
printf("Enter Burst time for process P%d in ms:",i+1);
scanf("%d",&bt[i]);
}
sjf(n,at,bt,p);
getch();
}

void sjf(int n,int at[],int bt[],char p[10][10])
{
char temp[10];
int tat[10],wt[10],i,j,temp1;
float totaltat=0,totalwt=0,avgtat,avgwt;

for(i=0;i {
for(j=i+1;j {
if(bt[i]>bt[j])
{
temp1=bt[i];
bt[i]=bt[j];
bt[j]=temp1;
strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
for(i=0;i {
tat[i]=0;
for(j=i;j>=0;j--)
{
tat[i]=tat[i]+bt[j];
}
tat[i]=tat[i]-at[i];
wt[i]=tat[i]-bt[i];
totalwt=totalwt+wt[i];
totaltat=totaltat+tat[i];
}
printf("\n");
printf("Process\tAT\tBT\tTAT\tWT\n");
printf("--------------------------------------\n");
for(i=0;i printf("%s\t%dms\t%dms\t%dms\t%dms\n",p[i],at[i],bt[i],tat[i],wt[i]);
avgwt=(float)totalwt/n;
avgtat=(float)totaltat/n;
printf("\nAvg turn around time=%5.2f ms\nAvg waiting time=%5.2f ms",avgtat,avgwt);
}




Output


The output of the above program is :

Enter the number of processes:3
Enter the name, burst time & arrival time for processes
Enter the name of the process:p1
Enter Arrival time for process P1 in ms:0
Enter Burst time for process P1 in ms:10
Enter the name of the process:p2
Enter Arrival time for process P2 in ms:0
Enter Burst time for process P2 in ms:5
Enter the name of the process:p3
Enter Arrival time for process P3 in ms:0
Enter Burst time for process P3 in ms:3

Process AT BT TAT WT
--------------------------------------
p3 0ms 3ms 3ms 0ms
p2 0ms 5ms 8ms 3ms
p1 0ms 10ms 18ms 8ms

Avg turn around time= 9.67 ms
Avg waiting time= 3.67 ms


Reference: http://learncandcpp.blogspot.com
Related Resources:


Read related articles: C program    

Did you like this resource? Share it with your friends and show your love!




Responses to "Program in 'C' for implementing SJF CPU Scheduling"
Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Awards & Gifts
    Active Members
    TodayLast 7 Daysmore...

    ISC Technologies, Kochi - India. Copyright © All Rights Reserved.