4th sem practical exam -2021-(operting system)
4th sem practical exam -2021-(operting system)
return 0;
}
O/P:-
2) Write a program using C to implement Round Robin scheduling
algorithm.
#include<stdio.h>
#include<conio.h>
void main()
{
// initlialize the variable name
int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];
float avg_wt, avg_tat;
printf(" Total number of process in the system: ");
scanf("%d", &NOP);
y = NOP; // Assign the number of process to variable y
// Use for loop to enter the details of the process like Arrival time and the Burst Time
for(i=0; i<NOP; i++)
{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
printf(" Arrival time is: \t"); // Accept arrival time
scanf("%d", &at[i]);
printf(" \nBurst time is: \t"); // Accept the Burst time
scanf("%d", &bt[i]);
temp[i] = bt[i]; // store the burst time in temp array
}
// Accept the Time qunat
printf("Enter the Time Quantum for the process: \t");
scanf("%d", &quant);
// Display the process No, burst time, Turn Around Time and the waiting time
printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )
{
if(temp[i] <= quant && temp[i] > 0) // define the conditions
{
sum = sum + temp[i];
temp[i] = 0;
count=1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - quant;
sum = sum + quant;
}
if(temp[i]==0 && count==1)
{
y--; //decrement the process no.
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-
at[i]-bt[i]);
wt = wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0;
}
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i=0;
}
}
// represents the average waiting time and Turn Around time
avg_wt = wt * 1.0/NOP;
avg_tat = tat * 1.0/NOP;
printf("\n Average Turn Around Time: \t%f", avg_wt);
printf("\n Average Waiting Time: \t%f", avg_tat);
getch();
}
Output:
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
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;
total=0;
printf("nProcesst Burst Time tWaiting TimetTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("np%dtt %dtt %dttt%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("nnAverage Waiting Time=%f",avg_wt);
printf("nAverage Turnaround Time=%fn",avg_tat);
}
Output:
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
scanf("%d",&n);
for(i=0;i<n;i++)
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
//sorting burst time, priority and process number in ascending order using selection sort
for(i=0;i<n;i++)
pos=i;
for(j=i+1;j<n;j++)
if(pr[j]<pr[pos])
pos=j;
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
for(i=1;i<n;i++)
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
total=0;
for(i=0;i<n;i++)
total+=tat[i];
return 0;
Output:
5)Writea program using C to implement preemptive priority
based scheduling algorithm.
#include<stdio.h>
int main()
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
scanf("%d",&n);
for(i=0;i<n;i++)
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
//sorting burst time, priority and process number in ascending order using selection
sort
for(i=0;i<n;i++)
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
for(i=1;i<n;i++)
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
total=0;
for(i=0;i<n;i++)
total+=tat[i];
return 0;
o/p:
b[9]=9999;
for(time=0;count!=n;time++)
{
smallest=9;
for(i=0;i<n;i++)
{
if(a[i]<=time && b[i]<b[smallest] && b[i]>0 )
smallest=i;
}
b[smallest]--;
if(b[smallest]==0)
{
count++;
end=time+1;
avg=avg+end-a[smallest]-x[smallest];
tt= tt+end-a[smallest];
}
}
printf("\n\nAverage waiting time = %lf\n",avg/n);
printf("Average Turnaround time = %lf",tt/n);
return 0;
}
output:-
Output:
Program code for worst fit:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
clrscr();
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files:-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no \tFile_size \tBlock_no \tBlock_size \tFragment");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
Output
8)Write a program to copy files using system calls.
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
void main()
{
char buf;
int fd_one, fd_two;
if (fd_one == -1)
{
printf("Error opening first_file\n");
close(fd_one);
return;
}
fd_two = open("second_file",
O_WRONLY | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
printf("Successful copy");
close(fd_one);
close(fd_two);
}
o/p:
first_file (before):
hello world
bye
second_file (after):
hello world
bye
O/P:
10) Write a Program(WAP) to report behavior of the Linux Kernel
including information on configured memory, amount of free and
used memory (Memory information)
#include<iostream>
using namespace std;
#include<stdlib.h>
#include<stdio.h>
int main()
{
cout<<"\n The Kernel Version is : \n";
system("cat/proc/sys/kernel/osrelease");
cout<<"\n The CPU Space : \n";
system("cat/proc/cpuinfo | awk 'NR==3, NR==4{print}'\n");
cout<<"\n Amount of CPU time since system was last booted is : ";
system("cat/proc/uptime \n");
cout<<"\n The configured memory is :\n ";
system("cat/proc/meminfo | awk 'NR == 1{print $2}'\n");
cout<<"\n Amount of free memory :\n ";
system("cat/proc/meminfo | awk 'NR == 2{print $2}'\n");
cout<<"\n Amount of used memory is :\n ";
system("cat/proc/meminfo | awk '{if (NR==1) a=$2; if(NR==2) b=$2 }END {print a-b}' \n");
cout<<endl;
return 0;
}
op:-
11)Write a program to report behavior of Linux kernel
including kernel version, CPU type and model. (CPU
information)
#include<iostream>
using namespace std
#include<stdlib.h>
#include<stdio.h>
int main ()
{
cout<<"\n The kernel version is, \n";
system("car/proc/sys/kernel/osrelease") ;
cout<<"\n The cpu space: \n";
system("cat /proc/cputnfo | awk 'NR==3, NR==4{print}' \n ");
cout<<"\n Amount of cpu time since system was last booted is: ";
system("cat /proc/uptime \n");
system("cat /proc/meminfo | awk 'NR==1, NR==4{print $2}' \n ");
cout<<"\n Amount of free memory: \n";
system("cat /proc/merminfo |awk 'NR = 2{Print $2}' \n ")
cout<<"\n Amount of used memory is: \n";
system("cat /proc/meninfo | awk '{ if (NR==1) a=$2; if(NR==2) b=$2 }END {print a-b}
' \n");
cout<<endl;
return(0) ;
}
Output:
12) WRITE A PROGRAM (using fork() and/or exec() commands) where parent
and child execute:
a) same program, same code.
O/p:
o/p:
c) before terminating, the parent waits for the child to finish its task
O/P: