You are on page 1of 17

OPERATING SYSTEM (3140702)

LAB MANUAL

1. Study of Advance commands and filters of Linux/UNIX.

● All the commands category-wise are as follows


1. Directory Related: pwd, cd, mkdir, rmdir
2. File Related: cat, cp, rm, mv, ls, wc, diff, cmp, comm, split
3. Filters:head, tail, sort, grep
4. Package Related: apt-get install, apt-get remove
 DIRECTORY RELATED
I. pwd: Print Working Directory
Syntax: pwd
Usage: print working directory name
II. cd:Change Directory
Syntax: cd [directory]
Usage: change working directory
Examples:
cd Change working directory to home directory
cd.. Change working directory to parent directory
cd /user/lib Change working directory to absolute path/user/lib
cd Change working directory to relative path doc/letters
doc/letters

III. mkdir: Make Directory


Syntax: mkdir directory
Usage: Creates new directory
Examples:
mkdir Demo Creates a new directory named Demo.

IV. rmdir: Remove Directory


Syntax: rmdir directory
Usage: Removes an empty directory. If the directory empty, it will not be removed.
Examples:
Rmdir Demo: Remove directory named Demo if it is empty

 FILE RELATED

I. cat:
Syntax: cat file File displays file contents
cat > file Creates new file.
cat>> file Appends to an existing file.
Usage: Display file contents, Creates new file, or appends data to an existing file.

II. ls:List files


Syntax: ls[directory]
Usage: List out contents of directory
Options:
A List out all files including hidden ones
D List only Directories
F Mark directories with /, executable files with *,symbolic links with @ sockets with=
l Long listing showing protections, number of links, owner, size and time of last
modification
S Size in kilobytes

III. cp:Copy command

Syntax: cp[-irp] file1[file2]target


Usage: copies a file or group of files.
Options:
i Interactive coping(Asks before over writing a destination file.)
r Recursive copying(It copis directory structure)

IV. mv:Move/Rename file

Syntax: mv[-if]file1[file2...]target
Usage: Renames a file, or moves a file or group of files.
Examples:
mv file1 Renames file1,to file2
mv old Moves the old file to the directory “archive”

V. rm:Remove file
Syntax: mv[-f][-i][-r]file
Usage: Removes files(Deletes files)
Options:
i interactive removal
r Recursive removal
f Forceful removal

 FILTERS

I. head:
Syntax: head[-n]file
Usage: Display top of the file.
Options:
n:indicates number of lines to be displayed top of a file
Examples:
head test.txt displays first 10 linesoffilestest.txt
Head -20test.txt displays first 20 lines of filestest.txt

II. tail:
Syntax: tail[-n]file
Usage: Display end of the file.
Options:
n:indicates number of lines to be displayed top of a file
Examples:
tail test.txt displays last 10 linesoffilestest.txt
tail -20test.txt displays last 20 linesoffilestest.txt

 PACKAGE RELATED

I. apt-get install
Syntax: apt-get install package
Usage:
● apt-get is the command-line tool used for install APT(advanced packaging Tool)software
packages.
● It is rapid ,practical and efficient way to unstall packages on your system.
Examples:
Apt-get install libc Install libc6,which libraries of the embedded GNU
C library.
II. apt-get remove
Syntax: apt-get remove package
Usage:
● apt-get is the command-line tool used for remove APT(advanced packaging Tool)software
packages.
Examples:
apt-get remove libc6 remove/uninstall libc6.

2. Study of Advance commands and filter of Linux/UNIX.

These advanced Unix commands will allow you to accomplish various tasks in Unix and Unix
like operating systems, generally giving you more options for managing your data and getting things
done.
It is best if you get familiar with the basic Unix commands first.Since this is an index of
commands, do take a minute to explore each of the pages for all the listed commands, they contain
examples of advanced usage of seemingly simple commands. There's also a very useful Unix Reference
section that explores some of the most useful topics at greater length and depth.

Unix file operations

● basename – get filename from the full path (remove directory names)
● ln – make links and symlinks to files and directories
● find – finding files and directories in Unix

Unix system status commands

● dmesg – show latest kernel buffer messages


● last – show history of user logins and reboot/shutdown commands
● w – show who is logged on and what they are doing
● who -r – confirm current run-level of your Unix/Linux OS
● uname – print Unix system information: hostname, kernel version, etc
● lsb_release – find Linux Standard Base (LSB) information
Privileged Access

● su – switch user (commonly used to become root)


● sudo – run commands with elevated (usually root-like) privileges
● be sure to check out sudo reference
● visudo – edit the /etc/sudoers file safely

Advanced process management in Unix

● ps -aef – show full listing of processes on your system


● ptree – show process tree in Solaris (can be done with ps in Linux)
● kill – kill a process (or send a specific signal to it)
● nice – start a process with certain priority (CPU resourcing)
● renice – update a CPU priority of an existing process
● pmap – print process map – detailed low-level memory usage
● pfiles – shows list of files/network ports open by process in Solaris

Text Manipulation commands

● awk – text processing and data extraction in text files


● grep – find and extract lines from text
● egrep – extended grep – advanced pattern matching in text files
● sed – stream editor (search/replace values in a string)
● tr – translate characters in a string using provided rules

Unix filesystems commands

● fstyp – confirm a type of filesystem on the specified device


● df – shows filesystem usage for active (mounted) filesystems
● du – shows disk usage info for files and directories

Working with disks and filesystems

● mount – attach filesystem into the file tree


● umount – detach filesystem from the file tree
● dd – copy or securely erase disks and disk partitions
● fsck – check filesystem integrity
● growfs – grow filesystem
● tune2fs – adjust tunable filesystem parameters (for ext2/ext3)
● mkfs – make new filesystem

Networking

● iptables – manage firewall rules on a Linux server


● netstat – network statistics and network routing information
● traceroute – tracing ICMP routes to a remote host
echo "Enter marks of 3 subjects out of 70"
read math
read sci
read eng

per=100
subjectTotal=210

sum=`expr $math + $sci + $eng`


percentages=`expr $sum \* $per / $subjectTotal`

echo $percentages

if [ $percentages -gt 80 ]
then
echo "Class A"
else if [ $percentages -gt 70 ]
then
echo "Class B"
else if [ $percentages -gt 60 ]
then
echo "Class C"

3.write a shell script to display multiplication table of given number.


echo "Enter a Number"
read n
i=0
while [ $i -le 10 ]
do
echo " $n x $i = `expr $n \* $i`"
i=`expr $i + 1`
done

4. Write a shell script to find factorial of given number n.


echo "Enter a number"
read num

fact=1

while [ $num -gt 1 ]


do
fact=$((fact * num)) #fact = fact * num
num=$((num - 1)) #num = num - 1
done

echo $fact
5.write a shell script which will accept a number b and display first n prime
numbers as output.
prime_1=0
echo "enter the range"
read n
echo " Prime number between 1 to $n is:"
echo "1"
echo "2"
for((i=3;i<=n;))
do
for((j=i-1;j>=2;))
do
if [ `expr $i % $j` -ne 0 ] ; then
prime_1=1
else
prime_1=0
break
fi
j=`expr $j - 1`
done
if [ $prime_1 -eq 1 ] ; then
echo $i
fi
i=`expr $i + 1`
done

6. Write a shell script which accepts number b and generates the first N prime
number.

echo "Enter the number upto which you want prime numbers:"
read n

for ((i=1;i<=n;++))
do
flag=0
for ((j=2;j<i;j++))
do

if [ 'expr $i % $j' -eq 0]


then
flag=1
fi
done

if [$flag-eq 0]
then
echo $i
fi

done
echo "Enter the number upto which you want prime numbers:"
read n

for ((i=1;i<=n;++))
do
flag=0
for ((j=2;j<i;j++))
do

if [ 'expr $i % $j' -eq 0]


then
flag=1
fi
done

if [$flag-eq 0]
then
echo $i
fi
done
7. Write a shell script which will generate first n Fibonacci numbers like:
1,1,2,3,5,13

1. Step 1: Start
2. Step 2: Declare variable
3. Step 3: Initialize variable
4. Step 4: Read n from user
5. Step 5: Print x and y
6. Step 6: Repeat until i&lt;n
7. 6.1 x=x+y
8. 6.2 print z
9. 6.3 x=y, y=z
10. 6.4 i=i+1
11. Stop 7: Stop

echo "How many number of terms to be generated ?"


read n
x=0
y=1
i=2
echo "Fibonacci Series up to $n terms :"
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1 `
z=`expr $x + $y `
echo "$z"
x=$y
y=$z
Done

10. write a shell script to display all executable files, directories and zero sized
files from current directory.
clear
echo "Enter Directory Name:"
read dir
ls $dir>tempfile.txt
count=0
if [ -d $dir ];then
for filename in `cat tempfile.txt`
do
if [ -x $filename ];then
echo "$filename"
count=`expr $count + 1`
fi
done
fi
echo "Total Executable Files Are $count"

rm tempfile.txt

11.write a shell script to check entered string is palindrome or not.

Algorithm to check whether a number is a palindrome or not


● Input the number.
● Find the reverse of the number.
● If the reverse of the number is equal to the number, then return true. Else, return false.
PROGRAM
echo "Enter the number"
read n
number=$n
reverse=0
while [ $n -gt 0 ]
do
a=`expr $n % 10 `
n=`expr $n / 10 `
reverse=`expr $reverse \* 10 + $a`
done
echo $reverse
if [ $number -eq $reverse ]
then
echo "Number is palindrome"
else
echo "Number is not palindrome"
fi
12. Study of Unix Shell and Environment Variable.
A variable is a character string to which we assign a value. The value assigned could be a
number, text, filename, device, or any other type of data.
For example, first we set a variable TEST and then we access its value using the echo command −
$TEST="Unix Programming"
$echo $TEST
It produces the following result.
Unix Programming

Note that the environment variables are set without using the $ sign but while accessing them we use the
$ sign as prefix. These variables retain their values until we come out of the shell.
When you log in to the system, the shell undergoes a phase called initialization to set up the
environment. This is usually a two-step process that involves the shell reading the following files −
● /etc/profile
● profile

The process is as follows −


● The shell checks to see whether the file /etc/profile exists.
● If it exists, the shell reads it. Otherwise, this file is skipped. No error message is displayed.
● The shell checks to see whether the file .profile exists in your home directory. Your home
directory is the directory that you start out in after you log in.
● If it exists, the shell reads it; otherwise, the shell skips it. No error message is displayed.

As soon as both of these files have been read, the shell displays a prompt −
$

Setting the Terminal Type


Usually, the type of terminal you are using is automatically configured by either the login or getty
programs. Sometimes, the auto configuration process guesses your terminal incorrectly.
If your terminal is set incorrectly, the output of the commands might look strange, or you might not be
able to interact with the shell properly.
To make sure that this is not the case, most users set their terminal to the lowest common denominator in
the following way −
$TERM=vt100
$

Environment Variables
Following is the partial list of important environment variables. These variables are set and accessed as
mentioned below −

Sr.No. Variable & Description


1
DISPLAY

Contains the identifier for the display that X11 programs should use by default.

2
HOME

Indicates the home directory of the current user: the default argument for the cd built-in
command.

3
IFS

Indicates the Internal Field Separator that is used by the parser for word splitting after
expansion.

4
LANG

LANG expands to the default system locale; LC_ALL can be used to override this. For
example, if its value is pt_BR, then the language is set to (Brazilian) Portuguese and the
locale to Brazil.

5
LD_LIBRARY_PATH

A Unix system with a dynamic linker, contains a colonseparated list of directories that the
dynamic linker should search for shared objects when building a process image after exec,
before searching in any other directories.

6
PATH

Indicates the search path for commands. It is a colon-separated list of directories in which
the shell looks for commands.
7
PWD

Indicates the current working directory as set by the cd command.

8
RANDOM

Generates a random integer between 0 and 32,767 each time it is referenced.

9
SHLVL

Increments by one each time an instance of bash is started. This variable is useful for
determining whether the built-in exit command ends the current session.

10
TERM

Refers to the display type.

11
TZ

Refers to Time zone. It can take values like GMT, AST, etc.

12
UID

Expands to the numeric user ID of the current user, initialized at the shell startup.

Following is the sample example showing few environment variables −


$ echo $HOME
/root
]$ echo $DISPLAY
$ echo $TERM
xterm
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/home/amrood/bin:/usr/local/bin
$

13. Write an awk program using function, which convert each word in a given
text into capital.

// starting
to_upper_case() // create user define function
{
a="the world is so beautiful"
echo "$a" | awk '{print toupper($0)}'
}

to_upper_case // call the function

15.Write a c program to implement FIFO page replacement algorithm.

#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);

printf("\nEnter Process Burst Time\n");


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

wt[0]=0; //waiting time for first process is 0

//calculating waiting time


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

printf("\nProcess\t\tBurst Time\tWaiting Time\tTotal Time");

//calculating turnaround time


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}

avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);

return 0;

16.Case Study of Windows operating System.


Operating system (OS), program that manages a computer’s resources, especially the allocation
of those resources among other programs. Typical resources include the central processing unit (CPU),
computer memory, file storage, input/output devices, and network connections. Management tasks
include scheduling resource use to avoid conflicts and interference between programs. Unlike most
programs, which complete a task and terminate, an operating system runs indefinitely and terminates only
when the computer is turned off.

Modern multiprocessing operating systems allow many processes to be active, where each
process is a “thread” of computation being used to execute a program. One form of multiprocessing is
called time-sharing, which lets many users share computer access by rapidly switching between them.
Time-sharing must guard against interference between users’ programs, and most systems use virtual
memory, in which the memory, or “address space,” used by a program may reside in secondary memory
(such as on a magnetic hard disk drive) when not in immediate use, to be swapped back to occupy the
faster main computer memory on demand. This virtual memory both increases the address space available
to a program and helps to prevent programs from interfering with each other, but it requires careful
control by the operating system and a set of allocation tables to keep track of memory use. Perhaps the
most delicate and critical task for a modern operating system is allocation of the CPU; each process is
allowed to use the CPU for a limited time, which may be a fraction of a second, and then must give up
control and become suspended until its next turn. Switching between processes must itself use the CPU
while protecting all data of the processes.

The first digital computer had no operating systems. They ran one program at a time, which had
command of all system resources, and a human operator would provide any special resources needed. The
first operating systems were developed in the mid-1950s. These were small “supervisor programs” that
provided basic I/O operations (such as controlling punch card readers and printers) and kept accounts of
CPU usage for billing. Supervisor programs also provided multiprogramming capabilities to enable
several programs to run at once. This was particularly important so that these early multimillion-dollar
machines would not be idle during slow I/O operations.

Computers acquired more powerful operating systems in the 1960s with the emergence of time-
sharing, which required a system to manage multiple users sharing CPU time and terminals. Two early
time-sharing systems were CTSS (Compatible Time Sharing System), developed at the Massaachusetts
Institute of Technology, and the Dartmouth College Basic System, developed at Dartmouth College.
Other multiprogrammed systems included Atlas, at the University of Manchester, England, and IBM’s
OS/360, probably the most complex software package of the 1960s. After 1972 the Multics system for
General Electric Co.’s GE 645 computer (and later for Honeywell Inc.’s computers) became the most
sophisticated system, with most of the multiprogramming and time-sharing capabilities that later became
standard.

The minicomputer of the 1970s had limited memory and required smaller operating systems. The
most important operating system of that period was UNIX, developed by AT&T for large minicomputers
as a simpler alternativs to Multics. It became widely used in the 1980s, in part because it was free to
universities and in part because it was designed with a set of tools that were powerful in the hands of
skilled programmers. More recently, Linux, an open-source version of UNIX developed in part by a
group led by Finnish computer science student Linus Torvalds and in part by a group led by American
computer programmer Richard Stallman, has become popular on personal computers as well as on larger
computers.

17.Case Study of Linux/Unix operating System.


Linux was first developed by Linux Torvalds, a student in Finland,in 1991.But,now Linux is not
owned by anyone.No one company or individual “owns” Linux, like as Windows are owned by a Single
company Microsoft.

Linux is most famous free and open source Operating System.Open source operating Systems are
available in source code format rather than as compiled binary code. Free Software means not by money
point of view.But software that is free,like linux,is distributed along with its source code.So that anyone
who receives it is free to make changes and redistribute it.You can make change to the source code of
Linux as per your requirements. Not only this,but you can also distribute this modified OS.You need to
concern that you have to provide your modified source code with this os.
Many people done such type of work,and as a result there are many flavours of Linux available
in the market.Some of the known ones are, Red Hat Enterprise Linux and its derivative such as Fedora
and CentOS,Debian and its derivatives such as Ubuntu and Linux Mint,Linspire,PCLinux Day by
day,Linux is becoming famous.It uses most of the features of UNIX ,it is multi-user,multitasking,
Operating System that also supports a handy Graphical User Interface(GUI).It is quite robust and
secure.You will rarely hear about virus attack on Linux System,as with windows.Linux can be found on a
wide range of devices from personal computer,mobiles,tablets and embedded systems to mainframe and
supercomputers.

You might also like