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

Operating Systems Lab Programs

Uploaded by

padamatiroshna
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Operating Systems Lab Programs

Uploaded by

padamatiroshna
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

1.

Write C programs to simulate the following CPU Scheduling algorithms:


a) FCFS CPU SCHEDULING ALGORITHM

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.

ALGORITHM:
Step 1: Start
Step 2: Define a structure process with elements p,bt,wt,tat.
Step 3: Read the Processes details p, & bt
Step 4: Initialize wt[0]=avgwt=0;
avgtat=tat[0]=bt[0];
Step 5:for i=1 to i<n do till
Step 6:wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i]; avgwt=avgwt+wt[i];
avgtat=avgtat+tat[i];
Step7: for i=0 to n do step 8
Step8: Print the output with the FCFS Fashion and Calculating bt,wt,&tat
Step 9: End
PROGRAM: FCFS CPU SCHEDULING ALGORITHM
#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\t B_time\t w_time\t turnarounftime\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:
b) SJF CPU SCHEDULING ALGORITHM
DESCRIPTION:
For SJF (Shortest Job F i r s t ) scheduling algorithm, read t h e 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 according to the length of its burst time. Then calculate
the waiting time and turnaround time of each of the processes accordingly.

ALGORITHM:
Step 1: Start
Step 2: Define a structure process with elements p,bt,wt,tat
Step3: Read process name P, burst time bt of the process
Step4: for i=0 to n go to step 6
Step:5 for j=0;j< i do
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;
}
Step6: else avgwt=wt[0]=0;
avgtat=tat[0]=bt[0];
Step 7: for i=1;i<n do
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
avgwt=avgwt+wt[i];
avgtat=avgtat+tat[i];
Step 8: Print the output with the SJF Fashion and Calculating Pid,bt,wt,&tat
Step 9: End
Program:
#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 name:\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_name\t B_time\t w_time\t turnarounftime\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("\n avg waiting time=%f\n", avgwt/n);
printf("avg tat time=%f\n", avgtat/n);
}

OUTPUT:
c) ROUND ROBIN CPU SCHEDULING ALGORITHM
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.

ALGORITHM:
Step 1: Start
Step 2: Define a structure process with elements st,bt,wt,tat,n,tq
Step 3: Read i,n,tq
Step 4: Read the Processes details n & bt
Step 5: for i=0 to i<n do st[i]=bt[i]
Step 6: for i=0,count=0;i<n; do till step 7
Step 7: check 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;
Step 8: if (n= =count)
break;
Step 9: else
wt[i]=tat[i]-bt[i];
avgwt=avgwt+wt[i];
avgtat=avgtat+tat[i];
Step 10: Print the output with the RoundRobin Fashion and Calculating Pid, bt, wt &tat
Step 11: End
Program:
#include<stdio.h>
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,atat;
printf("enter the number of processes"); scanf("%d",&n);
printf("enter the burst time of each process \n");
for(i=0;i<n;i++)
{
printf(("p%d",i+1);
scanf("%d",&bt[i]);
st[i]=bt[i];
}
printf("enter the time quantum");
scanf("%d",&tq);
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=t
q;
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==co
unt)
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("process no\t burst time\t waiting time\t turnaround time\n");
for(i=0;i<n;i++)
printf("%d\t\t %d\t\t %d\t\t %d\n",i+1,bt[i],wt[i],tat[i]);
printf("avg wt time=%f,avg turn around time=%f",awt,atat);
}
OUTPUT:
d) PRIORITY CPU SCHEDULING ALGORITHM 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.

ALGORITHM:
Step1: Start
Step2: Define a structure process with elements p, bt, wt, tatSte.
Step3: Read the Processes details pid, & bt
Step4: for i=0 to i<n do still step5
Step5: for j=0 to j<n do till step 6
Step6: 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;
Step7: initialize avgwt=wt[0]=0;
avgtat=tat[0]=bt[0];
Step8: for i=1;i<n do till step 9
Step9: wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
avgwt=avgwt+wt[i];
avgtat=avgtat+tat[i];
Step10: Print the output with the FCFS Fashion and Calculating
Pid,bt,wt,&tat
Step11 : End
PROGRAM: PRIORITY CPU SCHEDULING ALGORITHM

#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;
system ("clear");
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_name\t B_time\t w_time\t turnarounftime\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:

You might also like