0% found this document useful (0 votes)
152 views

1) Write A C Program To Implement First Come First Serve (FCFS) Algorithm Program

The document provides C program code to implement four different CPU scheduling algorithms: First Come First Serve (FCFS), Shortest Job First (SJF), Priority Scheduling, and Round Robin. For each algorithm, the code includes functions to input process details, calculate waiting times and turnaround times, and output performance metrics.

Uploaded by

Hrushikesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views

1) Write A C Program To Implement First Come First Serve (FCFS) Algorithm Program

The document provides C program code to implement four different CPU scheduling algorithms: First Come First Serve (FCFS), Shortest Job First (SJF), Priority Scheduling, and Round Robin. For each algorithm, the code includes functions to input process details, calculate waiting times and turnaround times, and output performance metrics.

Uploaded by

Hrushikesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1)Write a C program to implement First Come First Serve (FCFS) algorithm

PROGRAM:​/* FCFS */

#include<stdio.h>

void main()

int i,j,n,b[10],p[10],w[10],t[10];

float tw=0,tt=0,avgw,avgt;

printf("enter no.of process");

scanf("%d",&n);

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

printf("enter process id:");

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

printf("enter burst time:");

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

b[0]=0,w[0]=0;

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

w[i]=w[i-1]+b[i-1];

tw=tw+w[i];

t[i]=w[i]+b[i];
tt=tt+t[i];

avgw=tw/n;

avgt=tt/n;

printf("pid \t bt \t wt \t tat \n");

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

printf("p[%d]\t %d\t %d\t %d\n",i,b[i],w[i],t[i]);

printf("average waiting time is %f\n average turn around time is %f",avgw,avgt);

2) Write a C program to implement Shortest job First(SJF) algorithm

PROGRAM:​/* SJF */

#include<stdio.h>

int main()

int n,j,temp,temp1,temp2,pr[10],b[10],t[10],w[10],p[10],i;

float att=0,awt=0;

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

b[i]=0;w[i]=0;

}
printf("enter the number of process");

scanf("%d",&n);

printf("enter the burst times");

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

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

p[i]=i;

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

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

if(b[i]>b[j])

temp=b[i];

temp1=p[i];

b[i]=b[j];

p[i]=p[j];

b[j]=temp;

p[j]=temp1;

}
w[0]=0;

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

w[i+1]=w[i]+b[i];

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

t[i]=w[i]+b[i];

awt=awt+w[i];

att=att+t[i];

awt=awt/n;

att=att/n;

printf("\n\t process \t waiting time \t turn around time \n");

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

printf("\t\t p[%d] \t\t %d \t\t %d \n",p[i],w[i],t[i]);

printf("the average waitingtimeis %f\n",awt);

printf("the average turn around time is %f\n",att);

return 1;

3) Write a C program to implement Priority Scheduling algorithm

PROGRAM: /* priority */

#include<stdio.h>

void main()
{

int i,j,n,b[10],p[10],w[10],t[10],pr[10];

float tw=0,tt=0,avgw,avgt,temp1,temp2,temp3;

printf("enter no.of process");

scanf("%d",&n);

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

printf("enter process id:");

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

printf("enter burst time:");

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

printf(" enter priority:");

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

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

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

if(pr[i]>=pr[j])

temp1=b[i];

b[i]=b[j];

b[j]=temp1;

temp2=p[i];
p[i]=p[j];

p[j]=temp2;

temp3=pr[i];

pr[i]=pr[j];

pr[j]=temp3;

b[0]=0,w[0]=0;

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

w[i]=w[i-1]+b[i-1];

tw=tw+w[i];

t[i]=w[i]+b[i];

tt=tt+t[i];

avgw=tw/n;

avgt=tt/n;

printf("pid \t bt \t wt \t pr \t tat \n");

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

printf("p[%d]\t %d\t %d\t %d\t %d\n",p[i],b[i],w[i],pr[i],t[i]);

printf("average waiting time is %f\n average turn around time is %f",avgw,avgt);}


4)Write a C program to implement Round Robin scheduling algorithm

PROGRAM:​/* ROUND ROBIN */

#include<stdio.h>

void main()

int st[10],bt[10],wt[10],tat[10],n,tq;

int i,count=0,swt=0,stat=0,temp,sq=0;

float awt=0.0,atat=0.0;

printf("Enter number of processes:");

scanf("%d",&n);

printf("Enter burst time for sequences:");

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

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

st[i]=bt[i];

printf("Enter time quantum:");

scanf("%d",&tq);

while(1)

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

temp=tq;
if(st[i]==0)

count++;

continue;

if(st[i]>tq)

st[i]=st[i]-tq;

else

if(st[i]>=0)

temp=st[i];

st[i]=0;

sq=sq+temp;

tat[i]=sq;

if(n==count)

break;

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

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

swt=swt+wt[i];

stat=stat+tat[i];
}

awt=(float)swt/n;

atat=(float)stat/n;

printf("\tProcess_no \tBurst time\t Wait time \tTurn around time\n ");

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

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

printf("Avg wait time is %f Avg turn around time is %f",awt,atat);

You might also like