0% found this document useful (0 votes)
398 views

OS Lab Manual 4

This document is an instructor laboratory manual for an Operating Systems course at Fatima Jinnah Women University. It contains 13 labs covering topics like introduction to Linux, shell commands, shell programming, system calls, interprocess communication, memory management, multiprocessing, and CPU scheduling algorithms. The labs are designed to provide hands-on experience with these concepts. Students are expected to complete the exercises in the manual and demonstrate their understanding. Guidelines are provided for maintaining and submitting the manual for grading.

Uploaded by

Laiba Babar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
398 views

OS Lab Manual 4

This document is an instructor laboratory manual for an Operating Systems course at Fatima Jinnah Women University. It contains 13 labs covering topics like introduction to Linux, shell commands, shell programming, system calls, interprocess communication, memory management, multiprocessing, and CPU scheduling algorithms. The labs are designed to provide hands-on experience with these concepts. Students are expected to complete the exercises in the manual and demonstrate their understanding. Guidelines are provided for maintaining and submitting the manual for grading.

Uploaded by

Laiba Babar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Operating

System
Lab Manual

Fatima Jinnah Women University,


Rawalpindi

Operating System
BCS-132

Mr. Majid Shafique


Lab Engineer
majidshafique@fjwu.edupk

Instructor Laboratory Manual

DEPARTMENT OF COMPUTER (BCS)


Fatima Jinnah Women University, Rawalpindi
www.fjwu.edu.pk

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

Introduction to 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

Why and where it is used

Why Shell scripts?

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.

Uses of Shell scripts

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.

3. Automating repetitive tasks.

4. Executing important system procedures, like shutting down the system, formatting a disk,
creating a file system etc.

5. Performing some operations on many files.

Shells (bash) Structured Language Constructs


4
Linux calculator program (bc)

$ 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.

In Linux Shell Value Meaning Example


Zero Value (0) Yes/True 0
NON-ZERO Value No/False -1, 32, 55 anything
but not zero

Remember both bc and Linux Shell uses different ways to show True/False values.

Value Shown in bc as Shown in Linux Shell as


True/Yes 1 0
False/No 0 Non - zero value

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

Condition is defined as:


"Condition is nothing but comparison between two values."

Example:

Type following commands (assumes you have file called foo)


$ cat foo
A file foo is created
$ echo $?
The cat command return zero (0) i.e. exit status, on successful, this can be used, in if condition
as follows,

Write shell script as:

$ cat > showfile


#
#
#Script to print file
#
if cat $1
then
echo "File $1, found and successfully echoed"
fi

Run above script as:


$ chmod 755 showfile

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

If given condition is true then command1 is executed otherwise command2 is executed.

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:

Write Script as follows:

$ 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

echo "1. Unix (Sun Os)"


echo "2. Linux (Red Hat)"
echo "Select your os choice [1 or 2]? "
read a

if [ $a -eq 1 ]
then
echo "You Pick up Unix (Sun Os)"

else #### nested if i.e. if within if ######

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

Run the above shell script as follows:


$ chmod 755 nestedif.sh
$ ./nestedif.sh
1. Unix (Sun Os)
2. Linux (Red Hat)
Select you os choice [1 or 2]? 1
You Pick up Unix (Sun Os)

$ ./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 ]

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.

$ cat > ispostive


#
#
# Script to see whether argument is positive
#
if test $1 -gt 0
then

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

For multilevel if-then-else statement try the following script:

$ cat > elf


#
# Script to test if..elif...else
#
if [ $1 -gt 0 ]; then
echo "$1 is positive"
elif [ $1 -lt 0 ]
then
echo "$1 is negative"
elif [ $1 -eq 0 ]
then
echo "$1 is zero"
else
echo "Opps! $1 is not number, give number"
fi

Try above script as follows:


$ chmod 755 elf
$ ./elf 1

$ ./elf -2

$ ./elf 0
13
$ ./elf a

Here o/p for last sample run:


./elf: [: -gt: unary operator expected
./elf: [: -lt: unary operator expected
./elf: [: -eq: unary operator expected
Opps! a is not number, give number
Above program gives error for last run, here integer comparison is expected therefore error
like "./elf: [: -gt: unary operator expected" occurs, but still our program notify this error to
user by providing message "Opps! a is not number, give number".

LAB TASKS

Example of HELLO WORLD IN SHELL SCRIPT

CREATING A FILE NAME “hello” ON DESKTOP :

GIVING THE PERMISION OF READ WRITE AND EXECUTE TO FILE “hello:

WRITE THE CODE OF FILE IN DEFAULT EDITOR:

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

$ chmod 755 trmif

Answer the following question in reference to above script:


1. foo file exists on your disk and you give command, $ ./trmfi foo what will be output?

15
2. If bar file not present on your disk and you give command, $ ./trmfi bar what will be
output?

3. And if you type $ ./trmfi What will be output?

Task 2:

Write a shell script that computes the gross salary of an employee according to the following:

1) if basic salary is <1500 then HRA=10% of the basic salary.


2) if basic salary is >1500 then HRA=20% of the basic salary.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy