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

EXNO3_OS

The document outlines the implementation of four scheduling algorithms in C: First Come First Serve (FCFS), Shortest Job First (SJF), Round Robin, and Priority Scheduling. Each section includes the aim, algorithm steps, program code, sample output, and results indicating successful implementation. The algorithms compute waiting and turnaround times for processes based on their respective scheduling methods.

Uploaded by

vishal.cs23
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)
9 views

EXNO3_OS

The document outlines the implementation of four scheduling algorithms in C: First Come First Serve (FCFS), Shortest Job First (SJF), Round Robin, and Priority Scheduling. Each section includes the aim, algorithm steps, program code, sample output, and results indicating successful implementation. The algorithms compute waiting and turnaround times for processes based on their respective scheduling methods.

Uploaded by

vishal.cs23
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/ 15

EX.NO.

3A: IMPLEMENTATION OF FCFS SCHEDULING ALGORITHM


DATE:
AIM
To write a C program to implement First Come First Serve scheduling algorithm.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the input process and their burst time.
Step 3: Sort the processes based on order in which it requests CPU.
Step 4: Compute the waiting time and turnaround time for each process.
Step 5: Calculate the average waiting time and average turnaround time.
Step 6: Print the details about all the processes.
Step 7: Stop the program.
PROGRAM :
#include<stdio.h>
Void main()
{
int bt[50],wt[80],at[80],wat[30],ft[80],tat[80]; int i,n;
float awt,att,sum=0,sum1=0;char
p[10][5];
printf("\nenter the number of process ");
scanf("%d",&n);
printf("\nEnter the process name and burst-time:");
for(i=0;i<n;i++)scanf("%s%d",p[i],&bt[i]);
printf("\nEnter the arrival- time:");
for(i=0;i<n;i++)
scanf("%d",&at[i]);
wt[0]=0;
for(i=1;i<=n;i++)
wt[i]=wt[i-1]+bt[i-1];
ft[0]=bt[0]; for(i=1;i<=n;i++)
ft[i]=ft[i-1]+bt[i];
printf("\n\n\t\t\tGANTT CHART\n");
printf("\n \n");
for(i=0;i<n;i++)
printf("|\t%s\t",p[i]);
printf("|\t\n");
printf("\n \n");
printf("\n");
for(i=0;i<n;i++)
printf("%d\t\t",wt[i]);
printf("%d",wt[n]+bt[n]); printf("\n \n");
printf("\n"); for(i=0;i<n;i++)
wat[i]=wt[i]-at[i]; for(i=0;i<n;i++)
tat[i]=wat[i]-at[i];
printf("\n FIRST COME FIRST SERVE\n");
printf("\n Process Burst-time Arrival-time Waiting-time Finish-time Turnaround-time\n");
for(i=0;i<n;i++)
printf("\n\n %d%s \t %d\t\t %d \t\t %d\t\t %d
\t\t%d",i+1,p[i],bt[i],at[i],wat[i],ft[i],tat[i]);
for(i=0;i<n;i++)
sum=sum+wat[i];
awt=sum/n;
for(i=0;i<n;i++)
sum1=sum1+bt[i]+wt[i];
att=sum1/n;
printf("\n\nAverage waiting time:%f",awt);
printf("\n\nAverage turnaround time:%f",att);
}
}
OUTPUT:
enter the number of process 3
Enter the process name and burst-time:p1 2 p2 3
p3 4
Enter the arrival-time:0 1 2
GANTT CHART
| p1 | p2 | p3 |
0 2 5 9

FIRST COME FIRST SERVE


Process Burst-time Arrival-time Waiting-time Finish-time
Turnaround-time
p1 2 0 0 2 2
p2 3 1 1 5 4
p3 4 2 3 9 7
Average waiting time:1.333333 Average turnaround time:5.333333

RESULT:
The FCFS scheduling algorithm has been implemented in C.
EX.NO.3B : IMPLEMENTATION OF SJF SCHEDULING ALGORITHM
DATE:

AIM
To write a C program to implement shortest job first (non-pre-emptive) scheduling
algorithm.

ALGORITHM:
Step 1: Start the program.
Step 2: Get the input process and their burst time. Step 3: Sort the processes based on burst
time.
Step 4: Compute the waiting time and turnaround time for each process. Step 5: Calculate
the average waiting time and average turnaround time. Step 6: Print the details about all the
processes.
Step 7: Stop the program.
PROGRAM:
#include<stdio.h>void main()
{
int i,j,n,bt[30],at[30],st[30],ft[30],wat[30],wt[30],temp,temp1,tot,tt[30];float awt, att; int
p[15];
wat[1]=0;
printf("ENTER THE NO.OF PROCESS");
scanf("%d",&n);
printf("\nENTER THE PROCESS NUMBER,BURST TIME AND ARRIVALTIME");
for(i=1;i<=n;i++)
{
scanf("%d\t %d\t %d",&p[i],&bt[i],&at[i]);
}
printf("\nPROCESS\tBURSTTIME\tARRIVALTIME");
for(i=1;i<=n;i++)
{
printf("\np%d\t%d\t\t%d",p[i],bt[i],at[i]);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(bt[i]>bt[j])
{
temp=bt[i]; bt[i]=bt[j]; bt[j]=temp; temp1=p[i]; p[i]=p[j]; p[j]=temp1;
}
}
if(i==1)
{
}
else
{ st[1]=0;
ft[1]=bt[1]; wt[1]=0;
st[i]=ft[i-1];
ft[i]=st[i]+bt[i];
wt[i]=st[i];
}
}
printf("\n\n\t\t\tGANTT CHART\n"); printf("\n\n");
for(i=1;i<=n;i++)
printf("|\tp%d\t",p[i]); printf("|\t\n"); printf("\n\n");
printf("\n");

for(i=1;i<=n;i++)
printf("%d \t\t",wt[i]);
printf("%d",wt[n]+bt[n]); printf("\n \n"); for(i=2;i<=n;i++)
wat[i]=wt[i]-at[i]; for(i=1;i<=n;i++)
tt[i]=wat[i]+bt[i]-at[i]; printf("\nPROCESS\tBURSTTIME\tARRIVALTIME\tWAITINGTIME\tT
URNAROUNDTI ME\n");
for(i=1;i<=n;i++)
{
printf("\np%d %5d %15d %15d %15d",p[i],bt[i],at[i],wat[i],tt[i]);
}
for(i=1,tot=0;i<=n;i++)tot+=wt[i]; awt=(float)tot/n;
printf("\n\n\n AVERAGE WAITING TIME=%f",awt);for(i=1,tot=0;i<=n;i++) tot+=tt[i];
att=(float)tot/n;
printf("\n\n AVERAGE TURNAROUND TIME=%f",att);
}

OUTPUT:
enter the no.of process3
enter the process number,burst time and arrival time1 8 1
251
331

PROCESS BURSTTIME ARRIVALTIME WAITINGTIME TURNAROUNDTIME


p3 3 1 0 2
p2 5 1 2 6
p1 8 1 7 14
AVERAGE WAITING TIME=3.666667 AVERAGE TURNAROUND TIME=7.333333

RESULT:
The SJF scheduling algorithm has been implemented in C.
EX.NO.3C:IMPLEMENTATION OF ROUND ROBIN SCHEDULING ALGORITHM
DATE:

AIM:
To write a C program to implement Round Robin scheduling algorithm.

ALGORITHM:
Step 1: Start the program.
Step 2: Get the input process and their burst time.Step

3: Sort the processes based on priority.


Step 4: Compute the waiting time and turnaround time for each process. Step 5: Calculate
the average waiting time and average turnaround time. Step 6: Print the details about all the
processes.
Step 7: Stop the program.
PROGRAM:
#include<stdio.h> voidmain()
{
int ct=0,y[30],j=0,bt[10],cwt=0; int tq,i,max=0,n,wt[10],t[10],at[10],tt[10],b[10]; float
a=0.0,s=0.0;
char p[10][10];
printf("\n enter the no of process:"); scanf("%d",&n);
printf("\nenter the time quantum");scanf("%d",&tq);
printf("\nenter the process name,bursttime,arrival time"); for(i=0;i<n;i++)
{
scanf("%s",p[i]);
scanf("%d",&bt[i]); scanf("%d",&at[i]); wt[i]=t[i]=0;b[i]=bt[i];
}
printf("\n\t\tGANTT CHART"); printf("\n \n");
for(i=0;i<n;i++)
{
if(max<bt[i])
max=bt[i];
}
while(max!=0)
{
for(i=0;i<n;i++)
{
if(bt[i]>0)
{
if(ct==0)
wt[i]=wt[i]+cwt;
else

}
if(bt[i]==0) cwt=cwt+0;

wt[i]=wt[i]+(cwt-t[i]);

else if(bt[i]==max)
{
if(bt[i]>tq)
{
cwt=cwt+tq;

}
else
{
bt[i]=bt[i]-tq; max=max-tq;

cwt=cwt+bt[i]; bt[i]=0; max=0;

}
else if(bt[i]<tq)
{

}
printf("|\t%s",p[i]); y[j]=cwt; j++;

cwt=cwt+bt[i]; bt[i]=0; printf("|\t%s",p[i]); y[j]=cwt;


j++;
}
else if(bt[i]>tq)
{
cwt=cwt+tq; bt[i]=bt[i]-tq;
printf("|\t%s",p[i]); y[j]=cwt;
j++;
}
else if(bt[i]==tq)
{
cwt=cwt+bt[i];
printf("|\t%s",p[i]); bt[i]=0;y[j]=cwt; j++;
}

ct=ct+1;
}

t[i]=cwt;
}

for(i=0;i<n;i++)
{
wt[i]=wt[i]-at[i]; a=a+wt[i]; tt[i]=wt[i]+b[i]-at[i]; s=s+tt[i];
}
a=a/n; s=s/n; printf("\n ");
printf("\n0"); for(i=0;i<j;i++)
printf("\t%d",y[i]); printf("\n");
printf("\n "); printf("\n\t\t ROUND ROBIN\n");
printf("\n Process Burst-time Arrival-time Waiting-time Turnaround-
time\n");
for(i=0;i<n;i++)
printf("\n\n %d%s \t %d\t\t %d \t\t %d\t\t %d", i+1, p[i], b[i], at[i],wt[i], tt[i]);
printf("\n\nAvg waiting time=%f",a);
printf("\n\nAvgturn around time=%f",s);
}
OUTPUT:
enter the no of process:3 enter the time quantum2
enter the process name, bursttime, arrival time

p1 2 0
p2 3 1
p3 4 2
GANTT CHART
| p1| p2| p3| p2| p3
0 2 4 6 7 9

ROUND ROBIN

Process Burst-time Arrival-time Waiting-time Turnaround-time


p1 2 0 0 2
p2 3 1 3 5
p3 4 2 3 5
Avg Waiting Time=2.000000 Avg Turnaround Time=4.000000

RESULT
The Round Robin scheduling algorithm has been implemented in C.
EX.NO.3D: IMPLEMENTATION OF PRIORITY SCHEDULING ALGORITHM
DATE:

AIM
To write a C program to implement Priority Scheduling algorithm.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the input process and their burst time. Step 3: Sort the processes based on
priority.
Step 4: Compute the waiting time and turnaround time for each process.
Step 5: Calculate the average waiting time and average turnaround time. Step 6: Print the
details about all the processes.
Step 7: Stop the program.
PROGRAM:
#include<stdio.h> #include<string.h>
void main()
{
int bt[30],pr[30],np; intwt[30],tat[30],wat[30],at[30],ft[30];int i,j,x,z,t; float
sum1=0,sum=0,awt,att;char
p[5][9],y[9];
printf("\nenter the number of process"); scanf("%d",&np);
printf("\nEnter the process,burst-time and priority:"); for(i=0;i<np;i++)
scanf("%s%d%d",p[i],&bt[i],&pr[i]); printf("\nEnter the arrival-time:"); for(i=0;i<np;i++)
scanf("%d",&at[i]); for(i=0;i<np;i++)
for(j=i+1;j<np;j++)
{
if(pr[i]>pr[j])
{
x=pr[j]; pr[j]=pr[i]; pr[i]=x; strcpy(y,p[j]);
strcpy(p[j],p[i]);
strcpy(p[i],y);
z=bt[j]; b t[j]=bt[i]; bt[i]=z;
}
} wt[0]=0;
for(i=1;i<=np;i++)
wt[i]=wt[i-1]+bt[i-1];
ft[0]=bt[0]; for(i=1;i<np;i++)
ft[i]=ft[i-1]+bt[i]; printf("\n\n\t\tGANTT CHART\n");printf("\n
\n"); for(i=0;i<np;i++)
printf("|\t%s\t",p[i]);
printf("|\t\n");
printf("\n \n");
printf("\n"); for(i=0;i<=np;i++)
printf("%d\t\t",wt[i]);
printf("\n \n");
printf("\n"); for(i=0;i<np;i++)
wat[i]=wt[i]-at[i]; for(i=0;i<np;i++)
tat[i]=wat[i]-at[i]; printf("\nPRIORITY SCHEDULING:\n");
printf("\nProcess Priority Burst-time Arrival-time Waiting-time Turnaround-time");
for(i=0;i<np;i++)
printf("\n\n%d%s\t%d\t\t%d\t\t%d\t%d\t\t%d",i+1,p[i],pr[i],bt[i],a t[i],wt[i],tat[i]);
for(i=0;i<np;i++)
sum=sum+wat[i]; awt=sum/np; for(i=0;i<np;i++)
sum1=sum1+tat[i]; att=sum1/np;
printf("\n\nAverage waiting time:%f",awt); printf("\n\nAverageturn aroundtime is:%f",att);
}
OUTPUT:
Enter the number of process3
Enter the process, burst-time and priority:p1 3 3 p2 4 2
p3 5 1
Enter the arrival-time: 0 1 2
GANTT CHART

| p3 | p2 | p1 |

0
5
9
12

PRIORITY SCHEDULING:
Process PriorityBurst-time Arrival-time Waiting-time Turnaround-time
p3 1 5 0 0 0
p2 2 4 1 5 3
p1 3 3 2 9 5
Average waiting time: 3.666667 Average turnaround time is: 2.666667
RESULT
The Priority scheduling algorithm has been implemented in C.

You might also like