The Unix Architecture and Command
-
Upload
abhi-singh -
Category
Documents
-
view
391 -
download
1
Transcript of The Unix Architecture and Command
Lecture Notes UNIT 1 The UNIX Architecture and Command Usage
THE UNIX ARCHITECTURE AND COMMAND USAGE
1. Describe the salient features of UNIX operating system.
Features of UNIX:-
The features of the UNIX operating system are as listed below,
i. UNIX: A Multiuse 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.
The windows operating system is a single user system where the CPU,
memory and hard disk are all dedicated to a single user. Whereas in UNIX, the
resources are shared between all the users. So, UNIX is a multiuser system.
In multiuser system, the computer breaks up a unit of time into several
segments and each user is allotted a segment. So at any point in time, the
machine will be doing the job of a single user. The moment the allotted time
expires, the previous job, which was kept in pending or the job which is the
queue is taken up for the execution. This process goes on until the clock has
turned full-circle and the first user’s job is taken up once again.
ii. UNIX: A Multitasking System
As we know, a single user can also run multiple jobs continuously. As a user
we can edit a file, print another file, send the mail and browse at one point of a
time without leaving any of the application. The kernel is designed in such
way that, it handles a user’s multiple needs.
In multitasking system, a user can see only one job running in the foreground
and the rest of the jobs will be running in the background. We can switch
between the foreground and background jobs, suspend them or even terminate
them. So UNIX is a multitasking system.
Gangadharaiah S. Arun K. H., Dept. of ISE, AcIT 1
Lecture Notes UNIT 1 The UNIX Architecture and Command Usage
iii. The Building-Block Approach
In UNIX we have thousands of command; each of the command does a unique job.
But it also provides an option to the user to combine two commands together so that a
new job is achieved.
For an instance, we can list the contents of the directory by using ls command and
count the words, characters and lines in a file by using wc command. These two
commands can be combined together by using special characters know as filters. Ex: |
(pipe). When we combine ls and wc commands by using | (pipe) like ls|wc, the
outcome will be totally different from that off when they are executed individually. In
this case, the combined commands will give the count of number of files in your
directory.
To obtain the count of number of files in a directory, UNIX should a provided one
more command, if the facility of the filters were not there? Now by using a single
character like | (pipe), we can achieve it very easily. What | (pipe) does is, it takes the
output of the first command and gives it as an input for the second command. (ls|wc)
So, UNIX provides us to build our own command to achieve a special task by using
filters. So we can say UNIX supports building block approach.
iv. The UNIX Toolkit
As we know, in UNIX, the kernel is the one which looks after everything. But
the kernel by itself doesn’t do all that can benefit the user.
In order to exploit the power of UNIX, we need to use some host of
applications such as general purpose tools, text manipulation tools (filters),
compilers, networking tools, disk fragmentation tools and system
administration tools which comes with every UNIX system. So, UNIX
provides a toolkit for the user benefit.
v. Pattern Matching
As we know ls is the command which is used to list the files available in the
directory.
Example: $ ls
readme
chap01
chap02
chap03
Gangadharaiah S. Arun K. H., Dept. of ISE, AcIT 2
Lecture Notes UNIT 1 The UNIX Architecture and Command Usage
helpdir
Suppose if we want to list the files which stars 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.
vi. Programming Facility
The UNIX shell is also a programming language; it was designed for a
programmer. UNIX shell contains all the features, like control structure, loops
and variables, which makes UNIX as a powerful programming language. In
case of windows, if I have to execute C/C++ or java programs we need to
install additional software’s whereas in UNIX it is not necessary to do so, as
they come with UNIX operating system.
vii. 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.
Apart from the online documentation, through internet also we can download
information regarding UNIX. There are several newsgroups on UNIX, where
you can put your queries and get the information regarding that. The FAQ
(Frequently Asked Questions) is a document that addresses common problems
is also available in net. There are several articles published in magazines and
journals and lecture made available by universities on their web site.
2. Explain the architecture of UNIX operating system Gangadharaiah S. Arun K. H., Dept. of ISE, AcIT 3
Lecture Notes UNIT 1 The UNIX Architecture and Command Usage
The UNIX Architecture:-
The architecture of UNIX includes two main agencies, kernel and shell; these two handle all
the work of the system by themselves. The kernel interacts with machine hardware and the
shell with the user.
Kernel:-
Kernel is the core (heart or central part) of the operating system; it is a collection of
routines written in C language.
Kernel will be loaded into memory when the system is booted and communicates
directly with the hardware.
When the user application wants to access the hardware (hard disk or terminal), they
take the help of the kernel, which performs the job on the user behalf.
User applications access the kernel through set of functions called system calls
(System call is a request made by any application program to the kernel for
performing specific task).
Apart from providing the support to user application, the kernel also manages the
system’s memory, schedules processes, and decides their priority and many more.
Shell:-
In UNIX user always interact with kernel through commands. But computer does not
have inherent capability of translating those commands into action. So, it requires a
command interpreter, i.e., nothing but the shell. Shell is the interface between the user
and the kernel.
Gangadharaiah S. Arun K. H., Dept. of ISE, AcIT 4
Lecture Notes UNIT 1 The UNIX Architecture and Command Usage
In a System, there will be only one kernel running on the system, but there can be
many shells running, according to user choice.
When a user enters a command, the shell thoroughly examines the keyboard input for
special characters. If it finds any, it rebuilds a simplified command line and finally
communicates with the kernel to make sure that command is executed.
Example: - $ echo hello hi
hello hi
In the above example, the echo command has some input, which contains lots of
spaces between the arguments. While processing, the shell compresses all the multiple
spaces in the above command line to a single space. Later echo command runs with
spaces compressed and gives the output as hello hi.
File and process:-
The two major entities that are supported in the UNIX are the file and the process.
“Files have places and the processes have life”.
File:-
A file is an array of bytes or it is just a sequence of characters. File acts as container
for any information.
One file is part of another file by being a part of single hierarchical structure (file
system). So as a user, we can locate a file with reference to predefined place.
As a user, we can place a file at a specific location in the hierarchy and we can move a
file from one place to another.
UNIX considers directories and devices also as a file. The dominant file type is text
file and the behavior of the system is mainly controlled by the text file.
Process:-
Process is program under execution. The process is the name given to a file, when it is
executed as a program. A process is a “time image” of an executable file.
Like files, processes also belong to a separate hierarchical tree structure of process as
grand parents, parents, children, sliblings and grandchildren and they take birth and
die also.
The system call:-
System call is a set of routine exposed to get the kernel service.
Gangadharaiah S. Arun K. H., Dept. of ISE, AcIT 5
Lecture Notes UNIT 1 The UNIX Architecture and Command Usage
There will be finite number of system call implementation to any operating system.
All UNIX flavored operating system follow the POSIX standard.
UNIX operating system may support thousands of commands or applications. But
they all use or invoke finite system calls to communicate with the kernel.
In windows system, a ‘C’ programmer uses the standard library functions for
everything. As a windows user we can’t directly invoke write( ) system call, to add
some content into a file. Programmers are forced to use a library function like fprintf
for that purpose.
In contrast to above, a C programmer in UNIX system has complete access to the
entire system call routines as well as standard library functions.
3. What are internal and external commands in UNIX? Explain three of
them each with examples.
External Command
1. External command will have separate executable file in either /bin or /usr/bin
directory.
Example:
$type ls
/bin/ls
In the above example type command display the location of ls executable file. In this
case, it is stored in the /bin/ls directory.
2. Shell Environment variable PATH will store the path of the directory which contain
executable file.
3. If user invoke any external command, shell will search for invoked command
( related executable file) in PATH variable
4. Shell creates separate process for each external command invoked by user.
5. Complex operations (commands) are implemented as external command.
Internal Command
Gangadharaiah S. Arun K. H., Dept. of ISE, AcIT 6
Lecture Notes UNIT 1 The UNIX Architecture and Command Usage
1. Internal commands are built-in commands of shell. It will not have separate executable file
as in external command.
Example:
$type echo
echo is shell builtin
In the above command type command display echo as built-in command instead od
displaying the path of executable file as earlier.
2. Shell Environment variable PATH, will not have any path related to internal command.
3. If user invokes any internal command, shell will not search PATH variable.
4. Internal command will not create any new process. Shell process itself executes the
internal command.
5. Simple operations (commands) are implemented as internal command.
4. What is the difference between an argument and an option? Explain
with examples.
A command is a program that tells the UNIX system to do something. Command with its
arguments and options in known as command line. It has the form:
$command [options] [arguments]
Command indicates executable program.
[argument] indicates on what the command is to perform its action, usually a file
or series of files.
An [option] modifies the command, changing the way it performs.
Commands are case sensitive in UNIX. Example LS and ls are not the same.
Options are generally preceded by a hyphen (-), and for most commands, more than
one option can be strung together, in the form:
$command -[option][option][option]
Eg: $ ls -alR
Will perform a long list on all files in the current directory and recursively perform
the list through all sub-directories.
For most commands you can separate the options, preceding each with a hyphen,
$ command -option1 -option2 -option3
Eg: $ls -a -l -R
Gangadharaiah S. Arun K. H., Dept. of ISE, AcIT 7
Lecture Notes UNIT 1 The UNIX Architecture and Command Usage
Some commands have options that require file as parameters. Options requiring
parameters (files) are usually specified separately. Eg:
$wc –c test.c
1265 test.c
wc command will display character, word and line count of a specified file as
parameter. Here we have specified test.c as file parameter and –c as option. This will
display only character count of test.c
These are the standard conventions for commands. However, not all UNIX commands
will follow the standard. Some don't require the hyphen before options and some
won't let you group options together, i.e. they may require that each option be
preceded by a hyphen and separated by white space from other options and
arguments.
5. Write a note on man command.
A man page (short for manual page) is the software documentation for a computer program in
Unix or a Unix-like operating system. A user may invoke a man page by issuing the man
command. To read a manual page for a UNIX command, one can use
Syntax:
$man <Optinal_Section_Number> <command_name>
$man read // refer to shell read function in section 1
$man 3 read // refer to read system call in section 3
To read man output page wise, pager program is used. UNIX uses more pager by default.
Linux uses less pager by default. Since less commands are similar to vi editor command, it is
more powerful and popular.(f for forward, b for one page backward, /pattern to
search pattern)
Man has mainly 8 different sections. If man finds the documentation at first section itself,
then it will stop searching.
Section SVR4 Linux
1 User Programs User Programs
2 Kernel’s System calls Kernel’s System calls
Gangadharaiah S. Arun K. H., Dept. of ISE, AcIT 8
Lecture Notes UNIT 1 The UNIX Architecture and Command Usage
3 Library function Library function
4 Administrative file format Special files ( in /dev)
5 Miscellaneous Administrative file format
6 Games Games
7 Special files ( in /dev) Macro packages and conventions
8 Administration commands Administrative commands
All man pages follow a common layout that is optimized for presentation on a simple ASCII
text display, possibly without any form of highlighting or font control. Sections present may
include:
NAME
The name of the command or function, followed by a one-line description of what it does.
SYNOPSIS
In the case of a command, you get a formal description of how to run it and what command
line options it takes. For program functions, a list of the parameters the function takes and
which header file contains its definition. For experienced users, this may be all the
documentation they need. Any thing in [ ] is optional, otherwise it is compulsory. | is used
only when there is an only one option either on right or left side of the command.
DESCRIPTION
A textual description of the functioning of the command or function.
EXAMPLES
Some examples of common usage.
SEE ALSO
A list of related commands or functions.
Gangadharaiah S. Arun K. H., Dept. of ISE, AcIT 9
Lecture Notes UNIT 1 The UNIX Architecture and Command Usage
6. What is a file? Explain the different types of files available in UNIX OS?
Mention the rules to be followed to name a file.
File is a container for storing the information. All data in UNIX is organized into files. All
files are organized into directories. These directories are organized into a tree-like structure
called the file system.
In UNIX there are three basic types of files:
1. Ordinary Files: An ordinary file is a file on the system that contains data, text, or program
instructions.
2. Directories: Directories store both special and ordinary files. For users familiar with
Windows or Mac OS, UNIX directories are equivalent to folders.
3. Device Files/Special Files: All the devices and peripherals are represented by files.
Special files provide access to hardware such as hard drives, CD-ROM drives, modems, and
Ethernet adapters.
1. Ordinary File: An ordinary file or regular file is the most common type of file. Ordinary
files can be further divided into
a. Text File
A text file contains only printable characters.
We can view this file with simple text editor.
All the programs like c, c++, Perl belongs to this category.
Text in each file is end with new line character also called as Line Feed. Linefeed is
invisible to user. Only special command like od can display this character.
b. Binary File
Binary file contain both printable and non printable character.
It can contain entire range of ASCII ( 0 to 255 )
All object file, executable file. Audio, video, images belongs to binary file.
Normal text editor produces unreadable output if user try to display it using text editor
2. Directory File
Directory Files doesn’t contain any data of the files it holds.
Directory stores only certain details of each file/ folder it holds.Gangadharaiah S. Arun K. H., Dept. of ISE, AcIT 10
Lecture Notes UNIT 1 The UNIX Architecture and Command Usage
A Directory normally contain: Filename and Unique number (inode number)
associated which each file.
In UNIX, user can't directly update the directory file. Only kernel has write
permission to Directory.
Whenever users create/delete the file/Folder, respective file/folder name is added with
inode number by the kernel in the respective Directory.
3. Device Files/Special Files:
All device files are stored under /dev
Device file will have separate file attribute compare to ordinary file.
Operation and behavior of the device file depends on the attributes defined for the
file.
Most of the system call function which work on ordinary file will also work on device
file. E.g. read(), open(), close()
File Name
In Unix File name can have Maximum of 255 characters.
File name may contain any ASCII value except NULL and /
Control and non printable char is also allowed in filename
#@ @#acbe a.b.c.d are valid File Name
Prefer to use Only Alphanumeric char and special symbol like . (dot), - (hyphen), _
(underscore)
File name are case sensitive compare to DOS.
File name may/may not have extension. Even it can have more than one extension.
Following are valid file name in UNIX
test.c.txt test..c test test.c.java
Avoid using – (hyphen) at beginning. Because –Z file name will be considered as
option. This will force shell to display invalid option to the given command.
$cat –z
In the above example, Shell outputs as invalid option, even though you may have file
name with –z
Gangadharaiah S. Arun K. H., Dept. of ISE, AcIT 11
Lecture Notes UNIT 1 The UNIX Architecture and Command Usage
7. Explain Absolute Path Name and Relative Path Name with example.
Absolute Path Name
An absolute pathname is the location of a file system object relative to the root(/)
directory.
All absolute pathnames always begin with a slash (/). With Absolute pathname you
have access to complete file system objects such as directories and files.
Example: /usr/bin/man is the location of the man command.
In a system, two files can have same name, but can't have same absolute path
/home/ajay/Lab1.c
/home/suresh/Lab1.c
Even though above two files are having same name, they refer to two different file.
Commands can also be executed by absolute path name ($/bin/date). But to avoid
specifying lengthy path name, path for executable are included in PATH variable of
shell.
Relative pathname
A relative pathname is just a path that starts from your current directory, and thus,
doesn't start with a slash (/) character.
For example relative path is: homeworks/test1
which refers to a file(test1) in the subdirectory (homeworks) under your current
directory.
A single dot(.) and two dots(..) Can also be used for relative path.
. (a single dot) - Represents current Directory
.. (Two dots) - Parent Directory
Example:
$cd .. Changes to parent Directory
$cd ../.. Moves to two level up
$cp ../Sharma . Copy the content of sharma to current dir
Gangadharaiah S. Arun K. H., Dept. of ISE, AcIT 12
Lecture Notes UNIT 1 The UNIX Architecture and Command Usage
8. What is parent child Relationship? With the help of a diagram, explain the UNIX File system tree.
UNIX file System is organized as Hierarchical File System. The top most directory is root(/ ). Root (/) directory has all the subdirectories of the system under it. Every File or Folder except / should have parent directory in hierarchical file system.
In these parent-child relationships of UNIX file system, the parent is always a directory.
Super user will have full permission to access and modify any part of the file system.
For super user, /root directory will be the HOME directory.
Normal user will not have any permission to access root Directory.
Once user log on system, by default they are put under /home directory. Eg: If we have user with the name Akash, then once Akash log on to system, he will be put under /home/Akash.
UNIX File System Tree
Most vendors adopted the SVR4 structure of the UNIX file system. But it varies based on distribution. But following are the general directories under the root directory :
/bin and /Usr/bin Contain Unix commands binary.
Path variable always contain this Directory
/sbin and /usr/sbin Normal user can't execute this
Gangadharaiah S. Arun K. H., Dept. of ISE, AcIT 13
Lecture Notes UNIT 1 The UNIX Architecture and Command Usage
Only System has permission to Execute this. System admin PATH will display this
/etc Configuration files of the system.
/dev Contain device file. doesn’t Occupy Space on disk
/lib and /usr/lib Contain all library files in binary form
/usr/include Contain the standard header file
/usr/share/man This contains man pages. Each section of man pages are stored in separate directory.
/usr/share/man/man1 /usr/share/man/man2
/tmp Users are allowed to create temp files. These files are cleaned up regularly
/var Variable part of the file system. Contain print jobs, out going and incoming mail.
/home Each user will have separate directory.
Gangadharaiah S. Arun K. H., Dept. of ISE, AcIT 14