Module - 1 - Unix System Programming (BCS515C)
Module - 1 - Unix System Programming (BCS515C)
)
R. L. JALAPPA INSTITUTE OF TECHNOLOGY
(Approved by AICTE, New Delhi, Affiliated to VTU, Belagavi & Accredited by NAAC “A” Grade)
Kodigehalli, Doddaballapur- 561 203
Department of CS&E (Artificial Intelligence & Machine Learning)
Institute Mission
M1: To provide an outstanding Teaching, Learning and Research environment through Innovative Practices
in Quality Education.
M2: Develop Leaders with high level of Professionalism to have career in the Industry, Zeal for Higher
Education, focus on Entrepreneurial and Societal activities.
Department Vision
To empower the students with knowledge and skills to develop the competency in the field of Artificial
Intelligence and Machine Learning.
Department Mission
M1: To craft the students with Novel and Intellectual skills to capability in the field of Artificial Intelligence
and Machine Learning.
M2: To train the students to have Professional career in the field of AI and ML and zeal for Higher Studies
and Research.
Module 1
1. Unix Components/Architecture
The kernel
Is the core of the operating system.
It is a collection of routines(functions) written in C.
It is loaded into memory when the system is booted and communicates directly with the
hardware.
User programs that need to access the hardware use the services of the kernel via use of
system calls and the kernel performs the job on behalf of the user.
Kernel is also responsible for managing system’s memory, schedules processes, and
decides their priorities.
The Shell
This acts as an interface between user and the kernel.
When user types in the terminal , shell interprets the command and calls the program
that user wants. It uses the standard syntax for all commands.
The shell is represented by
Process:
A process, which is the name given to a file when it is executed as a program.
You can say that a process is simply the “time image” of an executable file.
Like files, processes also belong to a separate hierarchical tree structure.
UNIX provides the tools that allow us to control process, move them between
foreground and background, and even kill them.
A request for the operating system to do something on behalf of the user's program.
The system calls are functions used in the kernel itself.
Unix consisting of thousand commands in the system, they all use a handful of function,
called SYSTEM CALLS, to communicate with the kernel.
UNIX command writes a file with the write system call without going into the innards
that actually achieve the write operation.
The same system call can access both a file and a device; the open system call opens
both.
These system calls are built into the kernel and interaction through them represents an
efficient means of communication with the system.
2. Features of Unix
The following sections present the major features of this operating system.
1. UNIX: A Multiuser system:
UNIX is a multiprogramming system, it permits multiple programs to run and
compete for the attention of the CPU.
This can happen in two ways:
• Multiple users can run separate jobs.
• A single user can also run multiple jobs.
5. Pattern Matching
Suppose if we want to list the files which starts with the filename chap then
by using only ls we can’t achieve that. So UNIX provides us options of using
a special character know as a metacharacter.
Example:
ls chap*
chap01
chap02
chap03
The * is a special character used by the system to indicate that it can match a
number of filenames.
UNIX also supports regular expressions, which are framed with the characters
from the metacharacters set.
So, UNIX supports pattern matching concept also.
6. Programming Facility
The UNIX shell is also a programming language.
UNIX shell contains all the features, like control structure, loops and
variables, which makes UNIX as a powerful programming language.
7. Documentation
To know about UNIX in detail there exist many online help facilities like man
command. It is the most important reference for commands and their
configuration files.
The FAQ(Frequently asked Questions)- a document that addresses common
problems- is also widely available on the Net.
Personal Environment:
Although originally designed as a multiuser environment, many users are installing UNIX
on their personal computers.
This trend to personal UNIX system accelerated in the mid -1990’s with the availability of
Linux, a free UNIX system.
The Apple System X, released in 2001, incorporated UNIX as its kernel.
Time-sharing Environment:
Employees in large companies often work in what is known as a time-sharing environment.
In a time-sharing environment, all of the computing must be done by the central computer.
In other words, the central computer has many duties:
It must control the shared resources
It must manage the shared data and printing, and
It must also do the computing.
Client/Server Environment:
UNIX consists of four major components: the kernel, the shell, a standard set of utilities, and
application programs.
The Kernel:
The Shell:
The shell is the part of UNIX that is most visible to the user.
It receives and interprets the commands entered by the user.
There are two major parts of a shell.
The first part is the interpreter.
The interpreter reads your commands and works with the kernel to execute them.
The second part of the shell is a programming capability that allows you to write
a shell(command)script.
A shell script is a file that contains shell commands that perform a useful function.
It is also known as a shell program.
There are three standard shells used in UNIX today.
The Bourne shell, developed by Steve Bourne at the AT&T Labs, is the oldest.
Because it is the oldest and the most primitive, it is not used on many system today.
An enhanced version of the Bourne shell, called Bash, is used in Linux.
The C shell, developed in Berkeley by Bill Joy, A compatible version of the C shell,
tcsh, is used in Linux.
The Korn shell, developed by David Korn, also of the AT&T Labs, is the newest
and most powerful.
Utilities
A utility is a standard UNIX program that provides a support process for users.
Dept., of CS&E(AI & ML), RLJIT Page 6 Dr. Manjunatha B N
Module-1 Unix System Programming
Three common utilities are text editors, search programs, and sort programs.
Applications:
Exceptions
Some commands in UNIX like pwd do not take any options and arguments.
Some commands like who may or may not be specified with arguments.
The ls command can run without arguments (ls), with only options (ls –l), with only
filenames (ls f1 f2), or using a combination of both (ls –l f1 f2).
Some commands compulsorily take options (cut).
Some commands like grep, sed can take an expression as an argument, or a set of
instructions as argument.
1. Combining commands
Unix system allows you to specify more than one command in the command line. Each
command has to be separated from the other by a ; (semicolon)
Example:
wc - print newline, word, and byte counts for each file
You need not have to wait for the previous command to finish before you can enter the
next command.
Subsequent commands entered at the keyboard are stored in a buffer (a temporary storage
in memory) that is maintained by the kernel for all keyboard input.
The next command will be passed on to the shell for interpretation after the previous
command has completed its execution.
\a Bell
\b Backspace
\f Formfeed
\n Newline
\r Carriage return
\t Tab
\v Vertical tab
\\ Backslash
The command in its simplest form can be used in the same way as echo:
No filename entered
$ __
printf can do everything that echo does, some of its format strings can convert data from
one form to another.
Here’s how the number 255 is interpreted in octal(base 8) and hexadecimal(base 16)
ls : Listing Files
You can list the names of the files available in the directory with the ls command.
Example:
$ ls
chap01
chap02
Chap03
progs
When invoked by an ordinary user, passwd asks for the old password, and then it demands
the new password twice.
If everything goes smoothly, the new password is registered by the system, and the prompt
is returned.
Depending on the way they are configured, many systems conduct certain checks on the
string that you enter as password.
They may either disallow you from framing easy-to-remember passwords or advise you
against choosing a bad password.
These are good practice when handling your own password:
Don’t choose a password similar to your old one.
Don’t use commonly used names.
Use a mix of alphabetic or numeric characters.
Make sure the password is meaningful enough to prevent other from guessing it.
Don’t write down the password in an easily accessible document.
Change the password regularly.
When you enter a password, the string is encrypted by the system.
Encryption generates a string of seemingly random characters that UNIX uses subsequently
to determine the authenticity of a password.
This encryption is stored in a file named shadow in the /etc directory.
Locating Commands
All UNIX commands are single words like ls, cat, who, etc. these names are all in
lowercase.
•You can find the location of an executable program using type command:
Example:-
$ type ls
ls is /bin/ls
•ls command is a file (or program) found in the directory /bin.
This means that when you execute ls command, the shell locates this file in /bin directory
and makes arrangements to execute it.
[root@localhost ~]$ LS
bash: LS: command not found
[root@localhost ~]$
$ su
Password:******** root’s password
# pwd
/home/ravi Prompt changes, but directory
doesn’t
Though the current directory doesn’t change, the # prompt indicates that ravi now
has powers of a super user.
A text file contains only printable characters, and you can view and edit them.
All C and Java program sources, shell and perl scripts are text files.
Every line of a text file is terminated with the newline character, also known as
linefeed(LF).
A binary file contains both printable and unprintable characters that cover the entire
ASCII range ( 0 to 255 ).
The object code and executables that you produce by compiling C programs are binary
files. Sound and video files are also binary files.
Directory File:
A directory contains no data, but keeps details of the files and subdirectories that it contains.
A directory file contains one entry for every file and subdirectory that it houses. If you have
20 files in a directory, three will be 20 entries in the directory.
Each entry has two components:
the filename
a unique identification number of the file or directory(called the inode number).
When you create or remove a file, the kernel automatically updates its corresponding
directory by adding or removing the entry (filename and inode number) associated with the
file.
Device File:
You’ll also be printing files, installing software from CD-ROMs or backing up files to tape.
All of these activities are performed by reading or writing the file representing the device.
Device filenames are found in a single directory structure, /dev.
The kernel identifies a device from its attributes and uses them to operate the device.
The file system in unix is a collection of all these related files (ordinary, directory
and device files) organized in a hierarchical (an inverted tree) structure.
$ echo $PATH
/bin:/usr/bin:/usr/local/bin:/usr/ccs/bin:/usr/local/java/bin: .
There are six directories in this colon-separated list. Consider the second on /usr/bin it’s a
hierarchy of three directory name . The first is ‘/’ is the top most directory it is also called
as root, second is ‘usr’ is below the root directory and third is ‘bin’ is below the usr.
When you issue a command, the shell searches this list in the sequence specified to locate
and execute it.
1. Absolute Pathnames
An absolute pathname specifies the full path from the root to the desired directory or file.
A pathname that begins from root.
The pathname usually begins with a slash / or ~
Example:
cat /home/kumar/login.sql
Note:
If the first character of a pathname is /,the file’s location must be determined with respect
to root(the first /).
When you have more than one / in a pathname, for each such /,you have to descend one
level in the file system. thus in the above example kumar is one level below home and
two levels below root
You can have two files with the same name,but in different directories;their pathnames
will also be different.thus, the file /home/kumar/progs/c2f.pl can coexist with the file
/home/kumar/safe/c2f.pl.
2. Relative Pathnames
A pathname that is “relative” to the location of current working directory.
Paths that don't start with a slash(/) are always relative to the current directory.
Note : Relative pathname does NOT begin with a slash.
Example
$ cd progs
$ cat login.sql
Here, both progs and login.sql are presumed to exist in the current directory
If progs also contains a directory scripts under it, you still won’t need an absolute
pathname to change to that directory:
Example
$ cd progs/scripts //progs is in current directory
mkdir:Making Directories
rmdir:Removing Directories
The rmdir (remove directory) command removes directories. A directory needs to be
empty before you can remove it. If it’s not, you need to remove the files first.
Also, you can’t remove a directory if it is your present working directory; you must first
change out of that directory.
You cannot remove a subdirectory unless you are placed in a directory which is
hierarchically above the one you have chosen to remove.
$ rmdir pis ---directory must be empty---
Like mkdir, rmdir can also delete more than one directory in one shot.
$ rmdir patch dbs doc --removes three directories --
You can also delete directory trees with one mkdir command
$ rmdir pis pis/progs pis/data -Shows error as pis directory is not empty-
Note that when you delete a directory and its subdirectories, a reverse logic has to be
applied.
Eg : $ rmdir pis/data pis/progs pis --- directory tree deleted----
17. The dot (.) and double dots (..) notations to represent
present and parent directories and their usage in relative path
names.
. . -> Represents parent directory
. -> Represents current directory
Example 1
$ pwd
/home/kumar/progs/data/text
$ cd . . // moves one level up
$ pwd
/home/kumar/progs/data
Example 2
$ pwd
/home/kumar/progs/data/text
$ cd . . / . . // moves two levels up
$ pwd
/home/kumar/progs
[Ctrl-d]
$_
When the command line is terminated with [Enter] , the prompt vanishes. cat now waits
to take input from the user. Finally press [ Ctrl-d ] to signify the end of input to the
system.
$ rm progs/chap01 progs/chap02
rm Options
Interactive Deletion (-i)
Like in cp, the –i option makes the command ask the user for confirmation before
removing each file:
$ rm -i chap01 chap02 chap03
rm: remove chap01 (yes/no) ? y
rm: remove chap02 (yes/no) ? n
rm: remove chap03 (yes/no) ? [Enter] //No reponse--- file not deleted
A y removes the file, any other response leaves the file undeleted.
mv: Renaming Files
The mv command renames (moves) files. It has two distinct functions:
1. It renames a file (or directory).
2. It moves a group of files to a different directory.
mv doesn’t create a copy of the file, it merely renames it. No addition space is consumed
on disk during renaming.
To rename the file chap01 to man01, you should use
$ mv chap01 man01
It can move group of files can be moved to a directory. The following command moves
three files to the progs directory.
$ mv chap01 chap02 chap03 progs
mv can also be used to rename a directory, for instance, pis to perdir:
$ mv pis perdir
Assignment Questions
1. Explain the architecture of UNIX operating system with a neat diagram.
OR
Describe the relationship between the Kernel, Shell and the User with the help of a
diagram.
2. Discuss the salient features of UNIX operating system.
OR
Describe briefly the major features of the UNIX operating system.
3. What are system calls? Give two examples of system calls in UNIX operating system.
4. Discuss the following: i) the UNIX Environment ii) UNIX Structure
5. Explain internal and external commands with example.
6. Briefly explain the general features of unix commands or command structure.
7. Explain the following command with examples:
i) echo ii) printf iii) ls iv) who v) date vi) passwd vii)
cal viii) su command
8. Briefly describe: i) type command or locating command ii) login prompt
9. Explain the different file types available in UNIX.
10. With the help of a neat diagram, explain the parent-child relationship with respect to
UNIX file system. OR Illustrate with a diagram, the typical UNIX file system and
explain different types of files supported in UNIX.
11. Explain the following commands with an example:
i) HOME ii) pwd iii) cd iv) mkdir v) rmdir
12. Explain absolute path name and relative path name with example.
13. Explain the following commands with example:
i) cat ii) mv iii) rm iv) cp v) wc vi) od