OS Lab
OS Lab
OS Lab
Practical-1
AIM.1: Study of Unix Commands.
PROGRAM:
Basic unix commands:
1. who : The ‘$ who’ command displays all the users who have logged into the
system currently. As shown above, on my system I am the only user currently
logged in.The thing tty2 is terminal line the user is using and the next line gives the
current date and time
$ who
Output: harssh tty2 2017-07-18 09:32 (:0)
2. pwd : The ‘$pwd’ command stands for ‘print working directory’ and as the name
says,it displays the directory in which we are currently (directory is same as folder
for Windows OS users).
In the output, we are harssh directory(folder for Windows OS that are moving to
Linux),which is present inside the home directory.
$ pwd
Output: /home/harssh
3. mkdir : The ‘$ mkdir’ stands for ‘make directory’ and it creates a new
directory.We have used ‘$ cd’ (which is discussed below) to get into the newly
created directory and again on giving ‘$ pwd’ command,we are displayed with the
new ‘newfolder’ directory.
$ mkdir newfolder
$ cd newfolder
$ pwd
Output: /home/harssh/newfolder
4. rmdir : The ‘$ rmdir’ command deletes any directory we want to delete and you
can remember it by its names ‘rmdir’ which stands for ‘remove directory’.
$ rmdir newfolder
5. cd : The ‘$ cd’ command stands for ‘change directory’ and it changes your
current directory to the ‘newfolder’ directory.You can understand this a double-
clicking a folder and then you do some stuff in that folder.
$ cd newfolder (assuming that there is a directory named
'newfolder' on your system)
6. ls : The ‘ls’ command simply displays the contents of a directory.
$ ls
Output: Desktop Documents Downloads Music Pictures Public Scratch
Templates Videos
7. touch : The ‘$ touch’ command creates a file(not directory) and you can simple
add an extension such as .txt after it to make it a Text File.
$ touch example
$ ls
Output: Desktop Documents Downloads Music Pictures Public Scratch
Templates Videos example
Note: It is important to note that according to the Unix File structure, Unix treats
all the stuff it has as a ‘file’, even the directories(folders) are also treated as a
file.You will get to know more about this as you will further use Linux/Unix based
OS
8. cp : This ‘$ cp ‘ command stands for ‘copy’ and it simply copy/paste the file
wherever you want to.In the above example, we are copying a file ‘file.txt’ from the
directory harssh to a new directory new.
$ cp /home/harssh/file.txt /home/harssh/new/
9. mv : The ‘$ mv’ command stands for ‘move’ and it simply move a file from a
directory to another directory.In the above example a file named ‘file.txt’ is being
moved into a new directory ‘new’
$ mv /home/harssh/file.txt /home/harssh/new
10. rm : The ‘$ rm ‘ command for remove and the ‘-r’ simply recursively deletes
30 31
13. file : The ‘$ file’ command displays the type of file.As I mentioned earlier Linux
treats everything as a file so on executing the command file on a
directory(Downloads) it displays directory as the output
$ ls
Output: Desktop Documents Downloads Music Pictures Public Scratch
Templates Videos
$ file Downloads
Output: Downloads: directory
14. sort : As the name suggests the ‘$ sort’ sorts the contents of the file according to
the ASCII rules.
$ sort file
15. grep : grep is an acronym for ‘globally search a regular expression and print
it’.The ‘$ grep’ command searches the specified input fully(globally) for a match
with the supplied pattern and displays it.
In the example, this would search for the word ‘picture’ in the file newsfile and if
found,the lines containing it would be displayed on the screen.
$ grep picture newsfile
16. man : The ‘$ man’ command stands for ‘manual’ and it can display the in-built
manual for most of the commands that we ever need.In the above example, we can
read about the ‘$ pwd’ command.
$ man pwd
17. lpr : The ‘$ lpr’ command send a file to the printer for printing.
$ lpr new.txt
18. passwd : The ‘$ passwd’ command simply changes the password of the user.In
above case ‘harssh’ is the user.
$ passwd
Output: Changing password for harssh.
(current) UNIX password:
19. clear : The ‘$ clear’ command is used to clean up the terminal so that you can
At last, I want to say that these are the most basic and essential commands that are
used in the Linux operating system. You will need them even if you get to advance
the Unix. If you want to master them just keep on practicing them.
Practical-2
AIM: Write a shell script to input two no’s from the user and perform addition, subtraction,
multiplication, and division.
PROGRAM:
#!/bin/bash
addition() {
subtraction() {
multiplication() {
division() {
if [ $2 -eq 0 ]; then
else
fi
# Main script
OUTPUT:
Practical-3
AIM.: The distance between two cities (in km.) is input through the keyboard. Write a shell
script to convert and print distance in meters, feet, inches and centimeters.
PROGRAM:
#!/bin/bash
convert_distance() {
# Convert to meters
# Convert to feet
# Convert to inches
# Convert to centimeters
# Main script
read -p "Enter the distance between two cities (in km): " distance_in_km
convert_distance $distance_in_km
OUTPUT:
Practical-4
AIM.: Any integer is input through the keyboard. Write a shell script to find out whether it is
an odd number or even number.
PROGRAM:
# Take user input
echo "Enter a number"
read n
if [ $rs == 0 ]
then
echo " $i"
# end of if loop
fi
# incrementing i by one
((++i))
while [ $i -le $n ]
do
rs=`expr $i % 2`
if [ $rs != 0 ]
then
fi
((++i))
done
OUTPUT:
Practical-5
AIM: Write a shell script which receives any year form the keyboard and determines whether
the year is a leap year or not. If no argument is supplied the current year should be assumed.
PROGRAM:
clear
OUTPUT:
Practical-6
AIM: Write a shell script to find the factorial of any no entered through keyboard.
PROGRAM:
# !/bin/bash
read num
fact=1
n=$num;
do
num=`expr $num - 1`
done
OUTPUT:
Practical-7
AIM: Write a shell script which will accept a number and display first n prime numbers as
output.
PROGRAM:
m=2
while [ $m -le $n ]
do
i=2
flag=0
do
then
flag=1
break
fi
i=`expr $i + 1`
done
if [ $flag -eq 0 ]
then
echo $m
fi
m=`expr $m + 1`
done
OUTPUT:
Practical-8
AIM: . Write a shell script which will generate first n Fibonacci numbers like: 1, 1, 2, 3, 5,
13,...
PROGRAM:
read -p "Enter the Number: " number
x=0
y=1
i=2
echo "$x"
echo "$y"
do
i=`expr $i + 1`
z=`expr $x + $y`
echo "$z"
x=$y
y=$z
done
OUTPUT:
Practical-9
AIM: Write a shell script to read n numbers as command arguments and sort them in
descending order.
PROGRAM:
read -p "Enter The Number: " n
do
done
#sorting code
do
do
then
#swapping
temp=${arr[j]}
arr[$j]=${arr[$((j+1))]}
arr[$((j+1))]=$temp
fi
done
OUTPUT:
Practical-10
AIM: Write a shell script to display all executable files, directories and zero sized files.
PROGRAM:
echo Executable files
echo $files
echo
dir=$(ls -d )
echo $dir
echo
zero=$(find -size 0)
echo $zero
OUTPUT:
Practical-11
AIM: 11. Write a Program to Implement following scheduling algorithms.
(11.1)First Come First Serve (FCFS)
(11.2) Shortest Job First (SJF)
(11.3) Round-Robin
PROGRAM:
// C++ program for implementation of FCFS
// scheduling
#include<iostream>
// processes
wt[0] = 0;
// bt[i] + wt[i]
<< " Waiting time " << " Turn around time\n";
// around time
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
// Driver code
int main()
//process id's
findavgTime(processes, n, burst_time);
return 0;
OUTPUT:
Practical-12
AIM: Write a Program to Implement following Page Replacement Algorithms.
(12.1) First in First out (FIFO)
(12.2) Least Recently Used (LRU)
from collections import OrderedDict
PROGRAM:
//C++ implementation of above algorithm
#include<bits/stdc++.h>
unordered_set<int> s;
// of pages.
int page_faults = 0;
if (s.find(pages[i])==s.end())
s.insert(pages[i]);
page_faults++;
// each page
indexes[pages[i]] = i;
else
if (s.find(pages[i]) == s.end())
lru = indexes[*it];
val = *it;
s.erase(val);
s.insert(pages[i]);
page_faults++;
indexes[pages[i]] = i;
return page_faults;
// Driver code
int main()
int n = sizeof(pages)/sizeof(pages[0]);
int capacity = 4;
return 0;
OUTPUT: