Upload
dodien
View
216
Download
3
Embed Size (px)
Citation preview
UNIT-1
The Unix Operating System
Date : 18.01.2014
Prepared by : Thanu KurianDepartment :Computer Science
Engineered for Tomorrow
Course code: 10CS44
Engineered for Tomorrow
What is UNIX?• An Operating System (OS) • Mostly coded in C• Machine independence• It provides a number of facilities:
– management of hardware resources– directory and file system– loading / execution / suspension of programs
Engineered for Tomorrow
History (Brief)
• 1969 – First UNIX at Bell Labs– The MULTICS – Kernighan, Ritchie,
Thompson• 1970’s
– Bell Labs makes UNIX freeware
– Berkeley UNIX (BSD)– Bill Joy vi editor, C
Shell
• 1980’s– System V release 4– TCP/IP– Sun Microsystems
Solaris– Microsoft Xenix, SCO – MIT X-Windows
• 1990’s– GNU, LINUX– Stallman, Torvalds
Engineered for Tomorrow
Why Use UNIX?
• multi-tasking / multi-user• lots of software• networking capability• graphical (with command line)• easy to program • portable (PCs, mainframes,
super-computers)
continued
Engineered for Tomorrow
The UNIX architecture and Command UsageThe UNIX Architecture
Kernel and Shell:
Kernel:
• is the core of the OS
• kernel is loaded into memory when the system is booted and communicates directly with the hardware.
• user programs access the kernel thru’ a set of functions , called system calls.
• kernel also manages system’s memory, schedules processes, decides their priorities and performs other tasks.
Engineered for Tomorrow
Kernel – shell relationshipContd….
Hardware
Kernel
shellshell
shell
cp
grep
tar
ps
who sed
sort
ls
Other s/w
X window
Spreadsheets
Databases
Text Processors Browsers
Other Compilers
cc
user user user
Engineered for Tomorrow
Shell :
•Is the outer part of OS
• is the interface between user and kernel
• there is only one kernel running on the system, but there could be several shells in action – one for each user logged in.
• acts like command interpreter- it accepts commands issued by the user, interprets these commands, and executes the appropriate programs.
• Linux uses the Bash shell by default
Contd….
Engineered for Tomorrow
File and Process:
File:
• Is an array of bytes
• Hierarchical file system
• Directories and devices are members of file system
• Dominant file type is text
Engineered for Tomorrow
Process: • is the job (program) in execution.
• form a hierarchical tree structure
• UNIX provides tools to control processes, move them between foreground and background and kill them.
System Calls:• are the routines (functions) defined in the kernel
• user to communicate with the kernel thru’ system calls
• system calls are described in POSIX (Portable Operating System Interface) specification
• All UNIX systems use the same system calls.
e.g. : write, open
Engineered for Tomorrow
Features of UNIX:
1. A Multi-user system: Multiple users working on a single system. The resources (CPU,Memory and hard disk) are actually
shared between all users.
2. A Multiprogramming system: Permits multiple programs to run this can happen in 2 ways:
i) multiple users can run separate jobs.
ii) a single user can also run multiple jobs.
Engineered for Tomorrow
3. A Multitasking system: a single user runs multiple tasks concurrently. in this environment, one job is running in the
foreground , the rest run in the background.
4. The Building-Block Approach: “small is beautiful” is implemented thru’ pipes and
filters. by interconnecting a number of tools , we can have a
large number of combinations of their usage.
Engineered for Tomorrow
5. The UNIX Toolkit: There are general-purpose tools, text manipulation utilities
(filters), compilers and interpreters , networked applications, system administration tools and shells.
6. Pattern Matching: The * (called as metacharacter) is a special character used
by the system to indicate that it can match a number of files.
complex pattern matching can be done with the help of regular expressions.
Engineered for Tomorrow
7. Programming Facility:
The UNIX shell is a programming language. it has control structures, loops and variables. shell scripts are the programs that can invoke the UNIX commands. many of the system’s functions can be controlled and automated by using the
shell scripts
8. Documentation: The principal online help facility is the man command it is the reference for commands and their configuration files. there are several newsgroups on UNIX available on the Internet. The FAQ (Frequently Asked Questions) – a document that addresses common
problems is also available on the Internet. articles in magazines and journals, lecture notes by universities.
Engineered for Tomorrow
Understanding the UNIX CommandLocating Commands: UNIX is command-based system. the commands are essentially files containing programs , written in C. the commands used by all users are located in /bin and /usr/bin. to know the location of an executable program , use the type
command.
e.g.: $ type ls
ls is /bin/ls when we execute ls command , the shell locates this file in the /bin
directory and makes arrangements to execute it.
Engineered for Tomorrow
Contd….
The PATH variable:
The shell variable, PATH, specifies the search list of directories for locating commands.
The shell searches through these directories in order whenever it needs to find a command.
this variable can be evaluated using echo command as follows:
$ echo $PATH
/bin:/usr/bin:/usr/local/bin:/usr/ccs/bin:/usr/local/java/bin:.
Root directory
Engineered for Tomorrow
Environment variable:
A variable that specifies how an operating system or another program runs, or the devices that the operating system recognizes.
set of dynamic values that can affect the way running processes will behave on a computer.
A string consisting of environment information, such as a drive, path, or filename, associated with a symbolic name that can be used by MS-DOS and Windows.
Engineered for Tomorrow
Internal and External Commands
An Internal command is built into the shell, and is normally executed even if there's an external command of the same name.
e.g.: echo command
$ type echo
echo is a shell built-in
when echo is typed, the shell won’t look in its PATH to locate it (even if it’s there in /bin) , rather executes it from its own set of built-in commands.
Engineered for Tomorrow
External Commands exist on disk as separate files.
e.g.: ls command, exists in the /bin directory (or /usr/bin).
Command Structure: A command is used with options and arguments. commands and arguments must be separated by any number of
spaces or tabs (whitespace).
1. Options:
1) ls –l note
-l is an argument to ls , known as option. An option is normally preceded by a minus sign (-).
Engineered for Tomorrow
2) ls –z note
ls: illegal option – z //message from ls command
3) $ ls-l
bash:ls-l: command not found
4) ls –lat is same as ls –l –a –t to obtain the same output
Engineered for Tomorrow
2. Filename Arguments
1) tput clear // clear is an argument to tput
• cat list
many UNIX commands use a filename as argument, so the command can take input from the file.
many commands work with multiple filenames as arguments.
e.g.:
1) ls –lat chap01 chap02 chap03
2) cp chap01 chap02 progs // cp copies files
3) rm chap01 chap02 // rm removes files
Engineered for Tomorrow
The command with its arguments and options is known as the command line.
command line is complete only after user hits [Enter].
The complete line is then fed to the shell as its input for interpretation and execution.
Engineered for Tomorrow
Flexibility of Command usage: a command can often be entered in more than one way.
1. Combining commands: UNIX allows to specify more than one command in the command
line. each command has to be separated from the other by a ;
(semicolon)
e.g.: wc note ; ls –l note The combined output of two commands can be sent to the file
newlist as follows:
e.g.: (wc note ; ls –l note ) > newlist
metacharacter
Engineered for Tomorrow
2.A Command Line can be overflow or be split into multiple lines:
Terminal width is restricted to 80 characters.
The command simply overflows to the next line though it is still in a single logical line.
Sometimes it is necessary to split a long command line into multiple lines.
So the shell issues a secondary prompt , usually > , to indicate that command line is not complete.
Engineered for Tomorrow
Example
• $ echo “this is• >a three line // A second
prompt(>) appears• >text message”
• This is• a three line• text message
Engineered for Tomorrow
3. Entering a Command before previous command has finished:
Subsequent commands can be entered at the keyboard (as many commands as we wish) without waiting for the prompt.
the command that has been entered remains stored in a buffer, maintained by the kernel for all keyboard input.
Engineered for Tomorrow
man : Browsing the manual pages online UNIX offers an online help facility in the man command. Man displays the documentation- man documentation ,of every
command in the system e.g.: man wc //help on the wc command The entire man page for wc is displayed on the screen.
Navigation and Search: Two navigation commands working on all systems are:
• f or spacebar , advances the display by one screen of text at a time
• b, which moves back one screen.
Engineered for Tomorrow
Understanding a man page: A man page is divided into a number of compulsory and
optional sections. NAME, SYNOPSIS, DESCRIPTION are 3 common sections. NAME presents a one-line introduction to the command. SYNOPSIS shows the syntax- the options and arguments used
by the command. DESCRIPTION provides a detailed description.
Using man to understand man:
man man // Viewing man pages with man
Engineered for Tomorrow
Further Help with man –k, apropos and whatis
man –k: Searches a summary database and prints one-line description of the command.
Example:$ man –k awkawk awk(1) -pattern scanning and processing languagenawk nawk(1) -pattern scanning and processing language apropos: lists the commands and files associated with a keyword.
Engineered for Tomorrow
Example:$ apropos FTPftp ftp(1) -file transfer programftpd in.ftpd(1m) -file transfer protocol server ftpusers ftpusers(4) -file listing users to be disallowed
ftp login privilegeswhatis: lists one-liners for a command.
Example:$ whatis cpcp cp(1) -copy files
Engineered for Tomorrow
When Things Go Wrong
Engineered for Tomorrow
The File System
The File: Is a container for storing information. (may be a sequence of
characters)UNIX file doesn’t contain the eof (end-of-file) mark. A file’s size is not stored in the file , nor even its name. All file attributes are kept in a separate area of the hard
disk, which is accessible only to kernel.UNIX treats directories and devices as files as well.All physical devices like hard disk, memory, CD-ROM,
printer and modem are treated as files.
Engineered for Tomorrow
UNIX treats shells and kernel also as files. Files are divided into 3 categories:
i) Ordinary file:
also known as regular file.
contains only data as a stream of characters
ii) Directory file:
contains the file name and its associated inode number.
iii) Device file:
contains no data but the kernel uses the attributes of the file to operate the device.
all peripherals and devices are represented by files.
Engineered for Tomorrow
1. Ordinary (Regular) file:• Is the most common file type• ordinary file can be divided into 2 types: i) text file ii) binary filei) Text file:• contains only printable characters which can be viewed.• text file contains lines of characters where ever line is terminated with the newline
character. (linefeed or LF)e.gs: C and Java program files, shell and perl scriptsii) Binary File:• contain both printable and unprintable characters that cover the entire ASCII
range(0 to 255)e.gs: UNIX commands, object code and executables of C programs, Picture, sound and
video files
Engineered for Tomorrow
2. Directory FileA directory contains no data , but keeps some details of the files and
subdirectories that it contains.• The UNIX file system is organized with a number of directories and
subdirectories.• A directory contains the file name and not the file’s contents.• There will be an entry for each file in the directory.• Each entry has 2 components:
i) The filename
ii) A unique identification number for the file or directory, called the inode number.
• When any file is created or removed, the kernel automatically updates its corresponding directory by adding or removing the entry (inode number & filename) associated with that file.
Engineered for Tomorrow
3. Device File:• Device files are generally found inside a
single directory structure, /dev.
• The attributes of every file is stored on the disk .
• The kernel identifies a device from its attributes and then uses them to operate the device.
Engineered for Tomorrow
Filename:
In UNIX, the following characters can be used in filenames:
• Alphabetic characters and numerals
• The period(.), hyphen(-) and underscore(_)• A file can have as many dots embedded in its name. A
filename can also begin and end with a dot.
a.b.c.d.e is a valid filename.
• UNIX is case-sensitive.
chap01, Chap01 and CHAP01 are 3 different filenames.
Engineered for Tomorrow
Parent-Child Relationship:
The file system in UNIX is a collection of all the related files (ordinary, directory and device files) organized in a hierarchical structure.
• Directories and files have parent-child relationship.• The topmost directory is called root, represented by a frontslash(/).• The root directory(/) has a number of subdirectories under it. These
subdirectories have more subdirectories and files under them.• The home directory is the parent of kumar. / is the parent of home
and the grandparent of kumar.• login.sql is an ordinary file under kumar directory.• The visual representation is as shown :
Engineered for Tomorrow
38
root(/)
bin dev home lib stand tmp usr var
cat whodate
sharmakumar bin include sbin
login.sql progs
UNIX File System Tree:Contd..
Engineered for Tomorrow
The HOME Variable: The HOME Directory:• The home directory is created by the system when a user
account is opened.
e.g.: /home/kumar• The shell variable HOME tells the home directory.
$ echo $HOME
/home/kumar
Absolute Pathname:• shows a file’s location with reference to the top, i.e., root• is simply a sequence of directory names separated by slashes
e.g.: /home/kumar
Engineered for Tomorrow
pwd: Checking your current directory
The pwd (present working directory) command tells you the current directory
$ pwd
/home/kumar
Engineered for Tomorrow
cd: Changing the Current directory
The cd (change directory) command changes the current directory to the directory specified as argument.
$ pwd
/home/kumar
$ cd progs //change the subdirectory to progs under current directory
$ pwd
/home/kumar/progs
Engineered for Tomorrow
• If we want to switch to /bin directory ,we should use the absolute path name:
$ pwd
/home/kumar/progs
$ cd /bin
$ pwd
/bin
Engineered for Tomorrow
cd command can also be used without any arguments:
$ pwd
/home/kumar/progs
$ cd // cd used without arguments reverts to the home directory
$ pwd
/home/kumar
Engineered for Tomorrow
mkdir: Making Directories• Directories are created with the mkdir command. • The syntax of this command is:
mkdir name of the directory to be created
e.g.: a directory new is crated under the current directory as follows:
mkdir new• A number of subdirectories can be created with one
mkdir command as follows:
mkdir new dbs doc // 3 directories created
Engineered for Tomorrow
• UNIX system lets us create directory trees with just one invocation of the command.
e.g.: mkdir pis pis/progs pis/data //creates the directory tree• This creates 3 subdirectories –pis and 2 subdirectories under pis.
• We can’t create a subdirectory before the creation of its parent directory.
e.g.: $ mkdir pis/data pis/progs pis
mkdir: Failed to make directory “pis/data”; No such file or directory mkdir: Failed to make directory “pis/progs”; No such file or directory
Engineered for Tomorrow
Sometimes the system refuses to create a directory:
$ mkdir test
mkdir: Failed to make directory “test”; Permission denied
This may be due to following reasons:
• The directory test may already exist
• there may be an ordinary file by that name in the current directory
• If we try to create a directory in /bin , /etc or any other directory that houses the UNIX system files
Engineered for Tomorrow
rmdir: Removing Directories
• The rmdir(remove directory) command removes directories.
e.g.: rmdir doc //removes the directory doc
• rmdir can also delete more than one directory in one shot.
e.g:
rmdir pis/data pis/progs pis //directories are deleted in reverse order
Engineered for Tomorrow
48
Absolute Pathnames:
• shows a file’s location with reference to the top, i.e., root
• is simply a sequence of directory names separated by slashes e.g.: /home/kumar
• Suppose we are placed in /usr and want to access the file login.sql which is present in /home/kumar, we can give the pathname of the file as below:
cat /home/kumar/login.sql
Engineered for Tomorrow
• No two files in a UNIX system can have identical absolute pathnames .
• We can have 2 files with the same name , but in different directories; their pathnames will also be different.
Using the Absolute pathname for a command
• Since date command resides in /bin (or /usr/bin) , we can use the absolute pathname as follows:
$ /bin/dateMon Aug 25 09:40:45 IST 2008
Engineered for Tomorrow
• For any command that resides in the directories specified in the PATH variable, we need not use the absolute pathname.
• If we want to execute programs residing in some other directory that isn’t in PATH ,the absolute pathname needs to be specified.
e.g.: to execute the program less residing in /usr/local/bin , we need to give the absolute pathname:
/usr/local/bin/less
Engineered for Tomorrow
Relative Pathname:• A pathname which specifies the location of a file using
the symbols
. and ..
i.e. . (single dot) –represents the current directory
.. (two dots) –represents the parent directory
• Assume that we are in /home/kumar/progs/data/text , we can use .. as an argument to cd to move to the parent directory, /home/kumar/progs/data as shown below:
Engineered for Tomorrow
Using . and .. in relative path names
$ pwd/home/kumar/progs/data/text$ cd .. // moves one level up$ pwd/home/kumar/progs/data
• To move to /home , we can use the relative path name as follows:
$ pwd/home/kumar/pis$ cd ../.. // moves two levels up$ pwd/home
Engineered for Tomorrow
ls - list directory contents
• The command to list your directories and files is ls. • With options it can provide information about the size, type of file, permissions, dates of file creation,
change and access.Syntaxls [options] [argument]Common Options• When no argument is used, the listing will be of the current directory. There are many very useful options
for the ls command. A listing of many of them follows. When using the command, string the desired options together preceded by "-".
-a Lists all files, including those beginning with a dot (.).-d Lists only names of directories, not the files in the directory-F Indicates type of entry with a trailing symbol: executables with *, directories with / and symbolic links with
@-R Recursive list-u Sorts filenames by last access timet Sorts filenames by last modification time-i Displays inode number-l Long listing: lists the mode, link information, owner, size, last modification (time). If the file is a symbolic link,
an arrow (-->) precedes the pathname of the linked-to file.
Engineered for Tomorrow
• The mode field is given by the -l option and consists of 10 characters. The first character is one of the following:
CHARACTER IF ENTRY IS Ad directory- plain fileb block-type special filec character-type special filel symbolic links socket
• The next 9 characters are in 3 sets of 3 characters each. They indicate the file access permissions: the first 3 characters refer to the permissions for the user, the next three for the users in the Unix group assigned to the file, and the last 3 to the permissions for other users on the system.
Designations are as follows:• r read permission• w write permission• x execute permission• - no permission
Engineered for Tomorrow
The UNIX File System:
Files available during system installation:
/bin and /usr/bin –are the directories where all the commonly used UNIX commands are found.
/sbin and /usr/sbin – User will not be able to execute most command in these directories, only the system administrator can.
/etc – directory contains the configuration files of the system. User’s login name and password are stored in this directory.
/dev – this contains all device files. These files don’t occupy space on disk.
/lib and /usr/lib- contains all library files in binary form. C programs are linked with files in these directories.
Engineered for Tomorrow
usr/include - contains the standard header files used by C programs.
/usr/share/man - man pages are stored here. There are separate subdirectories here that contain the pages for each section.
The second group of files are as follows: /tmp – the directories where users are allowed to create
temporary fiels. /var – the variable part of the file system. Contains all print
jobs and outgoing and incoming mails. /home – on many systems users are housed here