Professional Documents
Culture Documents
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[])
{
int i;
for ( i = 0; i < n ; i++)
tat[i] = burst_time[i] + wait_time[i];
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;
}
Process (2) : 8
Process (3) : 12
ALGORITHM:
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++)
{
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);
}
ALGORITHM:
1) Input the number of processes and their arrival time(at), burst time (bt).
3) Find process with minimum remaining time and 0 arrival time at every single time lap.
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;
int rt[limit];
for (int i = 0; i < limit; i++)
{
rt[i] = burst_time[i];
}
if (check == false)
{
t++;
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++;
}
RESULT: