Os Labs
Os Labs
Os Labs
What is Linux?
Linux is the most popular server OS. It is distributed under an open source license. Linux is a clone of
UNIX. Its functionality list is quite like UNIX. Knowing one is as good as knowing the other.
However, when his suggestions were rejected by the designers of UNIX, he thought of launching an OS
which will be receptive to changes, modifications suggested by its users. So Linus devised a Kernel
named Linux in 1991. Though he would need programs like File Manager, Document Editors, Audio -
Video programs to run on it. Something as you have a cone but no ice-cream on top.
As time passed by, he collaborated with other programmers in places like MIT and applications for
Linux started to appear. So around 1991, a working Linux operating system with some applications was
officially launched, and this was the start of one of the most loved and open-source OS options
available today.
The main advantage of Linux was that programmers were able to use the Linux Kernel to design their
own custom operating systems. With time, a new range of user-friendly OS's stormed the computer
world. Now, Linux is one of the most popular and widely used Kernel, and it is the backbone of popular
operating systems like Debian, Knoppix, Ubuntu, and Fedora. Nevertheless, the list does not end here
as there are thousands of OS's based on Linux which offer a variety of functions to the users.
Linux Kernel is normally used in combination of GNU project by Dr. Richard Stallman. All mordern
distributions of Linux are actually distributions of Linux/GNU
Linux now enjoys popularity at its prime, and it's famous among programmers as well as regular
computer users around the world. Its main benefits are -
It offers a free operating system. You do not have to shell hundreds of dollars to get the OS like
Windows!
Is it for me?
Users, who are new to Linux, usually shun it by falsely considering it as a difficult and technical OS to
operate but, to state the truth, in the last few years Linux operating systems have become a lot more
user-friendly than their counterparts like Windows, so trying them is the best way to know whether
Linux suits you or not.
There are thousands of Linux based operating systems; most of them offer state-of-the-art security
and applications, all of it for free!
There are hundreds of Linux operating systems or Distributions available these days. Many of them are
designed with a specific purpose in mind. For example, to run a web server or to run on network
switches like routers, modems, etc.
The latest example of one of the most popular smartphone-based Linux Distribution is Android!
Here, are a few popular Linux Distributions (also called Linux Distro) -
Here, are a few popular Linux Distributions (also called Linux Distro) -
The term best is relative. Each Linux distribution is built for a specific purpose-built to meet the
demands of its target users.
The desktop Distributions are available for free on their respective websites. You might want to try
them one by one till you get to know which Distribution you like the most. Each one of them offers its
own unique design, applications, and security.
This is one of the easiest methods of installing Ubuntu or any distribution on your computer. Follow
the steps.
Step 1) Download the .iso or the OS files on your computer from this link.
Step 2) Download free software like 'Universal USB installer to make a bootable USB stick.
Step 3) Select an Ubuntu Distribution form the dropdown to put on your USB
Select the drive letter of USB to install Ubuntu and Press create button.
Mac OS uses a UNIX core. Your switch from Mac OS to Linux will be relatively smooth.
It's the Windows users who will need some adjusting. In this tutorial will introduce the Linux OS and
compare it with Windows.
In Microsoft Windows, files are stored in folders on different data drives like C: D: E:
But, in Linux, files are ordered in a tree structure starting with the root directory.
This root directory can be considered as the start of the file system, and it further branches out various
other sub-directories. The root is denoted with a forward slash '/'.
A general tree file system on your UNIX may look like this.
Types of Files
In Linux and UNIX, everything is a file. Directories are files, files are files, and devices like Printer,
mouse, keyboard etc.are files.
General Files
General Files also called as Ordinary files. They can contain image, video, program or simply text. They
can be in ASCII or a Binary format. These are the most commonly used files by Linux Users.
Directory Files
These files are a warehouse for other file types. You can have a directory file within a directory (sub-
directory).You can take them as 'Folders' found in Windows operating system.
Device Files:
In MS Windows, devices like Printers, CD-ROM, and hard drives are represented as drive letters like G:
H:. In Linux, there are represented as files.For example, if the first SATA hard drive had three primary
partitions, they would be named and numbered as /dev/sda1, /dev/sda2 and /dev/sda3.
Note: All device files reside in the directory /dev/
All the above file types (including devices) have permissions, which allow a user to read, edit or
execute (run) them. This is a powerful Linux/Unix feature. Access restrictions can be applied for
different kinds of users, by changing permissions.
1. Regular
2. Administrative(root)
3. Service
Regular User
A regular user account is created for you when you install Ubuntu on your system. All your files and
folders are stored in /home/ which is your home directory. As a regular user, you do not have access
to directories of other users.
Root User
Other than your regular account another user account called root is created at the time of installation.
The root account is a superuser who can access restricted files, install software and has administrative
privileges. Whenever you want to install software, make changes to system files or perform any
administrative task on Linux; you need to log in as a root user. Otherwise, for general tasks like playing
music and browsing the internet, you can use your regular account.
Service user
Linux is widely used as a Server Operating System. Services such as Apache, Squid, email, etc. have
their own individual service accounts. Having service accounts increases the security of your
computer. Linux can allow or deny access to various resources depending on the service.
Note:
1. Administrator
2. Standard
3. Child
4. Guest
Windows Vs. Linux: File Name Convention
In Windows, you cannot have 2 files with the same name in the same folder. While in Linux, you can
have 2 files with the same name in the same directory, provided they use different cases.
Consider, a regular user account "Tom". He can store his personal files and directories in the directory
"/home/tom". He can't save files outside his user directory and does not have access to directories of
other users. For instance, he cannot access directory "/home/jerry" of another user account"Jerry".
When you boot the Linux operating system, your user directory (from the above example /home/tom)
is the default working directory. Hence the directory "/home/tom is also called the Home directory
which is a misnomer.
The working directory can be changed using some commands which we will learn later.
In Windows, System and Program files are usually saved in C: drive. But, in Linux, you would find the
system and program files in different directories. For example, the boot files are stored in the /boot
directory, and program and software files can be found under /bin, device files in /dev. Below are
important Linux Directories and a short description of what they contain.
These are most striking differences between Linux and other Operating Systems. There are more
variations you will observe when switching to Linux and we will discuss them as we move along in our
tutorials.
Windows Linux
Windows uses different data drives like C: D: E to Unix/Linux uses a tree like a hierarchical file
stored files and folders. system.
Hard drives, CD-ROMs, printers are considered as Peripherals like hard drives, CD-ROMs, printers are
devices also considered files in Linux/Unix
There are 4 types of user account types 1) There are 3 types of user account types 1) Regular,
Administrator, 2) Standard, 3) Child, 4) Guest 2) Root and 3) Service Account
Administrator user has all administrative Root user is the super user and has all
privileges of computers. administrative privileges.
The most frequent tasks that you perform on your PC is creating, moving or deleting Files. Let's look at
various options for File Management.
Even though the world is moving to GUI based systems, CLI has its specific uses and is widely used in
scripting and server administration. Let's look at it some compelling uses -
Comparatively, Commands offer more options & are flexible. Piping and stdin/stdout are
immensely powerful are not available in GUI
Some configurations in GUI are up to 5 screens deep while in a CLI it's just a single command
Moving, renaming 1000's of the file in GUI will be time-consuming (Using Control /Shift to
select multiple files), while in CLI, using regular expressions so can do the same task with a
single command.
CLI load fast and do not consume RAM compared to GUI. In crunch scenarios this matters.
Both GUI and CLI have their specific uses. For example, in GUI, performance monitoring graphs give
instant visual feedback on system health, while seeing hundreds of lines of logs in CLI is an eyesore.
You must learn to use both GUI(File Manager) and CLI (Terminal)
GUI of a Linux based OS is similar to any other OS. Hence, we will focus on CLI and learn some useful
commands.
Once you launch the CLI (Terminal), you would find something as guru99@VirtualBox(see image)
written on it.
1) The first part of this line is the name of the user (nabeel, ubuntu, home...)
2) The second part is the computer name(nex) or the host name. The hostname helps identify a
computer over the network. In a server environment, host-name becomes important.
In the above illustration, we have moved from the /home directory to /bin using the 'cd' command.
The ~ sign does not display while working in /bin directory. It appears while moving back to the home
directory.
5) The '$' sign suggests that you are working as a regular user in Linux. While working as a root user, '#'
is displayed.
The directory that you are currently browsing is called the Present working directory. You log on to the
home directory when you boot your PC. If you want to determine the directory you are presently
working on, use the command -
pwd
pwd command stands for print working directory
Above figure shows that /home/guru99 is the directory we are currently working on.
Changing Directories
If you want to change your current directory use the 'cd' command.
cd /tem
Here, we moved from directory /tmp to /bin to /usr and then back to /tmp.
cd ~
Moving to root directory
The root of the file system in Linux is denoted by '/'. Similar to 'c:\' in Windows.
Note: In Windows, you use backward slash "\" while in UNIX/Linux, forward slash is used "/"
cd /
TIP: Do not forget space between cd and /. Otherwise, you will get an error.
You can navigate through multiple directories at the same time by specifying its complete path.
Example: If you want to move the /cpu directory under /dev, we do not need to break this operation in
two parts.
cd /dev/cpu
cd ..
Here by using the 'cd ..' command, we have moved up one directory from '/dev/cpu' to '/dev'.
Then by again using the same command, we have jumped from '/dev' to '/' root directory.
Example -
In Windows
C:\documentsandsettings\user\downloads
In Linux
/home/user/downloads
1. Absolute Path:
Let's say you have to browse the images stored in the Pictures directory of the home folder.
2. Relative Path:
The Relative path comes in handy when you have to browse another subdirectory within a given
directory.
It saves you from the effort to type complete paths all the time.
Suppose you are currently in your Home directory. You want to navigate to the Downloads directory.
cd /home/guru99/Downloads
Instead, you can simply type 'cd Downloads' and you would navigate to the Downloads directory as
you are already present within the '/home/guru99' directory.
cd Downloads
This way you do not have to specify the complete path to reach a specific location within the same
directory in the file system
Lab Session # 02
OBJECT: Executing some of the most frequently used Linux commands
THEORY
A Linux command is any executable file. This means that any executable file added to the system
becomes a new command on the system. A Linux command is a type of file that is designed to be run,
as opposed to files containing data or configuration information.
Or if the command is not on your path type the complete path and name of the command such as:
$ /usr/bin/command.
Some of the frequently used Linux commands are: su, pwd, cd, ls, more and less, find and grep, man.
(Refer to Appendix B for detail of few commands)
1. Su
Description: "su" stands for "super user". Runs a new shell under different user and group IDs. If no
user is specified, the new shell will run as the root user.
Syntax: su [-flmp] [-c command] [-s shell] [--login] [--fast] [--preserve-environment] [--
command=command] [--shell=shell] [-] [user]
(See the man pages for the description of the flags and options by using man su)
$ su <username> (to become another user) or
$ su (to become the root user).
2. pwd
Description: Displays the name of the current directory.pwd stands for present working directory. By
typing this command you are informed of which directory you are currently in.
Syntax: pwd
3. cd
Description: Changes the current directory to any accessible directory on the system.
Syntax: For instance to change from /home/user1 to a subdirectory of user1 wordfiles use the
following:
$ cd wordfiles
$ pwd
/home/user1/wordfiles
To change to /tmp use the following:
$ cd /tmp
$ pwd
/tmp
4. ls
Description: Displays the listing of files and directories. If no file or directory is specified, then the
current directory’s contents are displayed. By default the contents are sorted alphabetically.
(See the man pages for the description of the flags and options by using man ls)
5. more
Description: Displays one or more files screen by screen and allows for searching and jumping to an
arbitrary location in the file.
For example if there is a large text file called “textfile”, it can be viewed a page at a time with the given
command: $ more textfile
After pressing Enter, the first screen of the file is seen with the text -More- displayed on the last line of
the screen. Pressing the space bar jumps forward a full screen length, while pressing enter key moves
forward one line at a time. When the end of the body of the text is reached, the command prompt
appears.
To search forward through a file, enter slash followed by the word or phrase you want to search for
and then press enter. The display jumps forward to the first occurrence of the word or phrase being
searched for and displays the occurrence near the top of the screen. The same search can be repeated
by entering n after the first search, avoiding the need to type the same word or phrase repeatedly.
6. less
Description: Displays a text file one screen at a time while allowing searching and backward scrolling.
Syntax: less [-aeEGiINrsS] file….. (See the man pages for the description of the flags and options by
using man less)
This command is an improved version of the previous one. On addition to the functions previously
described the following are some of the other actions that can be performed using less:
7. find
Description: Looks for files below the specified paths that match all the criteria indicated by the
command-line options and takes any action indicated by those options. If no paths are specified, the
search takes place below the current directory.
Description: Searches files for lines matching a specific pattern and displays the lines
Grep stands for Global Regular Expression Parser. What grep does, essentially, is find and display lines
that contain a pattern that you specify. There are two basic ways to use grep.
The first use of grep is to filter the output of other commands. The general syntax is <command> |
grep <pattern>. For instance, if we wanted to see every actively running process on the system, we
would type ps -a | grep R. In this application, grep passes on only those lines that contain the pattern
(in this case, the single letter) R. Note that if someone were running a program called Resting, it would
show up even if its status were S for sleeping, because grep would match the R in Resting. An easy way
around this problem is to type grep " R ", which explicitly tells grep to search for an R with a space on
each side. You must use quotes whenever you search for a pattern that contains one or more blank
spaces.
The second use of grep is to search for lines that contain a specified pattern in a specified file. The
syntax here is grep <pattern> <filename>. Be careful. It's easy to specify the filename first and the
pattern second by mistake! Again, you should be as specific as you can with the pattern to be matched,
in order to avoid "false" matches.
Syntax: Assuming that you are in your home directory, then the following command searches for the
word “hello” in each file in your home directory and produces the results as follows:
$ grep hello*
This command then returns one line for each occurrence of the word. The name of the file is also
shown.
In general the pattern for the grep command is:
$ grep text-pattern file-list
The text-pattern can be a simple word or phrase or a more complicated regular expression. The use of
regular expressions can be found in the man pages.
The file-list can take any form allowed by the shell.
To check for the contents of all files in a directory use the following:
$ grep text-pattern *
where * indicates that all files in the current directory should be searched.
In its simplest form, the text pattern is a single word or part of a word containing no spaces. To search
for a phrase such as “is a test”, enclose the pattern in quotation marks as follows:
$ grep “is a test” *
(See the man pages for the description of the flags and options by using man grep)
The man pages are manual pages provided in a standard format with most Linux software. Almost all
the commands that ship with Red Hat Linux distribution include man pages. Using the man command
in its most basic form, any existing man page can be read:
$ man command-name
The above displays the man page for the specified command and allows scrolling through it and
searching it the same way as when using the less command to display text. If the specified man page
cannot be found an error is displayed.
EXERCISES
1. Write a command to:
List all files (and subdirectories) in the home directory.
Display the content of /etc/passwd file with as many lines at a time as the last digit of
your roll number.
Search the current directory, look for filenames that don’t begin with a capital letter.
Search the system for files that were modified within the last two days.
List all file names containing your roll number in the end.
List all files in the current directory and their sizes; use multiple columns and mark
special files.
List C source files in the current directory, showing larger file first.
Create a group called directors and assign it a password. Add user Ann and user James
to the group and assign passwords to each of them.
LAB SESSION # 3
OBJECT: File Permissions in Linux/Unix
User
A user is the owner of the file. By default, the person who created a file
becomes its owner.
Group
A user- group can contain multiple users. All users belonging to a group will
have the same access permissions to the file. Suppose you have a project
where a number of people require access to a file. Instead of manually
assigning permissions to each user, you could add all users to a group, and
assign group permission to file such that only this group members and no one
else can read or modify the files.
Other
Any other user who has access to a file. This person has neither created the
file, nor he belongs to a usergroup who could own the file. Practically, it
means everybody else. Hence, when you set the permission for others, it is
also referred as set permissions for the world.
Permissions
Every file and directory in your UNIX/Linux system has following 3 permissions
defined for all the 3 owners discussed above.
Read: This permission give you the authority to open and read a file. Read
permission on a directory gives you the ability to lists its content.
Write: The write permission gives you the authority to modify the contents of
a file. The write permission on a directory gives you the authority to add,
remove and rename files stored in the directory. Consider a scenario where
you have to write permission on file but do not have write permission on the
directory where the file is stored. You will be able to modify the file contents.
But you will not be able to rename, move or remove the file from the
directory.
Execute: In Windows, an executable program usually has an extension ".exe"
and which you can easily run. In Unix/Linux, you cannot run a program unless
the execute permission is set. If the execute permission is not set, you might
still be able to see/modify the program code(provided read & write
permissions are set), but not run it.
ls - l on terminal gives
Here, the first '-' implies that it is a file Else, if it were a directory, ‘d’ would have been
shown.
The first part of the code is 'rw-'. This suggests that the owner 'Home' can:
Read the file
Write or edit the file
He cannot execute the file since the execute bit is set to '-'.
The second part is 'rw-'. It for the user group 'Home' and group-members can:
Read the file
Write or edit the file
The third part is for the world which means any user. It says 'r--'. This means the user
can only:
Read the file
Changing file/directory permissions
'chmod' command is used to change the file/directory permissions
1. Absolute(Numeric) Mode
In this mode, file permissions are not represented as characters but a three-digit octal
number.
The table below gives numbers for all for permissions types.
2. Symbolic Mode
In the Absolute mode, you change permissions for all 3 owners. In the symbolic mode,
you can modify permissions of a specific owner. It makes use of mathematical
symbols to modify the file permissions.
Operator Description
+ Adds a permission to a file or directory
- Removes the permission
Sets the permission and overrides the permissions
=
set earlier.
We will not be using permissions in numbers like 755 but characters like rwx. Let's
look into an example
Changing Ownership and Group
For changing the ownership of a file/directory, you can use the following command:
chown user
In case you want to change the user as well as group for a file or directory use the
command
chown user:group filename
The 'chgrp' command can change the group ownership chrgrp group filename
LAB SESSION # 4
OBJECT: Input/Output Redirection In Linux
What is Redirection?
Redirection is a feature in Linux such that when executing a command, you can change the
standard input/output devices. The basic workflow of any Linux command is that it takes an
input and give an output.
Output Redirection
The '>' symbol is used for output (STDOUT) redirection.
Example:
Here the output of command ls -al is re-directed to file "listings" instead of your screen.
Note: Use the correct file name while redirecting command output to a file. If there is an
existing file with the same name, the redirected command will delete the contents of that file
and then it may be overwritten." If you do not want a file to be overwritten but want to add
more content to an existing file, then you should use '>>' operator.
Input redirection
The '<' symbol is used for input(STDIN) redirection
Example: The mail program in Linux can help you send emails from the Terminal.
You can type the contents of the email using the standard device keyboard. But if you want to
attach a File to email you can use the input re-direction operator in the following format.
This would attach the file with the email, and it would be sent to the recipient.
The above examples were simple. Let's look at some advance re-direction techniques which
make use of File Descriptors.
Your screen also has a File Descriptor. When a program is executed the output is sent to File
Descriptor of the screen, and you see program output on your monitor. If the output is sent
to File Descriptor of the printer, the program output would have been printed.
Error Redirection
Whenever you execute a program/command at the terminal, 3 files are always open, viz.,
standard input, standard output, standard error. These files are always present whenever a
program is run. As explained before a file descriptor, is associated with each of these files.
By default, error stream is displayed on the screen. Error redirection is routing the errors to a
file other than the screen.
Example 1
$ myprogram 2>errorsfile
Example 2
Here is another example which uses cat statement -
Cat filename.ext 2>error.log
LAB SESSION # 5
OBJECT: Linux/Unix Process Management
What is a Process?
An instance of a program is called a Process. In simple terms, any command that you
give to your Linux machine starts a new process.
Having multiple processes for the same program is possible.
Types of Processes:
Foreground Processes: They run on the screen and need input from the user.
For example, Office Programs
Background Processes: They run in the background and usually do not need
user input. For example, Antivirus.
1. Bg/Fg
Example:
1. Launch 'banshee' music player
2. Stop it with the 'ctrl +z' command
3. Continue it with the 'fg' utility.
2. Top
This utility tells the user about all the running processes on the Linux machine.
Press 'q' on the keyboard to move out of the process display.
The terminology follows:
ps ux
You can also check the process status of a single process, use the syntax -
ps PID
4. Kill
This command terminates running processes on a Linux machine.
To use these utilities you need to know the PID (process id) of the process you want
to kill
Syntax - kill PID
To find the PID of a process simply type
pidof Process name
Let us try it with an example.
5. NICE
Linux can run a lot of processes at a time, which can slow down the speed of some
high priority processes and result in poor performance. To avoid this, you can tell
your machine to prioritize processes as per your requirements. This priority is called
Niceness in Linux, and it has a value between -20 to 19. The lower the Niceness index,
the higher would be a priority given to that task. The default value of all the
processes is 0.
To start a process with a niceness value other than the default value use the following
syntax
To change Niceness, you can use the 'top' command to determine the PID (process id)
and its Nice value. Later use the renice command to change the value. Let us
understand this by an example.
6. DF
This utility reports the free disk space(Hard Disk) on all the file systems. If you want
the information in a readable format, then use the command
'df -h'
7. Free
This command shows the free and used memory (RAM) on the Linux system.
You can use the arguments
free -m to display output in MB
free -g to display output in GB
LAB SESSION # 6
OBJECT: Shell Scripting
What is a Shell?
An Operating is made of many components, but its two prime components are -
Kernel
Shell
While the Kernel is the innermost part of an operating system, a shell is the
outermost one. As it wraps around the delicate interior of an Operating system
protecting it from accidental damage, hence it is maned Shell.
The shell is much more than just a command interpreter, it is also a programming
language of its own with complete programming language constructs such as
variables, conditional execution, loops, functions and many more.
Types of Shell
There are different flavors of a shell, just as there are different flavors of operating
systems. Each flavor of shell has its own set of recognized commands and functions.
They are divided into two major types.
Bourne shell − If you are using a Bourne-type shell, the $ character is the
default prompt.
The Bourne Shell has the following subcategories −
Bourne shell (sh)
Korn shell (ksh)
Bourne Again shell (bash) -- Default shell for most Linux distributions
POSIX shell (sh)
C shell − If you are using a C-type shell, the % character is the default prompt.
The different C-type shells follow −
C shell (csh)
TENEX/TOPS C shell (tcsh)
Shell scripting is writing a series of command for the shell to execute. It can combine
lengthy and repetitive sequences of commands into a single and simple script, which
can be stored as file and executed anytime.
A shell script or shell program has its own syntax like other programming languages. It
allows user to define variables, assign various values, and so on. Complex programs
may contain conditional statements, loops, and functions.
1. Create a file using a vi editor(or any other editor). Name script file with
extension .sh
2. Start the script with #! /bin/sh
3. Write some code.
4. Save the script file as filename.sh
5. For executing the script type bash filename.sh
The bang line, in step 2, tells the kernel that a specific shell or language is to be used
to interpret the contents of the file. This is the line which is written at the beginning
of every program. It starts with the bang character (#!) and goes for example like this:
#!/bin/bash
This is the link to the Bourne again shell(bash). The instructions written after this bang
line will be interpreted using the above named shell.
Accessing Values: To access the value stored in a variable, prefix its name with the
dollar sign ($)
Example: echo $DEPARTMENT
Unsetting Variables: Unsetting or deleting a variable directs the shell to remove the
variable from the list of variables that it tracks.
Syntax: unset variable_name
Example: unset NAME
#!/bin/sh
echo "what is your name?"
read name
echo "How do you do, $name?"
read remark
echo "I am $remark too!"
Positional parameters
The shell has knowledge of a special kind of variable called a positional parameter.
Positional parameters are used to refer to the parameters that were passed to a shell
program on the command line or a shell function by the shell script that invoked the
function. When you run a shell program that requires or supports a number of
command-line options, each of these options is stored into a positional parameter.
The first parameter is stored into a variable named 1, the second parameter is stored
into a variable named 2, and so forth. To access the values stored in these variables,
you must precede the variable name with a dollar sign ($) just as you do with
variables you define.
The following shell program expects to be invoked with two parameters. The program
takes the two parameters and prints the second parameter that was typed on the
command line first and the first parameter that was typed on the command line
second.
# program reverse, prints the command line parameters out in reverse order
echo "$2" "$1"
Built-in Variables
These are special variables that Linux provides that can be used to make decisions in a
program. Their values cannot be modified. The following table lists these variables
and gives a brief description of what each is used for.
Variable Usage
Stores the first word of the entered command (the name of the shell
$0
program).
$n These variables correspond to the arguments with which a script was
invoked. Here n is a positive decimal number corresponding to the position
of an argument (the first argument is $1, the second argument is $2, and so
on).
Stores the number of command-line arguments that were passed to the
$#
shell program.
$? Stores the exit value of the last command that was executed.
$* Stores all the arguments that were entered on the command line ($1 $2 ...).
Stores all the arguments that were entered on the command line,
"$@"
individually quoted ("$1" "$2" ...).
LAB SESSION # 7
OBJECT: Arrays for Shell
Following is the simplest method of creating an array variable. This helps assign a
value to one of its indices.
array_name[index]=value
NAME[0]="Nabeel"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
After you have set any array variable, you access it as follows −
${array_name[index]}
You can access all the items in an array in one of the following ways −
${array_name[*]}
${array_name[@]}
#!/bin/sh
NAME[0]="Nabeel"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Index: ${NAME[0]}"
echo "Second Index: ${NAME[1]}"
echo "Complete Array First Method: ${NAME[*]}"
echo "Complete Array Second Method: ${NAME[@]}"
LAB SESSION # 8
OBJECT: Shell Basic Operators
There are various operators supported by each shell. We will discuss in detail about
Bourne shell (default shell) in this chapter.
Arithmetic Operators
Relational Operators
Boolean Operators
String Operators
File Test Operators
#!/bin/sh
val=`expr 2 + 2`
echo "Total value : $val"
There must be spaces between operators and expressions. For example, 2+2
is not correct; it should be written as 2 + 2.
The complete expression should be enclosed between ‘ ‘, called the backtick.
Arithmetic Operators
Subtracts right hand operand from left `expr $a - $b` will give
- (Subtraction)
hand operand -10
Multiplies values on either side of the `expr $a \* $b` will
* (Multiplication)
operator give 200
Divides left hand operand by right hand `expr $b / $a` will give
/ (Division)
operand 2
a = $b would assign
= (Assignment) Assigns right operand in left operand
value of b into a
Example
#!/bin/sh
a=10
b=20
val=`expr $a + $b`
echo "a + b : $val"
val=`expr $a - $b`
echo "a - b : $val"
val=`expr $a \* $b`
echo "a * b : $val"
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
echo "b % a : $val"
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
a + b : 30
a - b : -10
a * b : 200
b/a:2
b%a:0
a is not equal to b
The following points need to be considered when using the Arithmetic Operators −
There must be spaces between the operators and the expressions. For
example, 2+2 is not correct; it should be written as 2 + 2.
Complete expression should be enclosed between ‘ ‘, called the inverted
commas.
You should use \ on the * symbol for multiplication.
if...then...fi statement is a decision-making statement which has been
explained in the next section.
Bourne Shell supports the following relational operators that are specific to numeric
values. These operators do not work for string values unless their value is numeric.
Checks if the value of two operands are equal or not; [ $a -eq $b ] is not
-eq
if yes, then the condition becomes true. true.
-ne Checks if the value of two operands are equal or not; [ $a -ne $b ] is true.
if values are not equal, then the condition becomes
true.
Example
#!/bin/sh
a=10
b=20
if [ $a -eq $b ]
then
echo "$a -eq $b : a is equal to b"
else
echo "$a -eq $b: a is not equal to b"
fi
if [ $a -ne $b ]
then
echo "$a -ne $b: a is not equal to b"
else
echo "$a -ne $b : a is equal to b"
fi
if [ $a -gt $b ]
then
echo "$a -gt $b: a is greater than b"
else
echo "$a -gt $b: a is not greater than b"
fi
if [ $a -lt $b ]
then
echo "$a -lt $b: a is less than b"
else
echo "$a -lt $b: a is not less than b"
fi
if [ $a -ge $b ]
then
echo "$a -ge $b: a is greater or equal to b"
else
echo "$a -ge $b: a is not greater or equal to b"
fi
if [ $a -le $b ]
then
echo "$a -le $b: a is less or equal to b"
else
echo "$a -le $b: a is not less or equal to b"
fi
Boolean Operators
This is logical OR. If one of the operands is true, then [ $a -lt 20 -o $b -gt
-o
the condition becomes true. 100 ] is true.
This is logical AND. If both the operands are true, [ $a -lt 20 -a $b -gt
-a
then the condition becomes true otherwise false. 100 ] is false.
Example
#!/bin/sh
a=10
b=20
if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi
String Operators
The following string operators are supported by Bourne Shell.
Assume variable a holds "abc" and variable b holds "efg" then −
Operator Description Example
Checks if the value of two operands are equal or
= [ $a = $b ] is not true.
not; if yes, then the condition becomes true.
Checks if the value of two operands are equal or
!= not; if values are not equal then the condition [ $a != $b ] is true.
becomes true.
Checks if the given string operand size is zero; if
-z [ -z $a ] is not true.
it is zero length, then it returns true.
Example
Live Demo
#!/bin/sh
a="abc"
b="efg"
if [ $a = $b ]
then
echo "$a = $b : a is equal to b"
else
echo "$a = $b: a is not equal to b"
fi
if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi
if [ -z $a ]
then
echo "-z $a : string length is zero"
else
echo "-z $a : string length is not zero"
fi
if [ -n $a ]
then
echo "-n $a : string length is not zero"
else
echo "-n $a : string length is zero"
fi
if [ $a ]
then
echo "$a : string is not empty"
else
echo "$a : string is empty"
fi
We have a few operators that can be used to test various properties associated with
a Unix file.
Assume a variable file holds an existing file name "test" the size of which is 100 bytes
and has read, write and execute permission on −
-c file Checks if file is a character special file; if yes, then [ -c $file ] is false.
the condition becomes true.
Checks if file has its sticky bit set; if yes, then the
-k file [ -k $file ] is false.
condition becomes true.
Example
Assume a variable file holds an existing file name "/home/testuser/test.sh" the size
of which is 100 bytes and has read, write and execute permission −
#!/bin/sh
file="/home/testuser/test.sh"
if [ -r $file ]
then
echo "File has read access"
else
echo "File does not have read access"
fi
LAB SESSION # 9
OBJECT: Decision making (if - else)
Shell supports conditional statements which are used to perform different actions
based on different conditions. We will now understand two decision-making
statements here −
if...fi statement
if...else...fi statement
if...elif...else...fi statement
Syntax
if [ expression ]
then
Statement(s) to be executed if expression is true
fi
Example
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
Syntax
if [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if expression is not true
fi
Example
The above example can also be written using the if...else statement as follows −
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
Example
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
elif [ $a -gt $b ]
then
echo "a is greater than b"
elif [ $a -lt $b ]
then
echo "a is less than b"
else
echo "None of the condition met"
fi
LAB SESSION # 10
OBJECT: Shell Loops
The while loop enables you to execute a set of commands repeatedly until some
condition occurs. It is usually used when you need to manipulate the value of a
variable repeatedly.
Syntax
while command
do
Statement(s) to be executed if command is true
done
Here the Shell command is evaluated. If the resulting value is true, given statement(s)
are executed. If command is false then no statement will be executed and the
program will jump to the next line after the done statement.
Example
Here is a simple example that uses the while loop to display the numbers zero to
nine −
#!/bin/sh
a=0
while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
The for loop operates on lists of items. It repeats a set of commands for every item
in a list.
Syntax
for var in word1 word2 ... wordN
do
Statement(s) to be executed for every word.
done
Here var is the name of a variable and word1 to wordN are sequences of characters
separated by spaces (words). Each time the for loop executes, the value of the
variable var is set to the next word in the list of words, word1 to wordN.
Example
Here is a simple example that uses the for loop to span through the given list of
numbers −
Live Demo
#!/bin/sh
for var in 0 1 2 3 4 5 6 7 8 9
do
echo $var
done
Following is the example to display all the files starting with .bash and available in
your home. We will execute this script from my root −
#!/bin/sh
Until Loop
The while loop is perfect for a situation where you need to execute a set of
commands while some condition is true. Sometimes you need to execute a set of
commands until a condition is true.
Syntax
until command
do
Statement(s) to be executed until command is true
done
Here the Shell command is evaluated. If the resulting value is false, given
statement(s) are executed. If the command is true then no statement will be
executed and the program jumps to the next line after the done statement.
Example
Here is a simple example that uses the until loop to display the numbers zero to nine
−
#!/bin/sh
a=0
until [ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
Example
Here is a simple example which shows that loop terminates as soon as a becomes 5 −
#!/bin/sh
a=0
while [ $a -lt 10 ]
do
echo $a
if [ $a -eq 5 ]
then
break
fi
a=`expr $a + 1`
done
Example
The following loop makes use of the continue statement which returns from the
continue statement and starts processing the next statement −
Live Demo
#!/bin/sh
NUMS="1 2 3 4 5 6 7"
POSIX Introduction:
The POSIX thread libraries are a standard based thread API for C/C++. It allows one
to spawn a new
concurrent process flow. It is most effective on multi-processor or multi-core
systems where the process
flow can be scheduled to run on another processor thus gaining speed through
parallel or distributed
processing. Threads require less overhead than “forking” or “spawning” a new
process because the
system does not initialize a new system virtual memory space and environment for
the process. While
most effective on multiprocessor systems, gains also found on uniprocessor systems
which exploit latency
in I/O and other system functions which may halt process execution. (One thread
may execute while
another is waiting for I/O or some other system latency). All threads within a process
share the same
address space. A thread is created by defining a function and its arguments which
will be processed in the
thread. The purpose of using the POSIX thread library in your software is to execute
software faster.
Thread Basic:
• Thread operations include thread creation, termination, synchronization (joins,
blocking),
scheduling, data management and process interaction.
• A thread does not maintain a list of created threads, nor does it know the thread
that created it.
• All threads within a process share the same address space.
• Threads in the same process share:
o Process instructions
o Most data
o open files (descriptors)
o signals and signal handlers
o current working directory
o User and group id
• Each thread has a unique:
o Thread ID
o set of registers, stack pointer
o stack for local variables, return addresses
o signal mask
o priority
o Return value: errno, …pthread functions return "0" if OK.
Creating Threads:
Initially, your main() program comprises a single, default thread. All other threads
must be explicitly created by
the programmer.
pthread_create (thread,attr,start_routine,arg) creates a new thread and makes it
executable.
This routine can be called any number of times from anywhere within your code.
pthread_create arguments:
o thread: An opaque, unique identifier for the new thread returned by the
subroutine.
o attr: An opaque attribute object that may be used to set thread attributes. You can
specify a thread
attributes object, or NULL for the default values.
o start_routine: the C routine that the thread will execute once it is created.
o arg: A single argument that may be passed to start_routine. It must be passed by
reference as a
pointer cast of type void. NULL may be used if no argument is to be passed.
Thread Attributes:
• By default, a thread is created with certain attributes. Some of these attributes can
be changed
by the programmer via the thread attribute object.
• pthread_attr_init and pthread_attr_destroy are used to initialize/destroy the
thread attribute object.• Other routines are then used to query/set specific
attributes in the thread attribute object.
Attributes include: Detached or joinable state, Scheduling policy, Scheduling
parameters,
Scheduling contention scope, Stack size, Stack address, Stack guard (overflow) size.
Terminating Threads:
There are several ways in which a thread may be terminated:
o The thread returns normally from its starting routine. Its work is done.
o The thread makes a call to the pthread_exit()subroutine - whether its work is done
or not.
o The thread is canceled by another thread via the pthread_cancel() routine.
o The entire process is terminated due to making a call to either the exec() or exit().
o If main() finishes first, without calling pthread_exit() explicitly itself.
Discussion on pthread_exit():
• The pthread_exit() routine allows the programmer to specify an optional
termination status parameter. This optional parameter is typically returned to
threads "joining"
the terminated thread.
• In subroutines that execute to completion normally, you can often dispense with
calling pthread_exit() - unless, of course, you want to pass the optional status code
back.
• Cleanup: the pthread_exit() routine does not close files; any files opened inside the
thread
will remain open after the thread is terminated.
Discussion on calling pthread_exit() from main():
• There is a definite problem if main () finishes before the threads it spawned if you
don't
call pthread_exit() explicitly. All of the threads it created will terminate because main
()
is done and no longer exists to support the threads.
• By having main () explicitly call pthread_exit() as the last thing it does, main() will
block
and be kept alive to support the threads it created until they are done.
EXERCISES
a) Practice following simple code that creates 5 threads with the pthread_create()
routine.
Each thread prints a "Hello World!" message, and then terminates with a call to
pthread_exit(). Attach the snapshot of output window.
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
/* Last thing that main() should do */
pthread_exit(NULL);
}
b) Write a piece of code that demonstrates the thread argument passing in
pthread_create()
method. The code should explain how to pass a simple integer to each thread. The
calling thread
uses a unique data structure for each thread, ensuring that each thread's argument
remains intact
throughout the program. Also attach the print out of code and snapshot of output
window.
code fragment demonstrates how to pass a simple integer to each thread.
long taskids[NUM_THREADS];
for(t=0; t<NUM_THREADS; t++)
{
taskids[t] = t;
printf("Creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *) taskids[t]);
...
}
LAB SESSION # 12
OBJECT: Simulation of First Fit and Best Fit Memory Allocation Algorithms
using C Language
THEORY
Memory management is the act of managing computer memory at the system level.
The essential requirement of memory management is to provide ways to
dynamically allocate portions of memory to programs at their request, and free it for
reuse when no longer needed. This is critical to any advanced computer system
where more than a single process might be underway at any time.
The memory manager is responsible for allocating primary memory to processes and
for assisting the programmer in loading and storing the contents of the primary
memory.
Since primary memory can be space-multiplexed, the memory manager can allocate
a portion of primary memory to "each process" for its own use. Describing the
strategies below:
1. First Fit
In the first fit approach is to allocate the first free partition or hole large enough
which can accommodate the process. It finishes after finding the first suitable free
partition.
Advantage: Fastest algorithm because it searches as little as possible.
Disadvantage: The remaining unused memory areas left after allocation become
waste if it is too smaller. Thus request for larger memory requirement cannot be
accomplished.
2. Best Fit
The best fit deals with allocating the smallest free partition which meets the
requirement of the requesting process. This algorithm first searches the entire list of
free partitions and considers the smallest hole that is adequate. It then tries to find a
hole which is close to actual process size needed.
Advantage: Memory utilization is much better than first fit as it searches the smallest
free partition first available.
Disadvantage: It is slower and may even tend to fill up memory with tiny useless
holes.
3. Worst fit
In worst fit approach is to locate largest available free portion so that the portion left
will be big enough to be useful. It is the reverse of best fit.
Advantage: Reduces the rate of production of small gaps.
Disadvantage: If a process requiring larger memory arrives at a later stage then it
cannot be accommodated as the largest hole is already split and occupied.
EXERCISES
1. Simulate the First Fit Memory Allocation Algorithm in C following the given
steps. Also attach the print out of code and snapshot of output window. For
compilation of C code, refer to steps discussed in Lab Session # 08.
a) Declare structures ‘hole’ and ‘process’ to hold information about set
of holes and processes respectively.
b) Get number of holes, say ‘nh’ and the size of each hole.
c) Get number of processes, say ‘np’. Also get the memory requirements
for each process.
d) Allocate processes to holes, by examining each hole as follows:
i. If hole size > process size then mark process as allocated to
that hole and decrement hole size by process size.
ii. Otherwise check the next from the set of holes.
e) Print the list of process and their allocated holes or unallocated status.
f) Print the list of holes, their actual and current availability.
g) Stop
2. Simulate the Best Fit Memory Allocation Algorithm in C following the given
steps. Also attach the print out of code and snapshot of output window. For
compilation of C code, refer to steps discussed in Lab Session # 08.
a) Declare structures ‘hole’ and ‘process’ to hold information about set
of holes and processes respectively.
b) Get number of holes, say ‘nh’ and the size of each hole.
c) Get number of processes, say ‘np’. Also get the memory requirements
for each process.
d) Allocate processes to holes, by examining each hole as follows:
i. Sort the holes according to their sizes in ascending order.
ii. If hole size > process size then mark process as allocated to
that hole and decrement hole size by process size.
iii. Otherwise check the next from the set of sorted holes.
e) Print the list of process and their allocated holes or unallocated status.
f) Print the list of holes, their actual and current availability.
Stop