You are on page 1of 8

Exercise No: 3 IT18412 - Operating System Concepts Laboratory Date: 08/09/2021

IMPLEMENTATION OF SCHEDULING ALGORITHMS (FCFS, SJF)

AIM:
To execute scheduling algorithms, FCFS (First Come First Served) and SJF (Shortest Job First)
using C Programming.
1. FIRST COME FIRST SERVE (FCFS):

ALGORITHM:

1) Start
2) In function int waitingtime(int proc[], int n, int burst_time[], int wait_time[])
2.1. Set wait_time[0] = 0
2.2. For i = 1 and i < n and i++, set wait_time[i] = burst_time[i-1] + wait_time[i-1]
3) In function int turnaroundtime( int proc[], int n, int burst_time[], int wait_time[], int tat[])
3.1. For i = 0 and i < n and i++, set tat[i] = burst_time[i] + wait_time[i]
4) In function int avgtime( int proc[], int n, int burst_time[])
4.1. Declare and initialize wait_time[n], tat[n], total_wt = 0, total_tat = 0;
4.2. Call waitingtime(proc, n, burst_time, wait_time)
4.3. Call turnaroundtime(proc, n, burst_time, wait_time, tat)
4.4. For i=0 and i<n and i++, set total_wt = total_wt + wait_time[i],
total_tat = total_tat + tat[i] and
print process number, burstime wait time and turnaround time
4.5. Print "Average waiting time =i.e. total_wt / n
4.6. Print "Average turn around time = i.e. total_tat / n
5) In int main()
5.1. Declare and read input for n, proc[], burst_time[]
5.2. Call avgtime(proc, n, burst_time)
6) Stop

CODE:

#include <stdio.h>
int waitingtime(int proc[], int n, int burst_time[], int wait_time[])
{
wait_time[0] = 0;
for (int i = 1; i < n ; i++ )
wait_time[i] = burst_time[i-1] + wait_time[i-1] ;
return 0;
}

int turnaroundtime( int proc[], int n, int burst_time[], int wait_time[], int tat[])

Register No: 190801001 Page No: 1


Exercise No: 3 IT18412 - Operating System Concepts Laboratory Date: 08/09/2021

{
int i;
for ( i = 0; i < n ; i++)
tat[i] = burst_time[i] + wait_time[i];
return 0;
}

int avgtime( int proc[], int n, int burst_time[])


{
int wait_time[n], tat[n], total_wt = 0, total_tat = 0;
int i;
waitingtime(proc, n, burst_time, wait_time);
turnaroundtime(proc, n, burst_time, wait_time, tat);
printf("\n\nProcess Burst Time Waiting Time Turn Around Time \n");
for ( i=0; i<n; i++)
{
total_wt = total_wt + wait_time[i];
total_tat = total_tat + tat[i];
printf("%d %d %d %d\n", i+1, burst_time[i], wait_time[i], tat[i]);
}
printf("\n\nAverage waiting time = %.2f\n", (float)total_wt / (float)n);
printf("\nAverage turn around time = %.2f\n", (float)total_tat / (float)n);
return 0;
}

int main()
{
int proc[20], burst_time[20], n, i;
printf("\n\nEnter number of processes : ");
scanf("%d",&n);
printf("\nEnter Burst Time for : \n");
for(i=0;i<n;i++)
{
printf("Process (%d) : ",i+1);
scanf("%d",&burst_time[i]);
}
avgtime(proc, n, burst_time);
return 0;
}

SAMPLE INPUT AND OUTPUT:

Enter number of processes : 3

Enter Burst Time for :


Process (1) : 5

Register No: 190801001 Page No: 2


Exercise No: 3 IT18412 - Operating System Concepts Laboratory Date: 08/09/2021

Process (2) : 8
Process (3) : 12

Process Burst Time Waiting Time Turn Around Time


1 5 0 5
2 8 5 13
3 12 13 25

Average waiting time = 6.00


Average turn around time = 14.33

2. SHORTEST JOB FIRST (SJF) WITHOUT PREEMPTION:

ALGORITHM:

1) Input the number of processes and their burst time (bt).


2) Sort the burst time in ascending order
2) Find waiting time (wt) for all processes.
3) As first process that comes needn’t wait so waiting time for process 1 will be 0 i.e. wt[0] = 0.
4) Find waiting time for all other processes i.e. for process i -> wt[i] = bt[i-1] + wt[i-1] .
5) Find turnaround time = waiting_time + burst_time for all processes.
6) Find average waiting time = total_waiting_time / no_of_processes.
7) Similarly, find average turnaround time = total_turn_around_time / no_of_process

CODE:

#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
//sorting of burst times
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)

Register No: 190801001 Page No: 3


Exercise No: 3 IT18412 - Operating System Concepts Laboratory Date: 08/09/2021

{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\np%d\t%d\t\t%d\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%fn",avg_tat);
}

SAMPLE INPUT AND OUTPUT:

Enter number of process:3


Enter Burst Time:
p1:4
p2:6
p3:3
Process Burst Time Waiting Time Turnaround Time
p3 3 0 3
p1 4 3 7
p2 6 7 13
Register No: 190801001 Page No: 4
Exercise No: 3 IT18412 - Operating System Concepts Laboratory Date: 08/09/2021

Average Waiting Time=3.333333


Average Turnaround Time=7.666667n

3. SHORTEST JOB FIRST (SJF) WITH PREEMPTION:

ALGORITHM:

1) Input the number of processes and their arrival time(at), burst time (bt).

2) Traverse until all process gets completely executed.

3) Find process with minimum remaining time and 0 arrival time at every single time lap.

4) Reduce its time by 1.

5) Check if its remaining time becomes 0

6) Increment the counter of process completion.

7) Completion time of current process = current_time +1;

8) Calculate waiting time for each completed process.

wt[i]= Completion time - arrival_time-burst_time

9) Increment time lap by one.

10) Find turnaround time (waiting_time+burst_time).

11) Find average waiting time = total_waiting_time / no_of_processes.

12) Similarly, find average turnaround time = total_turn_around_time / no_of_process

CODE:

#include <stdio.h>
#include <stdbool.h>
int main()
{
int arrival_time[10], burst_time[10], temp[10];
int wt[10], tat[10], total_wt = 0, total_tat = 0;
int i, smallest, count = 0, time, limit;
double wait_time = 0, turnaround_time = 0, end;
float average_waiting_time, average_turnaround_time;

Register No: 190801001 Page No: 5


Exercise No: 3 IT18412 - Operating System Concepts Laboratory Date: 08/09/2021

printf("\nEnter the Total Number of Processes:\t");


scanf("%d", &limit);

printf("\nEnter Details of %d Processesn", limit);


for(i = 0; i < limit; i++)
{
printf("\nEnter Arrival Time:\t");
scanf("%d", &arrival_time[i]);
printf("Enter Burst Time:\t");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}

int rt[limit];
for (int i = 0; i < limit; i++)
{
rt[i] = burst_time[i];
}

int complete = 0, t = 0, minm = 9999;


int shortest = 0, finish_time;
bool check = false;

while (complete != limit)


{
for (int j = 0; j < limit; j++)
{
if ((arrival_time[j] <= t) && (rt[j] < minm) && rt[j] > 0)
{
minm = rt[j];
shortest = j;
check = true;
}
}

if (check == false)
{
t++;

Register No: 190801001 Page No: 6


Exercise No: 3 IT18412 - Operating System Concepts Laboratory Date: 08/09/2021

continue;
}

rt[shortest]--;
minm = rt[shortest];

if (minm == 0)
{
minm = 9999;
}

if (rt[shortest] == 0)
{
complete++;
check = false;
finish_time = t + 1;
wt[shortest] = finish_time - burst_time[shortest] - arrival_time[shortest];
if (wt[shortest] < 0)
{
wt[shortest] = 0;
}
}
t++;
}

for (int i = 0; i < limit; i++)


{
tat[i] = burst_time[i] + wt[i];
}

printf("\nProcess\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time");


for (int i = 0; i < limit; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf("\np%d\t\t %d\t\t %d\t\t %d\t\t\t
%d",i+1,arrival_time[i],burst_time[i],wt[i],tat[i]);
}

Register No: 190801001 Page No: 7


Exercise No: 3 IT18412 - Operating System Concepts Laboratory Date: 08/09/2021

printf("\n\nAverage Waiting Time=%f",(float)total_wt / (float)limit);


printf("\nAverage Turnaround Time=%f\n",(float)total_tat / (float)limit);
}

SAMPLE INPUT AND OUTPUT:

Enter the Total Number of Processes: 5

Enter Details of 5 Processes


Enter Arrival Time: 2
Enter Burst Time: 6

Enter Arrival Time: 5


Enter Burst Time: 2

Enter Arrival Time: 1


Enter Burst Time: 8

Enter Arrival Time: 0


Enter Burst Time: 3

Enter Arrival Time: 4


Enter Burst Time: 4

Process Arrival Time Burst Time Waiting Time Turnaround Time


p1 2 6 7 13
p2 5 2 0 2
p3 1 8 14 22
p4 0 3 0 3
p5 4 4 2 6

Average Waiting Time=4.600000


Average Turnaround Time=9.200000

RESULT:

Thus, the scheduling algorithms have been successfully implemented using C


programming.

Register No: 190801001 Page No: 8

You might also like