You are on page 1of 6

LAB-04

Aim: Programs to simulate the following non-preemptive CPU scheduling algorithms to find the
Turn Around Time and Waiting Time. a.FCFS b.SJF

First Come First Serve(FCFS): FCFS is a non-preemptive scheduling algorithm first arrived
process into ready queue is scheduled to CPU first. CPU scheduling is based on arrival times.

Processes arriving at same time intervals:-

Program:-

#include<stdio.h>
int main(){
int n,i;
printf("Enter no.of processes:");
scanf("%d",&n);
int bt[n],wt[n],tat[n];
for(i=0;i<n;i++){
printf("Enter burst time of process p%d:",i+1);
scanf("%d",&bt[i]);
}
int time=0;
for(i=0;i<n;i++){
wt[i]=time;
time+=bt[i];
tat[i]=time;
}
printf("\n\nGantt chart:\n\n");
for(i=0;i<n;i++){
printf("|%d|-->p%d-->",wt[i],i+1);
if(i==n-1)
printf("|%d|\n\n",tat[n-1]);
}
float wtotal=0,ttotal=0;
printf("Processes\tBT\tFT\tWT\tTAT\n");
for(i=0;i<n;i++){
wtotal+=wt[i];
ttotal+=tat[i];
printf("P%d\t\t%d\t%d\t%d\t%d\n",i+1,bt[i],tat[i],wt[i],tat[i]);
}
printf("\nAverage Waiting Time = %f\n",wtotal/n);
printf("Average Turn Around Time = %f\n",ttotal/n);
}
Output:-

Processes arriving at different time intervals:-

#include<stdio.h>
int main()
{
int n,i,j,temp;
printf("Enter no.of processes:");
scanf("%d",&n);
int pid[n],bt[n],at[n],wt[n],tat[n],ft[n];
for(i=0;i<n;i++)
{
pid[i]=i+1;
printf("Enter arrival time of process p%d:",i+1);
scanf("%d",&at[i]);
printf("Enter burst time of process p%d:",i+1);
scanf("%d",&bt[i]);
}
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){
if(at[j]>at[j+1]){
temp=at[j];
at[j]=at[j+1];
at[j+1]=temp;
temp=bt[j];
bt[j]=bt[j+1];
bt[j+1]=temp;
temp=pid[j];
pid[j]=pid[j+1];
pid[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
{
if(i==0){
wt[0]=at[0];
ft[0]=at[0]+bt[0];
}
else if(at[i]>ft[i-1])
ft[i]=at[i]+bt[i];
else
ft[i]=ft[i-1]+bt[i];

tat[i]=ft[i]-at[i];
wt[i]=tat[i]-bt[i];
}
printf("Processes\tAT\tBT\tFT\tWT\tTAT\n");
for(i=0;i<n;i++)
{
printf("P%d\t\t%d\t%d\t%d\t%d\t%d\n",pid[i],at[i],bt[i],ft[i],wt[i],tat[i]);
}
float wtotal=0,ttotal=0;
printf("\nGantt chart\n");
for(i=0;i<n;i++)
{
if(i==0 && at[0]!=0)
printf("0->idle->%d",at[0]);
if(i!=0 && at[i]>ft[i-1])
printf("->idle->%d",at[i]);
if(i==0 && at[0]==0)
printf("%d",wt[i]);
printf("->p%d->%d",pid[i],ft[i]);
wtotal+=wt[i];
ttotal+=tat[i];
}
printf("\n\nAverage Waiting Time = %f\n",wtotal/n);
printf("Average Turn Around Time = %f\n",ttotal/n);
}
Output:-

Shortest Job First(SJF): SJF is a non-preemptive scheduling algorithm. Process with less
execution time will be scheduled to CPU first.

Processes arriving at same time intervals:-

Program:-

#include<stdio.h>
int main(){
int n,i,j,temp;
printf("Enter no.of processes:");
scanf("%d",&n);
int pid[n],bt[n],wt[n],tat[n];
for(i=0;i<n;i++){
pid[i]=i+1;
printf("Enter burst time of process p%d:",i+1);
scanf("%d",&bt[i]);
}
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){
if(bt[j]>bt[j+1]){
temp=bt[j];
bt[j]=bt[j+1];
bt[j+1]=temp;
temp=pid[j];
pid[j]=pid[j+1];
pid[j+1]=temp;
}
}
}
int time=0;
for(i=0;i<n;i++){
wt[i]=time;
time+=bt[i];
tat[i]=time;
}
printf("\n\nGantt chart:\n\n");
for(i=0;i<n;i++){
printf("|%d|-->p%d-->",wt[i],pid[i]);
if(i==n-1)
printf("|%d|\n\n",tat[n-1]);
}
printf("Processes\tBT\tFT\tWT\tTAT\n");
float wtotal=0,ttotal=0;
for(i=0;i<n;i++){
wtotal+=wt[i];
ttotal+=tat[i];
printf("P%d\t\t%d\t%d\t%d\t%d\n",pid[i],bt[i],tat[i],wt[i],tat[i]);
}
printf("\nAverage Waiting Time = %f\n",wtotal/n);
printf("Average Turn Around Time = %f\n",ttotal/n);
}

Output:-

You might also like