SCSX4301-OPERATING SYSTEM-Labmanual
SCSX4301-OPERATING SYSTEM-Labmanual
LAB MANUAL
File Handling
• Text Processing
• System Administration
• Process Management
• Archival
• Network
• File Systems
• Advanced Commands
eg. ls, ls l,
ls prabhat
• cd – changes directories
Usage: cd [DIRECTORY]
eg. cd prabhat
cp sample_copy.txt target_dir
mv old.txt new.txt
rm remove
files or directories
Usage: history
Pattern
A Pattern is an expression that describes a set of strings which is used to give a concise description
eg. ab*cd matches anything that starts with ab and ends with cd etc.
Text Processing
cat n file1.txt
echo $HOME
• wc print
eg. wc file1.txt
wc L file1.txt
sort r file1.txt
System Administration
su – change user ID
eg. su remo, su
Process management
Usage: ps [OPTION]
eg. ps, ps el
eg. kill 9
archival
eg. du
eg. df
• quota – display disk usage and limits
eg. quota v
Advanced Commands
eg. reboot
eg. Poweroff
Variables in Shell
In Linux (Shell), there are two types of variable:
(1) System variables - Created and maintained by Linux itself. This type of variable defined in
CAPITAL LETTERS.
(2) User defined variables (UDV) - Created and maintained by user. This type of variable
defined in lower letters.
USER-DEFIEND VARIABLES
Syntax:
variable name=value
'value' is assigned to given 'variable name' and Value must be on right side = sign.
Example:
To define variable called 'vech' having value Bus
$ vech=Bus
To define variable called n having value 10
$ n=10
(1) Variable name must begin with Alphanumeric character or underscore character (_), followed
by one or more Alphanumeric character. For e.g. Valid shell variable are as follows
HOME
SYSTEM_VERSION
vech
no
(2) Don't put spaces on either side of the equal sign when assigning value to variable. For e.g. In
following variable declaration there will be no error
$ no=10
But there will be problem for any of the following variable declaration:
$ no =10
$ no= 10
$ no = 10
(3) Variables are case-sensitive, just like filename in Linux. For e.g.
$ no=10
$ No=11
$ NO=20
$ nO=2
Above all are different variable name, so to print value 20 we have to use $ echo $NO and not
any of the following
$ echo $no # will print 10 but not 20
$ echo $No# will print 11 but not 20
$ echo $nO# will print 2 but not 20
(4) You can define NULL variable as follows (NULL variable is variable which has no value at
the time of definition) For e.g.
$ vech=
$ vech=""
Try to print it's value by issuing following command
$ echo $vech
Nothing will be shown because variable has no value i.e. NULL variable.
echo Command
Shell Arithmetic
Use to perform arithmetic operations.
Syntax:
expr op1 math-operator op2
Examples:
$ expr 1 + 3
$ expr 2 - 1
$ expr 10 / 2
$ expr 20 % 3
$ expr 10 \* 3
$ echo `expr 6 + 3`
Double "Double Quotes" - Anything enclose in double quotes removed meaning of that
"
Quotes characters (except \ and $).
' Single quotes 'Single quotes' - Enclosed in single quotes remains unchanged.
` Back quote
`Back quote` - To execute command
Example:
$ echo "Today is date"
Can't print message with today's date.
$ echo "Today is `date`".
It will print today's date as, Today is Tue Jan ....,Can you see that the `date` statement uses back quote?
Use to get input (data from user) from keyboard and store (data) to variable.
Syntax:
read variable1, variable2,...variableN
$ vi sayH
#
#Script to read your name from key-board
#
echo "Your first name please:"
read fname
echo "Hello $fname, Lets be friend!"
Run it as follows:
$ Your first name please: vivek
Hello vivek, Lets be friend!
Syntax:
command1;command2
To run two command with one command line.
Examples:
$ date;who
Will print today's date followed by users who are currently login. Note that You can't use
$ date who
for same purpose, you must put semicolon in between date and who command.
CONTROL STATEMENTS
Decision making
Loops
Is there any difference making decision in Real life and with Computers? Well real life decision are quit
complicated to all of us and computers even don't have that much power to understand our real life
decisions. What computer know is 0 (zero) and 1 that is Yes or No. To make this idea clear, lets play
some game (WOW!) with bc - Linux calculator program.
$ bc
See what happened if you type 5 > 2 as follows
5>2
1
1 (One?) is response of bc, How? bc compare 5 with 2 as, Is 5 is greater then 2, (If I ask same question to
you, your answer will be YES), bc gives this 'YES' answer by showing 1 value. Now try
5<2
0
Try following in bc to clear your Idea and not down bc's response
5 > 12
5 == 10
5 != 2
5 == 5
12 < 2
It means when ever there is any type of comparison in Linux Shell It gives only two answer one is YES
and NO is other.
if condition
if condition which is used for decision making in shell script, If given condition is true then
command1 is executed.
Syntax:
if condition
then
command1 if condition is true or if exit status
of condition is 0 (zero)
...
...
fi
EXAMPLE
if [ -e . ]
then
echo "Yes."
else
echo "No."
fi
test command or [ expr ] is used to see if an expression is true, and if it is true it return zero(0),
otherwise returns nonzero for false.
Syntax:
test expression OR [ expression ]
Example:
if test $1 -gt 0
then
echo "$1 number is positive"
fi
For Mathematics, use following operator in Shell Script
For [ expr ]
For test statement
statement with if
with if command
command
is less than or
-le 5 <= 6 if test 5 -le 6 if [ 5 -le 6 ]
equal to
is greater than
-ge 5 >= 6 if test 5 -ge 6 if [ 5 -ge 6 ]
or equal to
if...else...fi
if condition
then
condition is zero (true - 0)
else
if condition is not true then
$ vi isnump_n
# Script to see whether argument is positive or negative
#
if [ $# -eq 0 ]
then
echo "$0 : You must give/supply one integers"
exit 1
fi
if test $1 -gt 0
then
echo "$1 number is positive"
else
echo "$1 number is negative"
fi
Multilevel if-then-else
Syntax:
if condition
then
condition is zero (true - 0)
execute all commands up to elif statement
elif condition1
then
condition1 is zero (true - 0)
execute all commands up to elif statement
elif condition2
then
condition2 is zero (true - 0)
execute all commands up to elif statement
else
None of the above condtion,condtion1,condtion2 are true
(i.e.
all of the above nonzero or false)
execute all commands up to fi
fi
Bash supports:
for loop
while loop
(a) First, the variable used in loop condition must be initialized, then execution of the
loop begins.
(c) The body of loop ends with a statement that modifies the value of the test (condition)
variable.
for Loop
Syntax:
Syntax:
EXAMPLE
Syntax:
while [ condition ]
do
command1
command2
command3
..
....
done
EXAMPLE
case $variable-name in
pattern1) command
...
..
command;;
pattern2) command
...
..
command;;
patternN) command
...
..
command;;
*) command
...
..
command;;
esac
The $variable-name is compared against the patterns until a match is found. The shell then
executes all the statements up to the two semicolons that are next to each other. The default is *)
and its executed if no match is found. For e.g. write script as follows:
Aim: Write a C program to implement the various process scheduling mechanisms such as FCFS, SJF,
Priority .
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Set the waiting of the first process as ‘0’ and its burst time as its turn around time
(a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1)
(b) Turn around time for Process(n)= waiting time of Process(n)+ Burst time for process(n)
Step 6: Calculate
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Start the Ready Q according the shortest Burst time by sorting according to lowest to
highest burst time.
Step 5: Set the waiting time of the first process as ‘0’ and its turnaround time as its burst time.
(a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1)
(b) Turn around time for Process(n)= waiting time of Process(n)+ Burst time for process(n)
Step 6: Calculate
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 5: Set the waiting of the first process as ‘0’ and its burst time as its turn around time
(a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1)
(b) Turn around time for Process(n)= waiting time of Process(n)+ Burst time for process(n)
Step 7: Calculate
Aim:
Algorithm:
Start ;
/* Initialize semaphore variables*/
Repeat continuously
End
Reepeat continuously
End
Step a: initialize two semaphore mutex=1 and db=1 and rc, (Mutex controls the access to read count rc)
Step b: create two threads one as Reader() another as Writer()
Reader Process:
Step 1: Get exclusive access to rc(lock Mutex)
Step 2: Increment rc by 1
Step 3: Get the exclusive access bd(lock bd)
Step 4: Release exclusive access to rc(unlock Mutex)
Step 5: Release exclusive access to rc(unlock Mutex)
Step 6: Read the data from database
Step 7: Get the exclusive access to rc(lock mutex)
Step 8: Decrement rc by 1, if rc =0 this is the last reader.
Step 9: Release exclusive access to database(unlock mutex)
Step 10 Release exclusive access to rc(unlock mutex)
Algorithm:
1. Initialize the state array S as 0, Si =0 if the philosopher i is thinking or 1 if hungry.
2. Associate two functions getfork(i) and putfork(i) for each philosopher i.
3. For each philiosopher I call getfork(i) , test(i) and putfork(i) if i is 0
4. Stop
Algorithm for getfork(i):
Step 1: set S[i]= 1 i.e. the philosopher i is hungry
Step 2: call test(i)
AIM:
a) First fit
b) Best fit
c) Worst fit &
d) To make comparative study
THEORY:
In an environment that supports dynamic memory allocation, a number of strategies are used to
allocate a memory space of size n (unused memory partition) from the list free holes to the processes
that are competing for memory.
First Fit: Allocation the first hole which is big enough.
ALGORITHM:
Step 3: Get the number of processes and values of block size for each process.
Step 4: First fit algorithm searches all the entire memory block until a hole which is big enough is
Step 5: Best-fit algorithm searches the memory blocks for the smallest hole which can be allocated to
Step 6: Worst fit algorithm searches the memory blocks for the largest hole and allocates it to the
process.
Step 7: Analyses all the three memory management techniques and display the best algorithm which
Aim:
Algorithm:
Step 1: Start.
Step 7: Check there is any cosumer.If yes check whether the buffer is empty
Step 10: Repeat checking for the producer and consumer till required
Algorithm: