🖥️
B.TECH II YEAR • II SEMESTER • R22

Operating Systems - Lab Manual

Operating Systems Laboratory • Program Solution

📘 R22 Regulation 🎓 II Year 📚 II Semester 🖥️ Operating Systems 🧪 Lab Program
Selected Program

☛   Write C programs to simulate the following CPU Scheduling algorithms a) FCFS b) SJF c) RoundRobin d) Priority

← All Operating Systems Programs

Aim:

☛   Write C programs to simulate the following CPU Scheduling algorithms
a) FCFS
b) SJF
c) RoundRobin
d) Priority

Solution :

a) FCFS

DESCRIPTION:

For FCFS scheduling algorithm, read the number of processes/jobs in the system, their CPU burst times. The scheduling is performed on the basis of arrival time of the processes irrespective of their other parameters. Each process will be executed according to its arrival time. Calculate the waiting time and turnaround time of each of the processes accordingly.

PROGRAM: ( fcfs_cpu.c )

 
#include <stdio.h>
int main( )
{
char p[10][10];
int bt[10],wt[10],tat[10],i,n;
float avgwt,avgtat;
printf("Enter no of processes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process %d name:\t",i+1);
scanf("%s",p[i]);
printf("Enter burst time : \t");
scanf("%d",&bt[i]);
}
wt[0]=avgwt=0;
avgtat=tat[0]=bt[0];
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
avgwt=avgwt+wt[i];
avgtat=avgtat+tat[i];
}
printf("P_name\tB_time\tW_time\tTAT\n");
for(i=0;i<n;i++) {
	printf("%s\t%d\t%d\t%d\n",p[i],bt[i],wt[i],tat[i]);
}
printf("\nAvg Waiting Time=%f", avgwt/n);
printf("\nAvg TAT time=%f\n", avgtat/n);
return 0;
}


OUTPUT:

 
$ gcc fcfs_cpu.c
$ ./a.out
Enter no of processes: 3
Enter process 1 name:   P1
Enter burst time :      3
Enter process 2 name:   P2
Enter burst time :      2
Enter process 3 name:   P3
Enter burst time :      5
P_name  B_time  W_time  TAT
P1      3       0       3
P2      2       3       5
P3      5       5       10

Avg Waiting Time=2.666667
Avg TAT time=6.000000



b) SJF

DESCRIPTION:

For SJF(Shortest Job First) scheduling algorithm, read the number of processes/jobs in the system, their CPU burst times. Arrange all the jobs in order with respect to their burst times. There may be two jobs in queue with the same execution time, and then FCFS approach is to be performed. Each process will be executed acco ding to the length of its burst time. Then calculate the waiting time and turnaround time of each of the processes accordingly.

PROGRAM: ( sjf_cpu.c )

 
#include <stdio.h>
int main()
{
int i,j,k,n,temp;
int p[10],bt[10],wt[10],tat[10];
float avgtat,avgwt;
printf("Enter no of processes: \t");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process number :\t");
scanf("%d",&p[i]);
printf("Enter burst time \t");
scanf("%d",&bt[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
if(bt[i]<bt[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
k=p[i];
p[i]=p[j];
p[j]=k;
}
}
}
avgwt=wt[0]=0;
avgtat=tat[0]=bt[0];
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
avgwt=avgwt+wt[i];
avgtat=avgtat+tat[i];
}
printf("P_No\tB_time\tW_time\tTAT\n");
for(i=0;i<n;i++) {
	printf("%d\t%d\t%d\t%d\n",p[i],bt[i],wt[i],tat[i]);
}
printf("\nAvg waiting time=%f\n", avgwt/n);
printf("Avg TAT =%f\n", avgtat/n);
}


OUTPUT:

 
$ gcc sjf_cpu.c
$ ./a.out
Enter no of processes:  3
Enter process number :  1
Enter burst time        4
Enter process number :  2
Enter burst time        2
Enter process number :  3
Enter burst time        6
P_No    B_time  W_time  TAT
2       2       0       2
1       4       2       6
3       6       6       12

Avg waiting time=2.666667
Avg TAT =6.666667



c) RoundRobin

DESCRIPTION:

For round robin scheduling algorithm, read the number of processes/ jobs in the system, their CPU burst times, and the size of the time slice. Time slices are assigned to each process in equal portions and in circular order, handling all processes execution. This allows every process to get an equal chance.

PROGRAM: ( rr_cpu.c )

 
#include <stdio.h>
#include <stdlib.h>
int main()
{
int p[10],st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,temp,sq=0;
float avgwt=0.0,avgtat=0.0;
printf("Enter number of processes:  ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process number:\t");
scanf("%d",&p[i]);
printf("Enter burst time:\t");
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];
avgwt=avgwt+wt[i];
avgtat=avgtat+tat[i];
}
printf("P_NO\t B_Time\t W_Time\t TAT\n");
for(i=0;i<n;i++)
printf("%d\t %d\t %d\t %d\t\n",i+1,bt[i],wt[i],tat[i]);
printf("Avg waiting time is %f\n Avg TAT is %f\n",avgwt/n,avgtat/n);
}

OUTPUT:

 
$ gcc rr_cpu.c
$ ./a.out
Enter number of processes:  3
Enter process number:   1
Enter burst time:       10
Enter process number:   2
Enter burst time:       3
Enter process number:   3
Enter burst time:       4
Enter time quantum:2
P_NO     B_Time  W_Time  TAT
1        10      7       17
2        3       6       9
3        4       7       11
Avg waiting time is 6.666667
 Avg TAT is 12.333333



d) Priority

DESCRIPTION:

For priority scheduling algorithm, read the number of processes/jobs in the system, their CPU burst times, and the priorities. Arrange all the jobs in order with respect to their priorities. There may be two jobs in queue with the same priority, and then FCFS approach is to be performed. Each process will be executed according to its priority. Calculate the waiting time and turnaround time of each of the processes accordingly.

PROGRAM: ( priority_cpu.c )

 
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,n,temp;
int p[10],pr[10],bt[10],wt[10],tat[10];
float avgtat,avgwt;
printf("Enter no of processes:\t");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process number:\t");
scanf("%d",&p[i]);
printf("Enter burst time:\t");
scanf("%d",&bt[i]);
printf("Enter priority:\t");
scanf("%d",&pr[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(pr[i]<pr[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pr[i];
pr[i]=pr[j];
pr[j]=temp;
}
}
}
avgwt=wt[0]=0;
avgtat=tat[0]=bt[0];
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
avgwt=avgwt+wt[i];
avgtat=avgtat+tat[i];
}
printf("P_No\tB_time\tW_time\tTAT\n");
for(i=0;i<n;i++)
printf("%d\t%d\t%d\t%d\n",p[i],bt[i],wt[i],tat[i]);
printf("\nAvg waiting time=%f\n", avgwt/n);
printf("Avg TAT time=%f\n", avgtat/n);
}

OUTPUT:

 
$ gcc priority_cpu.c
$ ./a.out
Enter no of processes:  3
Enter process number:   1
Enter burst time:       3
Enter priority: 2
Enter process number:   2
Enter burst time:       4
Enter priority: 1
Enter process number:   3
Enter burst time:       6
Enter priority: 3
P_No    B_time  W_time  TAT
3       6       0       6
1       3       6       9
2       4       9       13

Avg waiting time=5.000000
Avg TAT time=9.333333



💻 Run This C Program

☛   Write C programs to simulate the following CPU Scheduling algorithms a) FCFS b) SJF c) RoundRobin d) Priority
The selected C program is loaded automatically into the editor. Edit the program, provide input if required and click Run.

The compiler is embedded using OneCompiler. Your C program is compiled and run in the embedded Linux environment; Study Glance does not execute C code directly on your web server. Some Operating Systems programs (fork, pipes, FIFOs, message queues, shared memory and semaphores) may be restricted in an online sandbox. If a program does not run here, compile it on a Linux system with gcc.
← Back to Operating Systems Lab Programs 📚 All Lab Manuals