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

OS File ST

IT

Uploaded by

jagritisethi07
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)
2 views

OS File ST

IT

Uploaded by

jagritisethi07
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/ 24

Operating System with LINUX–

MCA I

Practical File
GURU NANAK INSTITUTE OF MANAGEMENT
Affiliated to GGSIPU
(Guru Gobind Singh Indraprastha University)

Faculty Name: Swati Tomar


Prof. Shubhra Saggar Roll No: 02729704424

1 |P a g e
INDEX
S.No TOPIC PAGE NO
1) Execute general purpose commands pwd, cat, ls, mkdir, rmdir, cd, date,
time, cal, clear,
3
2) Work with Linux terminals and basic commands : who, whoami, login,
passwd, su, banner, tty, diff, chmod, ln
4
3) Execute process commands - ps, wait, sleep, exit, kill, jobs, fg, bg, rm, mv,
cp, join, split, cat, head, tail, touch
5
4) Write and execute Shell Script to
a) Find out whether-Given file exits, File has read, write and execute
6
permissions?
b) To generate mark sheet of a student. Take 3 subjects, calculate and
display total marks, percentage and Class obtained by the student.
c) To find factorial of given number n.
d) To accept a number b and display first n prime numbers as output.
To generate first n Fibonacci numbers like: 1, 1, 2, 3, 5, 13,…

5) Created n child processes using for loop and print the PID and PPID of each
child processes.
9
6) Write a C program to calculate total waiting and turnaround time of n
processes with FCFS CPU Scheduling algorithm.
10
7) Write a C Program for Priority CPU Scheduling
11
8) Write a C Program for CPU Scheduling - Pre-emptive and Non-Pre-emptive.
13
9) Write a C Program for Priority CPU Scheduling
16
10) Work with Linux memory management commands Top, free,
/proc/meminfo
18
11) Write a C program to simulate the following contiguous memory allocation
Techniques
19
a) Worst fit
b) Best fit
c) First fit.

12) Write and execute Shell Script and C program to


a) Find out whether-Given file exits, File has read, write and execute
24
permissions?

2 |P a g e
1) Command : pwd
Pwd command: - displays the name of current/working directory as below.

2) Command : cal(year)
Syntax : cal 2021
Cal command: - Cal is a handy tool that you can invoke any time to see the calendar of any specific month
or a complete year.

3) Command : man(command name)


Man command :- Man command used to display the user manual of any command that we can run on the
terminal. It provides a detailed view.

4) Command : mkdir
3 |P a g e
Syntax : mkdir hrshu
Mkdir command :- it is used to create new directory.

5) command : ls/dir
Ls/dir command: - You can list the names of the files available in this directory with the ls command.

6) command : cat(file name)


Syntax : cat abc
Cat command :- To check whether the shell has actually done the job use the cat command with the filename
as argument.

7)Command : rmdir (file name)


Syntax : rmdir hrshu
Rm command :- its used to remove/delete the directory or file. It can remove only empty directory.

8) Command : cd(file name)


Syntax : cd hrshu
Cd command: - cd stands for "change directory", changes the shell's current working directory.

4 |P a g e
9) Command : time(command name)
Time command :- The time command is used to determine how long a given command takes to run. It is
useful for testing the performance of your scripts and commands.

10) Command: clear


clear command : clear is a standard Unix computer operating system command that is used to clear the
terminal screen.

5 |P a g e
4) Write and execute Shell Script to
a)Find out whether-Given file exits, File has read, write and execute permissions?

echo -n "Enter file name : "


read file
[ -w $file ] && W="Write = yes" || W="Write = No"

[ -x $file ] && X="Execute = yes" || X="Execute = No"

[ -r $file ] && R="Read = yes" || R="Read = No"

echo "$file permissions"


echo "$W"
echo "$R"
echo "$X"

b) To generate mark sheet of a student. Take 3 subjects, calculate and display total marks, percentage
and Class obtained by the student.

echo "Enter the three subject marks for the student"


read m1 m2 m3
sum1=`expr $m1 + $m2 + $m3`
echo "Sum of 3 subjects are: " $sum1
per=`expr $sum1 / 3`
echo " Percentage: " $per
if [ $per -ge 60 ]
then
echo "You get Distinction”
elif [ $per -ge 50 ]
then
echo “You get First class”
elif [ $per -ge 40 ]
then
echo "You get Second class"
else
echo "You get Fail"
fi

6 |P a g e
c) To find factorial of given number n
echo "Enter a number"
read num

fact=1

for((i=2;i<=num;i++))
{
fact=$((fact * i)) #fact = fact * i
}

echo $fact

7 |P a g e
d) To accept a number b and display first n prime numbers as output.
To generate first n Fibonacci numbers like: 1, 1, 2, 3, 5, 13,…

echo "The Fibonacci series is : "

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


do
echo -n "$a "
fn=$((a + b))
a=$b
b=$fn
done

8 |P a g e
5) Created n child processes using for loop and print the PID and PPID of each child processes.
#include<stdio.h>
int main()
{
for(int i=0;i<5;i++) // loop will run n times (n=5)
{
if(fork() == 0)
{
printf("[son] pid %d from [parent] pid %d\n",getpid(),getppid());
exit(0);
}
}
for(int i=0;i<5;i++) // loop will run n times (n=5)
wait(NULL);
}

9 |P a g e
6) Write a C program to calculate total waiting and turnaround time of n processes with FCFS CPU
Scheduling algorithm
#include<stdio.h>
void findWaitingTime(int processes[], int n,
int bt[], int wt[])
{
wt[0] = 0;
for (int i = 1; i < n ; i++ )
wt[i] = bt[i-1] + wt[i-1] ;
}
void findTurnAroundTime( int processes[], int n,
int bt[], int wt[], int tat[])
{
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
void findavgTime( int processes[], int n, int bt[])
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt);
findTurnAroundTime(processes, n, bt, wt, tat);
printf("Processes Burst time Waiting time Turn around time\n");
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(" %d ",(i+1));
printf(" %d ", bt[i] );
printf(" %d",wt[i] );
printf(" %d\n",tat[i] );
}
int s=(float)total_wt / (float)n;
int t=(float)total_tat / (float)n;
printf("Average waiting time = %d",s);
printf("\n");
printf("Average turn around time = %d ",t);
}
int main()
{
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
int burst_time[] = {10, 5, 8};

findavgTime(processes, n, burst_time);
return 0;

10 | P a g e
7) Write a C Program for Priority CPU Scheduling.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
//clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter process name,arrivaltime,execution time & priority:");
//flushall();
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
11 | P a g e
}
for(i=0; i<n; i++)

if(i==0)
{
st[i]=at[i];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
else
{
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
for(i=0; i<n; i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}

12 | P a g e
8) Write a C Program for CPU Scheduling
a) (Non-Pre-emptive)
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);

printf("nEnter Burst Time:n");


for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}

//sorting of burst times


for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

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];
}

13 | P a g e
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);
}

b) Pre-emptive .
#include <stdio.h>
int main()
{
int arrival_time[10], burst_time[10], temp[10];
int i, smallest, count = 0, time, limit;
double wait_time = 0, turnaround_time = 0, end;
float average_waiting_time, average_turnaround_time;
printf("nEnter the Total Number of Processes:t");
scanf("%d", &limit);
printf("nEnter Details of %d Processesn", limit);
for(i = 0; i < limit; i++)
{
printf("nEnter Arrival Time:t");
scanf("%d", &arrival_time[i]);
printf("Enter Burst Time:t");
14 | P a g e
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
burst_time[9] = 9999;
for(time = 0; count != limit; time++)
{
smallest = 9;
for(i = 0; i < limit; i++)
{
if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] &&
burst_time[i] > 0)
{
smallest = i;
}
}
burst_time[smallest]--;
if(burst_time[smallest] == 0)
{
count++;
end = time + 1;
wait_time = wait_time + end - arrival_time[smallest] - temp[smallest];
turnaround_time = turnaround_time + end - arrival_time[smallest];
}
}
average_waiting_time = wait_time / limit;
average_turnaround_time = turnaround_time / limit;
printf("nnAverage Waiting Time:t%lfn", average_waiting_time);
printf("Average Turnaround Time:t%lfn", average_turnaround_time);
return 0;
}

15 | P a g e
9) Write a C Program for Priority CPU Scheduling.
#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;
printf("Enter Total Number of Process:");
scanf("%d",&n);

printf("\nEnter Burst Time and Priority\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]);
p[i]=i+1; //contains process number
}

//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;
}
wt[0]=0; //waiting time for first process is zero

//calculate waiting time


for(i=1;i<n;i++)
{
wt[i]=0;
16 | P a g e
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}
avg_wt=total/n; //average waiting time
total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n; //average turnaround time
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);

return 0;
}

17 | P a g e
10) Work with Linux memory management commands Top, free,

Top :

Free :

18 | P a g e
11) Write a C program to simulate the following contiguous memory allocation Techniques
a) Worst fit
b) Best fit
c) First fit.
a) 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;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - First Fit");
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)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
19 | P a g e
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
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();
}

INPUT

Enter the number of blocks: 3


Enter the number of files: 2

Enter the size of the blocks:-


Block 1: 5
Block 2: 2
Block 3: 7

Enter the size of the files:-


File 1: 1
File 2: 4

b) Best fit.

#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
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);
20 | P a g e
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)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;

lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;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();
}

21 | P a g e
c) First 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("\n\tMemory Management Scheme - Worst Fit");
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++)
{
22 | P a g e
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:\tFragement");
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();
}

23 | P a g e
12) Write and execute Shell Script and C program to
a) Find out whether-Given file exits, File has read, write and execute permissions?

echo -n "Enter file name : "


read file
[ -w $file ] && W="Write = yes" || W="Write = No"

[ -x $file ] && X="Execute = yes" || X="Execute = No"

[ -r $file ] && R="Read = yes" || R="Read = No"

echo "$file permissions"


echo "$W"
echo "$R"
echo "$X"

24 | P a g e

You might also like