OS Lab Manual 4
OS Lab Manual 4
System
Lab Manual
Operating System
BCS-132
PREFACE
20-21
1
This lab manual has been prepared to facilitate the students of Computer Science in studying and
analyzing different operating systems i.e. LINUX or Ubuntu The lab sessions are designed to
improve the abilities of the students by giving hands on experience. After completing the
laboratory exercises, the students will be familiar with the practical issues of the different concepts
explained in the course i.e. real time network configuration and Shell programming
.
PREPARED BY
Lab manual is prepared by Ms. Madiha Wahid, Ms. Mehreen Kamran, Ms. Bakhtawar Seerat and
Mr. Majid Shafique under the supervision of Head of Department Dr. Nargis Bibi
GENERAL INSTRUCTIONS
a. Students are required to maintain the lab manual with them till the end of the semester.
b. All readings, answers to questions and illustrations must be solved on the place
provided. If more space is required, then additional sheets may be attached. You may
add screen print to the report by using the ‘Print Screen’ command on your keyboard
to get a snapshot of the displayed output.
c. It is the responsibility of the student to have the manual graded before deadlines as
given by the instructor.
d. Loss of manual will result in re submission of the complete manual.
e. Students are required to go through the experiment before coming to the lab session.
f. Students must bring the manual in each lab.
g. Keep the manual neat clean and presentable.
h. Plagiarism is strictly forbidden. No credit will be given if a lab session is plagiarized
and no re-submission will be entertained.
i. Marks will be deducted for late submission.
j. You need to submit the report even if you have demonstrated the exercises to the lab
instructor or shown them the lab report during the lab session
2
Contents
Lab 01
Introduction to Linux 2
Lab 02
Introduction to Basic Shell commands 5
Lab 03
Implementing Linux Commands 10
Lab 04
Introduction to Shell Programming 15
Lab 05
System Calls in UNIX 24
Lab 06 & 07
System Calls in UNIX 29
Lab 08
Interprocess Communication using Pipes 36
Lab 09
Memory Management Schemes- First Fit Best Fit 40
Lab 10
Multithreading 43
Lab 11
Implement Shortest Job First (Non-Preemptive) CPU Scheduling Algorithm 48
Lab 12
Implement Round Robin CPU Scheduling Algorithm 51
Lab 13
Implement Banker’s Algorithm 53
3
Lab 04
Summary
Items Description
Course Title Operating System
Lab Title
Introduction to Shell Programming
Duration 3 Hours
Operating System Linux Operating System
/Tool/Language
Objective To get familiar with the basic concepts of shell programming
Shell
A shell is a command line interpreter. It takes commands and executes them. As such, it
implements a programming language.
Shell scripts
A shell script or a shell program is a series of commands put in a file and executed by the
Shell
Since the user cannot interact with the kernel directly, Shell programming skills are a must to
be able to exploit the power of UNIX to the fullest extent. A shell script can be used for
variety of tasks and some of them are listed below.
1. Customizing your work environment For Example Every time you login, if you want to see
the current date, a welcome message, and the list of users who have logged in you can write a
shell script for the same.
2. Automating your daily tasks. For example, to back up all the programs at the end of the day.
4. Executing important system procedures, like shutting down the system, formatting a disk,
creating a file system etc.
$ bc
After this command bc is started and waiting for your commands, i.e. give it some calculation
as follows type 5 + 2 as:
5+2
7
7 is response of bc i.e. addition of 5 + 2.
5>2
1
1 (One?) is response of bc, How? bc compare 5 with 2 as, Is 5 is greater then 2, and print the
answer by showing 1 value. Now try
5<2
0
0 (Zero) indicates the false i.e. Is 5 is less than 2?
Try other operators in bc to clear your idea and see bc's response
Whenever there is any type of comparison in Linux Shell, it gives only two answer one is YES
and other is NO.
Remember both bc and Linux Shell uses different ways to show True/False values.
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
5
then
command1 if condition is true
or if exit status of condition is 0 (zero)
...
...
fi
Example:
6
$./showfile foo
Shell script name is showfile ($0) and foo is argument (which is $1).Then shell compare it as
follows:
if cat $1 which is expanded to if cat foo.
if cat command finds foo file and if its successfully shown on screen, it means our cat
command is successful and its exist status is 0 (indicates success), So our if condition is also
true and hence statement echo " File $1, found and successfully echoed" is proceed by shell.
Now if cat command is not successful then it returns non-zero value (indicates some sort of
failure) and this statement echo " File $1, found and successfully echoed" is skipped by our
shell.
if...else...fi
Syntax:
if condition
then
condition is zero (true - 0)
execute all commands up to else statement
else
if condition is not true then
execute all commands up to fi
fi
Example:
$ vi isnump_n
#
#
7
# Script to see whether argument is positive or negative
#
if [ $# -eq 0 ]
then
echo "$0 : You must give one integer"
else
if test $1 -gt 0
then
echo "$1 number is positive"
else
echo "$1 number is negative"
fi
Fi
Try it as follows:
8
$ chmod 755 isnump_n
$ ./isnump_n 5
5 number is positive
$ ./isnump_n -45
-45 number is negative
$ ./isnump_n
./ispos_n : You must give/supply one integers
$ isnump_n 0
0 number is negative
Example:
You can write the entire if-else construct within either the body of the if statement of the body
of an else statement. This is called the nesting of ifs.
$ vi nestedif.sh
a=0
if [ $a -eq 1 ]
then
echo "You Pick up Unix (Sun Os)"
if [ $a -eq 2 ]
then
echo "You Pick up Linux (Red Hat)"
else
echo "What you don't like Unix/Linux OS."
9
fi
fi
$ ./nestedif.sh
1. Unix (Sun Os)
2. Linux (Red Hat)
Select you os choice [1 or 2]? 2
You Pick up Linux (Red Hat)
10
$ ./nestedif.sh
1. Unix (Sun Os)
2. Linux (Red Hat)
Select you os choice [1 or 2]? 3
What you don't like Unix/Linux OS.
Nested … if
You can use the nested ifs as follows also:
Syntax:
if condition
then
if condition
then
.....
..
do this
else
....
..
do this
fi
else
...
.....
do this
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:
Following script determine whether given argument number is positive.
11
echo "$1 number is positive"
fi
Run it as follows:
$ chmod 755 ispostive
$./ ispostive 5
5 number is positive
$./ispostive -45
Nothing is printed
$./ispostive
./ispostive: test: -gt: unary operator expected
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
12
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
$ ./elf -2
$ ./elf 0
13
$ ./elf a
LAB TASKS
14
EXECUTE THE FILE IN TERMINAL:
Task 1:
Write shell script as follows:
cat > trmif
#
# Script to test rm command and exist status
#
if rm $1
then
echo "$1 file deleted"
fi
Press Ctrl + d to save
15
2. If bar file not present on your disk and you give command, $ ./trmfi bar what will be
output?
Task 2:
Write a shell script that computes the gross salary of an employee according to the following:
16
Task 3:
Write a shell script to ADD two numbers taken from argument?
Note: show error if no argument or more than 2 arguments are passed
$ ./ADD 5 6
Output : Sum of 5 and 6 = 5+6 = 11
17