Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

OS Lab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

Operating System

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.

IU2041050070 NISH MEHTA


Operating System

$ 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

file. Try ‘$ rm filename.txt’ at your terminal


$ rm file.txt
11. chmod : The ‘$ chmod’ command stands for change mode command.As there
are many modes in Unix that can be used to manipulate files in the Unix
environment.Basically there are 3 modes that we can use with the ‘chmod’
command
1. +w (stands for write and it changes file permissions to write)
2. +r (stands for read and it changes file permissions to read)
3. +x (generally it is used to make a file executable)
$ chmod +w file.txt
$ chmod +r file.txt
$ chmod +x file.txt
12. cal : The ‘$ cal’ means calendar and it simply display calendar on to your screen.
$ cal
Output : July 2017
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29

IU2041050070 NISH MEHTA


Operating System

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

type with more accuracy


$ clear
20. history : The ‘$ history’ command is used to get list of previous commands may
be obtained by executing the following command. you can also use parameters like !
n to re-execute the nth command, !! to executes the most recent command, and !
cp this will execute the most recent command that starts with cp.
$ history

IU2041050070 NISH MEHTA


Operating System

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

# Function to perform addition

addition() {

result=$(echo "$1 + $2" | bc)

echo "Addition Result: $result"

# Function to perform subtraction

subtraction() {

IU2041050070 NISH MEHTA


Operating System

result=$(echo "$1 - $2" | bc)

echo "Subtraction Result: $result"

# Function to perform multiplication

multiplication() {

result=$(echo "$1 * $2" | bc)

echo "Multiplication Result: $result"

# Function to perform division

division() {

if [ $2 -eq 0 ]; then

echo "Cannot divide by zero!"

else

result=$(echo "scale=2; $1 / $2" | bc)

echo "Division Result: $result"

fi

# Main script

# Input two numbers from the user

read -p "Enter the first number: " num1

read -p "Enter the second number: " num2

# Call functions with user input

addition $num1 $num2

subtraction $num1 $num2

multiplication $num1 $num2

division $num1 $num2

OUTPUT:

IU2041050070 NISH MEHTA


Operating System

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

# Function to convert distance

convert_distance() {

# Convert to meters

meters=$(echo "$1 * 1000" | bc)

# Convert to feet

feet=$(echo "$1 * 3280.84" | bc)

IU2041050070 NISH MEHTA


Operating System

# Convert to inches

inches=$(echo "$1 * 39370.1" | bc)

# Convert to centimeters

centimeters=$(echo "$1 * 100000" | bc)

# Print the results

echo "Distance in Meters: $meters m"

echo "Distance in Feet: $feet ft"

echo "Distance in Inches: $inches in"

echo "Distance in Centimeters: $centimeters cm"

# Main script

# Input distance from the user

read -p "Enter the distance between two cities (in km): " distance_in_km

# Call the function with user input

convert_distance $distance_in_km

OUTPUT:

IU2041050070 NISH MEHTA


Operating System

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

echo "Even Numbers - "


i=1

# -le means less than or equal to


while [ $i -le $n ]
do
# arithmetic operations is performed with 'expr'
rs=`expr $i % 2`

if [ $rs == 0 ]

IU2041050070 NISH MEHTA


Operating System

then
echo " $i"

# end of if loop
fi

# incrementing i by one
((++i))

# end of while loop


done

# Now printing odd numbers


echo "Odd Numbers - "
i=1

while [ $i -le $n ]
do

rs=`expr $i % 2`

if [ $rs != 0 ]
then

echo " $i"

fi

((++i))
done

OUTPUT:

IU2041050070 NISH MEHTA


Operating System

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

IU2041050070 NISH MEHTA


Operating System

echo "LEAP YEAR SHELL SCRIPT"


echo -n "Enter a year:"
read year_checker
if [ `expr $year_checker % 4` -eq 0 ]
then
echo "$year_checker is a leap year"
else
echo "$year_checker is not a leap year"
fi

OUTPUT:

Practical-6
AIM: Write a shell script to find the factorial of any no entered through keyboard.
PROGRAM:
# !/bin/bash

IU2041050070 NISH MEHTA


Operating System

echo "Enter a number"

read num

fact=1

n=$num;

while [ $num -ge 1 ]

do

fact=`expr $fact \* $num`

num=`expr $num - 1`

done

echo "Factorial of $n is $fact"

OUTPUT:

Practical-7
AIM: Write a shell script which will accept a number and display first n prime numbers as
output.
PROGRAM:

IU2041050070 NISH MEHTA


Operating System

read -p "Enter the NUmber: " n

echo "The prime numbers $n are: "

m=2

while [ $m -le $n ]

do

i=2

flag=0

while [ $i -le `expr $m / 2` ]

do

if [ `expr $m % $i` -eq 0 ]

then

flag=1

break

fi

i=`expr $i + 1`

done

if [ $flag -eq 0 ]

then

echo $m

fi

m=`expr $m + 1`

done

OUTPUT:

IU2041050070 NISH MEHTA


Operating System

IU2041050070 NISH MEHTA


Operating System

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 "Fibonacci Series Upto $number Number: "

echo "$x"

echo "$y"

while [ $i -lt $number ]

do

i=`expr $i + 1`

z=`expr $x + $y`

echo "$z"

x=$y

y=$z

done

OUTPUT:

IU2041050070 NISH MEHTA


Operating System

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

for((i=0; i<$n; i++))

do

read -p "Enter value of arr[$i]: " arr[$i]

done

#sorting code

for((i=0; i<$n; i++))

do

for((j=0; j<n-i-1; j++))

do

if [ ${arr[j]} -lt ${arr[$((j+1))]} ]

then

#swapping

temp=${arr[j]}

arr[$j]=${arr[$((j+1))]}

arr[$((j+1))]=$temp

fi

done

echo "Numbers in Descending order: " ${arr[*]}

OUTPUT:

IU2041050070 NISH MEHTA


Operating System

Practical-10
AIM: Write a shell script to display all executable files, directories and zero sized files.
PROGRAM:
echo Executable files

files=$(find lab_solutions -executable -type f)

echo $files

echo

echo List of Directories

dir=$(ls -d )

echo $dir

echo

echo List of zero sized files

zero=$(find -size 0)

echo $zero

OUTPUT:

IU2041050070 NISH MEHTA


Operating System

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>

using namespace std;

// Function to find the waiting time for all

// processes

void findWaitingTime(int processes[], int n,

int bt[], int wt[])

// waiting time for first process is 0

wt[0] = 0;

// calculating waiting time

for (int i = 1; i < n ; i++ )

wt[i] = bt[i-1] + wt[i-1] ;

// Function to calculate turn around time

void findTurnAroundTime( int processes[], int n,

int bt[], int wt[], int tat[])

// calculating turnaround time by adding

// bt[i] + wt[i]

for (int i = 0; i < n ; i++)

tat[i] = bt[i] + wt[i];

IU2041050070 NISH MEHTA


Operating System

//Function to calculate average time

void findavgTime( int processes[], int n, int bt[])

int wt[n], tat[n], total_wt = 0, total_tat = 0;

//Function to find waiting time of all processes

findWaitingTime(processes, n, bt, wt);

//Function to find turn around time for all processes

findTurnAroundTime(processes, n, bt, wt, tat);

//Display processes along with all details

cout << "Processes "<< " Burst time "

<< " Waiting time " << " Turn around time\n";

// Calculate total waiting time and total turn

// around time

for (int i=0; i<n; i++)

total_wt = total_wt + wt[i];

total_tat = total_tat + tat[i];

cout << " " << i+1 << "\t\t" << bt[i] <<"\t "

<< wt[i] <<"\t\t " << tat[i] <<endl;

cout << "Average waiting time = "

<< (float)total_wt / (float)n;

cout << "\nAverage turn around time = "

<< (float)total_tat / (float)n;

IU2041050070 NISH MEHTA


Operating System

// Driver code

int main()

//process id's

int processes[] = { 1, 2, 3};

int n = sizeof processes / sizeof processes[0];

//Burst time of all processes

int burst_time[] = {10, 5, 8};

findavgTime(processes, n, burst_time);

return 0;

OUTPUT:

IU2041050070 NISH MEHTA


Operating System

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>

using namespace std;

// Function to find page faults using indexes

int pageFaults(int pages[], int n, int capacity)

// To represent set of current pages. We use

// an unordered_set so that we quickly check

// if a page is present in set or not

unordered_set<int> s;

// To store least recently used indexes

// of pages.

unordered_map<int, int> indexes;

// Start from initial page

int page_faults = 0;

for (int i=0; i<n; i++)

// Check if the set can hold more pages

if (s.size() < capacity)

// Insert it into set if not present

// already which represents page fault

if (s.find(pages[i])==s.end())

IU2041050070 NISH MEHTA


Operating System

s.insert(pages[i]);

// increment page fault

page_faults++;

// Store the recently used index of

// each page

indexes[pages[i]] = i;

// If the set is full then need to perform lru

// i.e. remove the least recently used page

// and insert the current page

else

// Check if current page is not already

// present in the set

if (s.find(pages[i]) == s.end())

// Find the least recently used pages

// that is present in the set

int lru = INT_MAX, val;

for (auto it=s.begin(); it!=s.end(); it++)

if (indexes[*it] < lru)

lru = indexes[*it];

val = *it;

IU2041050070 NISH MEHTA


Operating System

// Remove the indexes page

s.erase(val);

// insert the current page

s.insert(pages[i]);

// Increment page faults

page_faults++;

// Update the current page index

indexes[pages[i]] = i;

return page_faults;

// Driver code

int main()

int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};

int n = sizeof(pages)/sizeof(pages[0]);

int capacity = 4;

cout << pageFaults(pages, n, capacity);

return 0;

OUTPUT:

IU2041050070 NISH MEHTA

You might also like