FCFS SJF RR

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

//Name:Vaishnavi p.

mokal

//Class: SE Computer; Roll No. 36; Batch:S2

//Aim: Write a C program to implement FCFS scheduling algorithm.

Source Code:

#include<stdio.h>

#include<conio.h>

int main()

int n,bt[20],wt[20],tat[20],ts=0,avwt=0,avtat=0,i,j;

printf("Enter total number of processes(maximum 20):");

scanf("%d",&n);

printf("\nEnter Process Burst Time\n");

for(i=0;i<n;i++)

printf("P[%d]:",i+1);

scanf("%d",&bt[i]);

wt[0]=0; //waiting time for first process is 0

//calculating waiting time

for(i=1;i<n;i++)

wt[i]=0;

for(j=0;j<i;j++)

wt[i]+=bt[j];

}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");

//calculating turnaround time

for(i=0;i<n;i++)

tat[i]=bt[i]+wt[i];

avwt+=wt[i];

avtat+=tat[i];

printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);

avwt=avwt/n;

avtat/=i;

printf("\n\nAverage Waiting Time:%d",avwt);

printf("\nAverage Turnaround Time:%d\n",avtat);

for (i=0;i<n;i++)

printf("| p %d ",i);

printf("| \n");

for(i=0;i<=n;i++)

printf("%d ",ts);

ts=ts +bt[i];

return 0;

}
OUTPUT:
//Name: Vaishnavi p. mokal

//Class: SE Computer; Roll No. 36; Batch:S2

//Aim: Write a C program to implement SJF scheduling algorithm.

Source Code:

#include<stdio.h>

#include<conio.h>

void main()

int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,ts=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; //contains process number

//sorting burst time in ascending order using selection sort

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; //waiting time for first process will be zero

//calculate waiting time

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; //average waiting time

total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");

for(i=0;i<n;i++)

tat[i]=bt[i]+wt[i]; //calculate turnaround time

total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);

avg_tat=(float)total/n; //average turnaround time

printf("\n\nAverage Waiting Time=%f",avg_wt);

printf("\nAverage Turnaround Time=%f\n",avg_tat);

for (i=0;i<n;i++)

printf("| p %d ",p[i]);

printf("| \n");

for(i=0;i<=n;i++)

printf("%d ",ts);

ts=ts +bt[i];

return 0;

}
OUTPUT:

//Name:Vaishnavi p. mokal

//Class: SE Computer; Roll No. 36; Batch:S2

//Aim: Write a C program to implement Round Robin scheduling algorithm.


Source Code:

#include<stdio.h>

int at[100],bt[100],rt[100],temp[100];

float wait_time=0,turn_time=0;

void main()

int c,j,n,time,r,flag=0,time_q,ltt,i,wt=0;

printf("Enter no.of process:");

scanf("%d",&n);

r=n;

for(c=0;c<n;c++)

printf("Enter arrival time of p%d:\t",c+1);

scanf("%d",&at[c]);

printf("Enter burst time of p%d: \t",c+1);

scanf("%d",&bt[c]);

rt[c]=bt[c];

temp[c]=bt[c];

printf("\n");

printf("Enter time quantum:\t");

scanf("%d",&time_q);

printf("\n\n\tprocess\tAT\tBT\tTAT\tWT \n\n");

for(time=0,c=0;r!=0;)

if(rt[c]<=time_q && rt[c]>0)

time=time+rt[c];

rt[c]=0;

flag=1;

}
else if (rt[c]>0)

rt[c]=rt[c]-time_q;

time=time+time_q;

if(rt[c]==0 && flag==1)

wt=0;

wt=time-at[c]-bt[c];

r--;

printf("\tP%d\t%d\t%d\t%d\t%d\n",c+1,at[c],bt[c],time-at[c],wt);

ltt=time-at[c];

wait_time=wait_time+time-at[c]-bt[c];

turn_time=turn_time+time-at[c];

flag=0;

if(c==n-1)

c=0;

else if(at[c+1]<=time)

c++;

else

c=0;

j=0;

printf("\n\n\n");

printf("Gantt Chart ");

printf("\n\n\n");

printf("\t");

for(i=at[0];i<time;)

if(bt[j]>=time_q)

{
printf("P%d\t",j+1);

i+=time_q;

bt[j]=bt[j]-time_q;

else if(bt[j]>0)

printf("p%d\t",j+1);

i+=bt[j];

bt[j]=0;

j++;

if(j>=n)

j=0;

printf("\n");

j=0;

printf("\t");

for(i=at[0];i<time;)

if(temp[j]>=time_q)

printf("%d\t",i+time_q);

i+=time_q;

temp[j]=temp[j]-time_q;

else if(temp[j]>0)

printf("%d\t",i+temp[j]);

i+=temp[j];
temp[j]=0;

j++;

if(j>=n)

j=0;

printf("\n\n\n");

printf("\nAverage_waiting_time=%f\n",wait_time/n);

printf("Average_turn_around_time=%f\n",turn_time/n);

printf("\n\n");

OUTPUT:

You might also like