The UNIX Operating System

clashjudiciousElectronics - Devices

Nov 8, 2013 (4 years and 1 month ago)

92 views

1

1

1

TCS Confidential

The UNIX Operating System

2

2

2

TCS Confidential

UNIX Operating System

Objectives :


Learn about the Unix Operating System and develop the skills
required to build software applications in the Unix Environment.

3

3

3

TCS Confidential





1.

Introduction & History of UNIX

2.

File System

3.

Basic Utilities

4.

Shell Features

5.

Advanced Utilities

6.

Communication Features

7.

System Calls

Contents :

4

4

4

TCS Confidential






Chapter 1



Introduction and History of
UNIX





5

5

5

TCS Confidential


Introduction and History of UNIX

1.

Evolution

2.

Development

3.

Standard release
-

AT&T versions

4.

BSD UNIX

5.

Other implementations of UNIX

6.

Features of UNIX

7.

UNIX Philosophy

8.

UNIX operating system

9.

UNIX Kernel

10.

Programming Environment

11.

Design Principles

12.

Limitations

6

6

6

TCS Confidential




Primarily influenced by
MULTICS




MULTICS


-

Developed for
GENERAL ELECTRIC 645




mainframe computer


-


Interactive operating system


-


Batch
-

mode characteristics




UNIX Operating System


-


First Version developed in 1969 by




Ken Thompson on DEC PDP Series ( PDP
-

7 )


-


Business, Scientific and Industrial Users

Evolution

7

7

7

TCS Confidential


Development

*

Originally written in assembly language for
PDP
-
7


*

Transportability facilitated by Thompson who
developed the
B language


*

B language modified by Ritchie and renamed
as
C language

*

Thompson (1980) and others wrote UNIX in
C which could be ported to any computer



8

8

8

TCS Confidential

Standard release
-

AT&T Versions




Version
3



Written in C






Moved to PDP
-

11
/
45
and





PDP
-

11
/
70

Version
6



Released in
1975






For use by universities only

Version
7



Released in
1978






First commercial version but



primarly used in universities






32
V
-

VAX version

Version
8



UNIX Support Group(USG)



takes responsibility






Research Group still busy

9

9

9

TCS Confidential

Standard release
-

AT&T Versions …Contd...



System III



Released in
1981





Commercial orientation





Over
100
manufacturers of UNIX
-
based




micro , mini and

mainframes





UNIX users >
100
,
000

System V

Release
1



Released in
1983





Enhancement of System III

System V

Release
2



Released in
1984





Enhancements of System V , Release
1

System V

Release
3



Released in
1988


System V

Release
4.0




1
0

10

10

TCS Confidential

BSD UNIX

(from Berkeley Software Distributions)

* UNIX's entry into University of California, Berkeley Campus during
1976
-
77

* Inputs of UCB Faculty/researchers incorporated in System V of AT&T UNIX




UNIX
3
BSD



Released in
1978






Developed by Bill Joy and Ozalp Baboglu






Added Virtual memory, Demand paging





to the VAX version
32
V



UNIX
4
BSD



DARPA
-

funded Development






Provided support for DARPA TCP/IP






C
-
Shell , ex/vi , new compilers for PASCAL





and LISP were added


UNIX
4.1
BSD



Released in
1981



UNIX
4.2
BSD



Released in
1983



UNIX
4.3
BSD



Latest Release

1
1

11

11

TCS Confidential

Other Implementations of UNIX

(based on Version
7
, System III, System V of AT&T UNIX or UNIX
4.2
BSD)



Xenix




Microsoft’s UNIX operating system





to run on microcomputers


IBM PC/IX



IBM PC


Zeus



Zilog Computers


DEC Ultrix


DEC


HP
-
UX



HP


UNIX



SUN


Elxsi UNIX


ELXSI


Pyramid UNIX


Pyramid


Data General UNIX

Data General


Perkin Elmer UNIX

Perkin Elmer


MassComp UNIX


Mass Computers


NBI UNIX


NBI


Amdahl UNIX


Amdahl

1
2

12

12

TCS Confidential





Multitasking


Performing tasks simultaneously rather than



sequentially



e.g., While printing a document , start editing

another document



Each task is considered to be a process

Multi
-
user


Several users can use the same computer



simultaneously



i
.e , more than one keyboard and terminal




can be connected to one computer



Highly cost
-
effective


Portability

Easier to modify the UNIX system




code for installation on a new computer rather


than write from scratch a new operating system


UNIX Features

1
3

13

13

TCS Confidential



UNIX System supplied tools


Integral utilities


Basic Operation of Operating




system like Command




interpreter or Shell



Tools



Text print formatting like nroff




and typesetting troff

UNIX Features

1
4

14

14

TCS Confidential


*

Available on micros, minis and mainframes

*

UNIX Communication and electronic mail




Communication between different terminals

hooked to same computer




Communication between computers of different



sizes and types in different locations as far away



as other countries

*

Incorporation of third party application programs

*

Hierarchical File System

*

On
-
Line Help facility



Example : man <Command>



*

Library of application software


1
5

15

15

TCS Confidential


Make each program do one thing well


Expect the output of one program to

become the input to another


Suited for prototype development

i.e., design and build easily


Modular approach

i.e., be prepared to throw and rebuild


Usage of tools


Building essential tools

The UNIX Philosophy

1
6

16

16

TCS Confidential













Kernel


Schedules tasks and manages data storage




Shell


A program that




• interprets the user commands/requests




• calls programs from memory and





• executes them one at a time or in a series





(called a pipe)




Tools &


Specific capabilities added on to the OS


Applications


Kernel( Heart of Unix Operating System)

Shell Interpreter

Tools and Applications

The UNIX Operating System


1
7

17

17

TCS Confidential




The Users

Shells and Commands

Compilers and Interpreters

System Libraries

System Call Interface to Kernel


Signal



File System



CPU Scheduling




Terminal


Swapping


Page


Handling





Replacement




Character


Block


Demand Paging


I/O System


I/O System





Terminal


Disk & Tape


Virtual


Drivers


Drivers


Memory


Kernel Interface to the Hardware



Terminal


Device


Memory


Controller


Controller


Controller



Terminals


Disks & Tapes

Physical






Memory

1
8

18

18

TCS Confidential

File Management

And Security

Networking

Services

Date and Time

Services

Input / Output

Services

Signal

Handling

Process

Scheduling

System

Administration

and Accounting

Memory

Management

UNIX

SYSTEM

KERNEL

The UNIX Kernel

1
9

19

19

TCS Confidential


*

A GOOD operating system !!







Simple yet powerful




Allows building of tools, writing of small




programs and putting them together




A rich & productive environment



*

A PORTABLE operating system !!




Written in C




Runs on a range of computers




Source code is available

The Programming Environment

2
0

20

20

TCS Confidential




*

Designed for Programmers

*

Interactive

*

Multiple Processes can be initiated


Foreground Process


Background Process


*

Methods to Co
-
ordinate Process

*

Signal to communicate with processes

*

Files, Directories, Devices treated as files

*

Tree structured directories to hold files

*

Emphasis on program development facilities

*

Sources available on
-
line

*

Simplicity before efficiency

The Design Principles


2
1

21

21

TCS Confidential




*

Designed for computer professionals



*

Not a real
-
time operating system



*

File Security depends on owner



*

Defective programs may keep running

Drawbacks of UNIX

2
2

22

22

TCS Confidential

Chapter
2


File System

2
3

23

23

TCS Confidential











1
.

File System Layout

2
.

Concepts of file

3
.

Features of File system

4
.

Starting and Ending a session

5
.

File Management utilities

6
.

Directory Management

7
.

File operation

8
.

Mountable File System

9
.

File security




The UNIX File System

2
4

24

24

TCS Confidential



Disks are partitioned into File Systems.



-

Logical blocks of
512
bytes or multiples




-

Arranged to form a hierarchical structure



-

Convenient to manage data



File System Layout




Boot

Super


Inode


Data



block

block


list


block





Boot block




-

generally occupies the first sector




-

may contain bootstrap code


Super block




-

Information about the file system





How large, where free and other details


Inode list




-

Contains a list of inodes




-

One inode is the root inode


Data block




-

Contains file and administrative data




-

Allocated data can belong to that block





only



File Systems in UNIX

2
5

25

25

TCS Confidential




-

Ordinary Files



-

Directory Files



-

Special Files



-

Standard Files


Classification of files in UNIX

2
6

26

26

TCS Confidential


*

A File in UNIX is a Stream of bytes (
8
-
bits)



Kernel does not impose a structure on files



*

File may contain text, data and even machine



language code



Examples



Text Files

:

Lines of ASCII characters






separated by a new
-
line




Commands

:

Sequence of commands






interpreted by UNIX text





Data



:

File containing data as






stream of bytes




Executable

:

File containing machine







language instructions



*

The files format remains unchanged but only the

way




the program interprets it is different


Concept of Ordinary Files

2
7

27

27

TCS Confidential





*

Gathering together related files in a common place


*

Every file is assigned to a Directory


*

Directories have names
(maximum of
14
characters
)


*


A file within a Directory can itself be another Directory
(A Sub
-
directory)



*

Internally a Directory is just a file that contains



-

a list of file names






-

their Inode Numbers and



-

a pointer to the actual file on the disk


*

An upper limit of around
35
sub
-
directories



Examples




/ ( Forward slash )


Prime or Root Directory





Note
-

in DOS it is
\

( Backward slash )


/usr



Directory for the user



/usr/trg
1



Sub
-
Directory for trg
1
under Directory usr



Concept of Directory Files

2
8

28

28

TCS Confidential


* Used to reference Physical devices such as terminals, disks, floppy drives and
tape drives


* Read and written like ordinary files


* Requests cause activation of the associated Physical Device


* Device Drivers associated with each file


* Types :

Character file


Terminal (tty
0
, tty
1
)


Transmits data Character by


Character

Block file


Disk Drives (hd
0
,fd
0
)
Transmits data in
512
or
1024


byte chunks



* Major and Minor numbers identify the hardware link




Major No.s

: Indicates
type

of device



Minor No.s

: Different
instances

of the device


Concept of Special Files

2
9

29

29

TCS Confidential


*

Helps display information on the screen

*

Special Names for Communication channels

*

Keyboard input channel is called
Standard input


(stdin)
-

file id is
0

*

Terminal Screen output is called
Standard output


(stdout)
-

file id is
1


Diagnostic error messages (generated by a
program) are sent to
Standard error


(stderr)
-

file id is
2

(
shown on terminal screen

)

*


All three files are open by default at the time of
login

Concept of Standard Files

3
0

30

30

TCS Confidential

*

A means for the system to identify a file




Note : Unix is
case sensitive



*

User accesses a file by a user
-
assigned file


name



*

Can be a sequence of characters
(a



maximum of
14
)





*

May be identified by two or more names


(Multiple links to a file)




*

Internally assigned a unique
inode number


(for a File System)




*

Different files can be grouped under a


directory

File Names

3
1

31

31

TCS Confidential


Absolute Path name



*

A file is identified by the path name from the root


e.g.,




/usr/trg/c/test.c


where





test.c

is an
ordinary
file





usr, trg, c

are
directories





trg

is a
sub
-
directory

under
usr





Relative path name




*

UNIX keeps track of the user's current directory



*

If a

"/"

does not precede a file name then the name interpretation



begins with the current directory


e.g.,




If current directory is



/usr/trg




then the file could be just referenced as
c/test.c





Path Names

3
2

32

32

TCS Confidential



*

Hierarchical



*

Security on each file



-

Owner


-

Group


-

All others


*

Separate security for



-

read


-

write
and


-

execute


*

Removable


*

File Independence
-


*

Time stamp on each file



-

Modification time


-

Access time

Features

3
3

33

33

TCS Confidential

dev

bin

lib

spool

etc

tmp

console

lp
0

tty
0

sh

csh

---

---

Libc.a

group

init

---

passwd

/

spell

troff

---

man

---

local

uch

bin

src

lib

tmp

---

---

troff

tmac

---

lib

bin

*.h

include

usr

Unix

3
4

34

34

TCS Confidential


/bin


Basic Unix utilities


cp, mv, ln



/dev


Special I/O device files

fd
0
, lp
0



/etc

Administrative Programs

passwd, shutdown



/lib

Libraries used by Unix

libc.a



/usr/bin

Unix utilities


cal, bc,spell




/usr/adm



Administrative commands




and files



adduser




/tmp


Temporary files created



on error conditions


Standard File Hierarchy

3
5

35

35

TCS Confidential


/usr/games



-

Game Programs


/usr/include


-


Include files for language procedure





Examples : C
-
header files





stdio.h, math.h


/usr/lib


-


Archive libraries





Example :

troff


/usr/mail



-


Mail files





Example :

mailbox


/usr/news



-


News files



/usr/spool



-


Spool files



/usr/tmp



-


Temporary files


/usr/src



-

Program Source Files


3
6

36

36

TCS Confidential




*

Three levels of access


User/Owner, group, others

*

Three types of access on Files and Directories


Read, Write, Execute



Access Mode

Ordinary


Directory



Read


Examination of

Listing of




File Contents

files within







Directory



Write


Allows changing


Creating new




of file contents

files within







Directory



Execute


Executing file as

Searching the




a command

Directory

allowed



allowed


Security and Access Methods

3
7

37

37

TCS Confidential




login

:

User can type his name and password to identify himself






login

command can be used as



$ exec login

to log
-
on onto another user account after identifying yourself in
response to prompts for user name and password


su



setuser


This is used to become another user or
super
-
user

provided the
password is known.


e.g.,

$su


Prompt the user for the superuser password
$su
-

trg
2


Prompt the user for the password of user
trg
2
$su
-

trg
2
-
c "ls
-
l"


Temporarily changes to
trg
2

and executes the command
ls
-
l
and

comes back to the original user

Starting and Ending a Session

3
8

38

38

TCS Confidential

Starting And Ending A Session …Contd….


passwd

Change the password for the user


e.g.,
$ passwd


Prompt you for old password and new password



logout

This command exits or logs
-
out from the current user



and executes the file
.logout
before coming ou
t




e.g.,


$ logout




or




$ exit





or




$ <ctrl
-
d>



exits from the current login

3
9

39

39

TCS Confidential





Directory



Operation


File Comp.


Security

Management



cd




cmp



passwd


pwd




comm



crypt


mkdir








chown


rmdir







chgrp


mvdir







umask









chmod



File



File



Mountable Copy, Move

contents

compression

file


Remove & Time












cat


pack


mount


cp

ls


unpack


umount


ln

wc






mv

file






rm







touch

File Management Utilities

4
0

40

40

TCS Confidential

mkdir


creates a new directory


rm


removes a file


rmdir


removes a directory


du


displays disk usage


df


displays number of free block


touch


updates the time of last modification


find


locates files that match certain area


file


displays the type of file


pwd


displays full pathname of current directory

4
1

41

41

TCS Confidential

General Structure:


Command Name

[Options]


[Arguments]



E.g.,

Command Name

Tells the shell what to do

(in this case

list files
)

Options

Control how the command will work

(in this case
ask for a long listing
)

Arguments

on which the command works

(in this case the directory

usr/trg

)

ls

-
l

/usr/trg

Command Structure

4
2

42

42

TCS Confidential




Directory Management




cd


Change working Directory



cd..



Parent Directory



cd.



Current Directory


e.g.,


$ cd /usr/trg/c


( current Directory is
c

)


$ cd ..




( current Directory is
trg

)


$ cd ./c


( current Directory is again
c

)


or
$ cd c


$ cd



( home directory
-

in this case
/usr/trg)




mkdir




Make a Directory




$ mkdir pathname



Makes Directory in
777
mode



Write permission should at least be permitted for owner in parent Directory


e.g.,



$ mkdir /usr/trg
2


( makes directory
trg
2
)

4
3

43

43

TCS Confidential




rmdir


Remove a Directory






$ rmdir pathname





*

Directory should be empty
, or else


rm
-
r


(recursively remove)



e.g.,


$ rmdir /usr/trg
2


(removes directory

trg
2
)








pwd




Print Working Directory

4
4

44

44

TCS Confidential


cat


Concatenate & Print on screen or printer




$cat [Options] [Arguments]




Options

-


take input from stdin






-
n

no. of output lines




-
s

squeeze adj. blank lines




-
v

enable display of non
-
printing characters




-
b

used with
-
n

to avoid numbering blank lines





e.g.,



$ cat try.c

Display the contents of
try.c
on the screen



$ cat



Takes input from stdin i.e. keyboard and displays

on



screen



File Contents

4
5

45

45

TCS Confidential



$ cat f
1
> f
2



Takes input from file
f
1
& puts it


on file
f
2

$ cat f
2
> f
3



f
3

contains the contents of
f
1

$ cat f
4
>> f
3


Appends the contents of

f
4

to





file

f
3

$ cat try[
0
-
3
] > final

The file final contains contents




of

try
0
, try
1
, try
2
try
3

$ cat test* > report


The file report contains all files




beginning with

test

4
6

46

46

TCS Confidential


Is[Options]


List the Directory Contents


Options


-
1

number one single column output




-
l


long format (
II

also used)




-
a


all entries including dot files




-
s


gives no. of disk blocks




-
i


inode no.




-
t


ordered by modification time recent


first recursively display all directories,


starting specified or current directory

4
7

47

47

TCS Confidential


$ Is
-
I


List the files along with the protection bits and the user


$ Is
-
a

List the files starting with .and..also


$ Is
-
1

symtab.c



symtab.o



treegen



test


$ Is
-
I


-
rw
-
r

r


1
smj proj
1 30766
Mar
3 15
:
08
symtab.c



-
rw
-
r

r


1
smj proj
1 8759
Mar
3 15
:
12
symtab.o



-
rwxr
-
xr
-
x
4
smj proj
1 40743
Mar
3 15
:
23
treegen



drwxrwxr
-
x
1
smj proj
1 53
Mar
1 09
:
15
test


$ Is
-
a


.



..



.profile



.cshrc



symtab.c



...



$ Is
-
iI

10936
-
rw
-
r

r

I smj proj
1 3076
Mar
3 15
:
08
test.c



10936
-

inode number of file test.c

4
8

48

48

TCS Confidential




wc




Word Count





$wc [Options] filename


Options


-


Display no. of lines, words, characters




-
I

Display no. of lines




-
w

Display no. of words




-
c


Display no. of characters


e.g.,

$

wc test.c



20 200 5678



20
-

lines



200
-

words



5678
-

characters


nl




no. of lines in the file and temp’ly lists out the file





Similar to
wc
-
l < filename >

4
9

49

49

TCS Confidential


file



Determine file types




$file [Options] [Arguments]


Options


-
f

filelist




Normal File Types




-


C program text




-


assembler program text




-


commands text




-


ASCII text




-


English text




e.g.,

$ file test.c




C Program test

5
0

50

50

TCS Confidential




cp


copy a file




-
i

-

user interactive mode




e.g.,




$ cp test.c test.c.bak




test.c and test.c.bak contain the same contents




Extra disk storage





In


Create link





e.g.,





$ ln first.c second.c




The file is referenced by two different names




No Extra disk storage

5
1

51

51

TCS Confidential



mv


Moves or renames files and directories





-
i

interactive mode


e.g.,



$ mv old.c new.c



Renames the file

old.c
as
new.c


rm



Deletes the indicated file(s) files




rm

removes files and directories



-
i


remove interactively



-
f


forcible remove



-
r


remove recursively




• Dangerous




• used in conjunction with
-
i



touch

Updates access, modification or change times of a file




-
a


update access time



-
m


update modification time



-
c

prevents creating the file


e.g.,

$ touch f
1



* The current system date & time stamp is put on the file

f
1



* If
f
1

does not exist then it is created with
0
bytes




5
2

52

52

TCS Confidential






cmp



Compare two files




If files are same no output is sent to the terminal,
or else
The line
number and


the byte at which the first difference occurs is reported






-
s


Outputs nothing Registers return code











Return code




0

if files are identical



1

if files are different



2

on error



e.g.,



$ cmp test
1
test
2



test
1
and

test
2

differ in char
36
line
3



$ cmp
-
s test
1
test
2



$ echo $status




outputs
1

indicating that the files are different

File Comparison

5
3

53

53

TCS Confidential



diff


-

Reports more than one differences






$diff [Options] file
1
file
2




-
b


Ignores trailing blanks





-
e

Gives a list of
ed

commands so as to convert




file
1
into file
2
.



e.g.,




$ diff test
1
test
2



Outputs:


n
1
a n
3
,n
4




n
1
,n
2
d n
3




n
1
,n
1
c n
3
,n
4



where


* n
1
,n
2
, n
3
,n
4
are line numbers




* a ,d, c means
append
,

delete

,
change
respectively


5
4

54

54

TCS Confidential






comm




Display common lines



$comm
-
[
123
] f
1
f
2



Prints a three column output:




-

lines that occur only in f
1




-

lines that occur only in f
2




-

lines that occur in both



comm
-
12

-

prints lines common to the two files



comm
-
23

-

prints only lines in the first file but not in the





second



comm
-
123

-

prints nothing



e.g.,







$ comm test
1
test
2

Reports the common lines between





files
test
1
,
test
2
and reports the lines differing



$ comm
-
12
test
1
test
2

Prints line common to both


5
5

55

55

TCS Confidential







pack



Compress the file




$ pack <filename>



e.g.,

$ pack try




-

Creates a file
try.z

which is packed




-

Normally the executables are packed




-

The size is reduced by
25
-

40
%




unpack


Uncompress packed file



or



pcat



e.g.,

$ unpack try.z



or



$ pcat try.z







unpacks the file try.z

File Compression

5
6

56

56

TCS Confidential


mount



Associates a directory with a device



e.g.,


Mounting a floppy on the root file system



umount


Dissociates directory from the device



e.g.,



$ mount /dev/fd
096
/mnt

Mounts the floppy on






the directory /mnt



$ umount /mnt


Dissociates /mnt from






the floppy

Mountable File System

5
7

57

57

TCS Confidential


passwd



To change the password






chown



To change the ownership of the file





$ chown owner filename



e.g.,

$ chown trg
2
test.c



* Initially the owner is trg
1



* Only the owner or the superuser can




change the ownership of the file

File Security

5
8

58

58

TCS Confidential


chmod




change the permissions of the file






$ chmod who op permission <filelist>



who

a, u, g, o


all, user, group, others


op


+,
-
, =


+ add,
-

remove, = set


permission

r,w,x


r read, w write, x execute



e.g.,


$ chmod a=rw test.c




* users, group, others have

read





and
write

permissions



$ chmod u+r, g+w, o+x test.c




*

read

for users
write

for groups
execute

for others



$ chmod
777
test.c





* Sets
read
,
write
,
execute

Permissions


5
9

59

59

TCS Confidential




umask



Set file creation mode mask











$ umask nnn



(nnn set file creation mode)



umask

can also be set as a
shell variable




e.g.,

umask
022


-

Files normally created with
777
mode is assigned



755
permission



The

value of each digit is subtracted from the corresponding "digit"
specified by the system for the creation of a file.

6
0

60

60

TCS Confidential


tail

Displays the last lines of file


options :
-
n (n= no. of lines)


e.g.,

$ tail
-
30
test.c


Displays the last
30
lines of file
test. c


head

Displays the top lines of file


e.g.,

$ head
-
10
test.c
Displays the first
10
lines of
test.c


split

Splits the file into different files as

specified by the number of lines

e.g.,

$ split
-
20
test.c


Splits the file
test.c
in blocks of
20
lines and creates files
xaa
,
xab
,
xac




and so on, such that


xaa


has first
20
lines of
test.c


xab

has the next
20
lines of
test.c






...


The file test.c is unaffected


$ split
-
20
test.c try
Generates files as
tryaa

,
tryab

,
tryac


paste

Joins the two or more files horizontally


e.g.,
$ paste xaa xab



File
xaa

and
xab

are joined horizontally and output to the terminal

File Operation

6
1

61

61

TCS Confidential

Chapter
3


BASIC UTILITIES

6
2

62

62

TCS Confidential






1
.

Line Editor
Ed



2
.

Visual Editor
Vi







3
.

Debuggers



4
.

Profiling Tools



5
.

C
-
Beautification

Basic Utilities

6
3

63

63

TCS Confidential



Invoking ed:


$ ed filename


Prompt :




Ed has no formal prompt


P
-

Prompts with a “*”

This is a toggle function

Append:


a

-


Appends given text after current line

Write :


w

-

Writing to the file

Exiting ed :


q


-

Quit from ed



Example :



$ ed test.c



? test.c



P



*a






<
————
>




<
————
>




<
————
>



*w



*q

The edit buffer can be written to some other file also as

*w filename

The Line Editor
-

ed

6
4

64

64

TCS Confidential



abc


abc



a*bc


abc,

aabc,

aaa...bc



a.c


abc,

aac,

acc,

axc,

a$c



a.*c


abc,

axyc,

a$+pmc,

abcdefgc



a[b
2
m]c


abc,

a
2
c,

amc



a[
0
-
2
b
-
d]c


a
0
c,

a
1
c,

a
2
c,

abc,

acc,


adc



a[^
0
-
2
]c


a
3
c,

a
5
c,

axc, ....



a[xy]*c


ac,

axc,

axxc,

ayyyc



a
\
{
3
,
\
}


aaa,

aaaa
-


3
or more a’s



a
\
{
3
,
5
\
}


aaa,

aaaa,

aaaaa






between
3
&
5
a’s

Pattern Matching

6
5

65

65

TCS Confidential




Command


Explanation



Usage




p

print current line (s)



1
,
3
p




l

list current line(s)



2
,
4
l



(display invisible

characters like tabs etc.)




n

print line(s) with


number before it


1
,
5
n





a

append text after current line


4
a








i

insert text before current line


3
i






Adding and Replacing Text..

6
6

66

66

TCS Confidential




Command


Explanation


Examples



.



Pointer to current line


*
-

Previous line



.=



Gives line number













n



Current line is ‘n’


*
-

Current line



+n



Move up by n lines






-
n



Move down by n lines



















$



points to the last line















,



short for
1
,$








;



short for .,$



/.../



forward search for string





of characters enclosed between





slashes

*$=

Prints total
no. lines
but
does not
changecurrent
line



Traversing within a file

6
7

67

67

TCS Confidential







Command



Explanation



Usage

d




delete line(s) in text



2
,
5
d


c



change line(s) in text



2
,
4
c


m




move line(s) in text



2
,
3
m
5


t




copy line(s) in text



2
,
3
t
5


j




join lines in text




2
,
3
j


u




undo last command



Note

:

The above
commands

can be easily
remembered

by associating them with the


first characters

of their
action
.



Substitute Commands




(n,n)s/oldstr/newstr

-

replaces oldstr with newstr (n,n) range of lines
-

optional


s/oldstr/newstr/g

-

all oldstrs' on the current line are replaced with newstr


s/oldstr/newstr/p

-

the replacement is only effected in print but not executed


s/oldstr/newstr&/

-

newstr is inserted at every match


s/oldstr/%/


-

matched strings are replaced by the replacement string in




the most recent substitute command




Modifying Text

6
8

68

68

TCS Confidential





/^ pattern/

searches for patterns at





beginning of line


/pattern $/


searches for pattern at end of line


/pattern/




the pattern is searched forward




//



forward search




??


backward search

Finding Text

6
9

69

69

TCS Confidential




Command


Explanation






(n,n)g/findstr/commandlist

Executes given commandlist for





every occurrence of findstr


(n,n)G/findstr/


Inputs one command to execute for


every occurrence of findstr



(n,n)v/findstr/commandlist

Executes given commandlist for every


non
-
occurrence of findstr


(n,n)V/findstr/


Inputs one command to execute for


every non
-
occurrence of findstr




Command Execution

7
0

70

70

TCS Confidential




Command


Explanation






f



print current filename


w



write contents of buffer into file


w file
2


write contents of buffer to file
2


r



read current file after current line



r file
2



read contents of file
2
after current line



e file
2


edit file
2

> invoke ed on file
2


!shellcmd

Execute shell command by preceding with
‘!’


! ls

l
gives a listing of current directory


Special ed Commands

7
1

71

71

TCS Confidential




*

an improved version of
ed


*

less terse


*

provides display options like numbered


line


*

allows shorthand versions of commands


*

clear response for error messages


The ex editor

7
2

72

72

TCS Confidential


The editor



*

Powerful full screen editor



*

vi v/s ed, ex



*

Mostly single key stroke commands



*

Interface with ‘ex’



*

Macro facility



*

Ability to process text



Invoking

$ vi filename




e.g.,
$ vi pgm.c



Modes :




*

Command mode




*

Insert mode



*

From Insert mode Pressing




<ESC> remitts Command mode

The vi
-

Visual Editor

7
3

73

73

TCS Confidential



Types of commands




* vi
-
commands (invisible)



Command mode



Commands can be categorised as :







* Cursor movement




* Text manipulations





-

insert, delete, copy, change




* Marking/Selecting, Positioning





* Search



Objects of interest recognized by ‘vi’:




* characters




* words




* lines




* block

7
4

74

74

TCS Confidential





Format of commands


[count] command [operand]


Use “.” to repeat last command


Use “u” to undo last command





Cursor Movement


Line Oriented :




^ or (zero)

beginning of line



$

end of line



Character oriented



h


move left


l


move right


j


move down


k


move up

7
5

75

75

TCS Confidential



Format of commands (contd.)




Word Oriented :



e


move to end of next word



w


move to beginning of word



b


move to beginning of previous word



E


move to end of next word




ignoring punctuation



W


move to beginning of word ignoring





punctuation



B


move to previous word ignoring




punctuation


7
6

76

76

TCS Confidential



Block Oriented :



)

move forward one sentence



(

move backward one sentence



}

move forward one para



{

move backward one para



%

move to find matching parenthesis



Rightly used in C
-

Programming for matching ‘(‘ & ‘)’



m

mark a particular line with a label.



e.g.,
ma
(marks the line with label

a
)





return cursor to position (
m
)



e.g., "
a

moves the cursor to the line




which was marked with label
a



^g

file information, line number



7
7

77

77

TCS Confidential





Text Manipulation






Insert mode :


a


append after cursor


A


at end of line


i


insert at cursor


I


at beginning of line


o


enter in new line after current line


O


enter in before current line


esc


to exit insert mode



Delete mode :


u


undo last command


nx


delete n
-

next character


ndw


deleting n next words


ndd


deleting n lines
-





current line and n
-
1
below current






line are deleted.



D


delete till end of line

7
8

78

78

TCS Confidential






Changing Text :



nr



replace char


s



delete current char, enter insert mode


S



delete line, enter insert mode


cw



change word


Cut & Paste :



nyy



yank n line





Named Buffers : a to z (
26
)





Unnamed Buffers :
1



p



print buffer


Join :



J



join next line to current EOL



7
9

79

79

TCS Confidential





Search



Searching for a character :



fc

search forward for character ‘c’



tc

similar to f, cursor placed
1
char left of ‘c’



Fc

search backward for character ‘c’



Tc

similar to F, cursor place
1
char right of ‘c’



;

continue search in same direction





continue search in reverse direction


8
0

80

80

TCS Confidential





Search



File Related :



G


go to the end line



nG


go to the nth line



M


go to the middle line



H


go to the top line



L


go to the last line



8
1

81

81

TCS Confidential















Text Manipulation



Screen Adjustments :



^d


Scroll down half a screen



^u


Scroll up half a screen



^f


Page forward




^b


Page backward




^e


exposes one more line at bottom of






screen




^y


exposes one more line at the top of






screen



8
2

82

82

TCS Confidential












Text Manipulation



Searching for a string :





/string



for searching forward



?string



for searching backward



Use n to continue search in the same direction


Use N to continue search in the opposite direction



s/oldstr/newstr


commands can be used in vi






8
3

83

83

TCS Confidential





Common Set commands




set ai



Causes automatic indentation



set noai



Nullifies the effect of auto
-
indent



set nu



Causes line numbers to be displayed



set wrapmargin = n Sets n column right margin



set bf



The beautify option Removes all





unimportant control characters



set



Causes a displays of current set options

8
4

84

84

TCS Confidential














Text Manipulation






:w!


file force write



:q


quit normally



:q!


quit, no write



:n


next file, for $ vi f
1
f
2
f
3
f
4



:n!


edit next file, discarding changes




to current file





:n args


specify new argument list




8
5

85

85

TCS Confidential



Why ?


*

for running a Program under the control of the programmer

*

for examining the values of various variables and stack contents


Types


fsdb



File System Debugger



Fixes damaged file system









kdb


Kernel Debugger



Resides in kernel,



Allows memory examination



Disassembles instructions



Executes programs


sdb


-

Debugging source code written in C, Fortran
77



-

Available on System V




Role

The functionality of any debugger is essentially the same

Debuggers :

8
6

86

86

TCS Confidential








Debugger
-

sdb





*

Invoked as




sdb <executable file>




e.g.,

$ cc
-
g test.c



$ sdb a.out



*

sdb

checks for the status of files



*

sdb
gives warning messages if source files



are not present or source files were



modified after the object files



*

sdb

gives a prompt * after invocation



*

some commands can be used to avail the



important features of
sdb






8
7

87

87

TCS Confidential











SDB Commands



Command


Explanation



*
r <arguments>



run the program




(arguments optional )

*
s



stepping through the instructions

*
20
b



setting a break point at line number
20


(Normally break points are set at function calls)




*
/pattern



searches for the pattern

*
<function
-
name> b

setting the break point at the function name

\
*
B



gives a list of break points
\

*
d <line
-
no>



deletes the break point at the specified line no.

*D



deletes all the break points




8
8

88

88

TCS Confidential


sdb Commands (contd.)




Command


Explanation


*
S



stepping over the function call

*
c



continue upto the break point

*
variable name


prints the value of variable active at that




time

*
variable name/x

prints the value of variable in hex

*
variable name/c


prints the value of variable in character

*
variable name/o

prints the value of variable in octal

*
variable name/s


prints the value of variable in null





terminated string



*variable name/a

character strings at the specified address

*
function:variable

prints the value of the variable in the





specified function

8
9

89

89

TCS Confidential

sdb Commands (contd.)



Command


Explanation


*
p



print the current line of the source

*
<line
-
no>p



print the corresponding line
-
no of the source

*
<ctr
-
d>


prints next
10
lines of source or data or instructions



depending on the previous command

*
w



prints a window of
10
lines of source or data or



instructions around the current line

*
<Enter
-
key>



executes the previous command

*
!<command>


executes the shell command

*
k



kills the debugger

*q



quits the debugger

9
0

90

90

TCS Confidential

CTRACE
-

Debugger


Different from the conventional debuggers like
sdb


Works in the absence of a debugger


Ctrace

simulates the insertion of
printf

statements


Programmer’s way of debugging is to insert
printf

statements at the required places


Results in more
printf

statements than the program itself


Ctrace

also traces infinite loops


9
1

91

91

TCS Confidential













CTRACE
-

Debugger
(Contd .)


*

Invoked as



ctrace [options] < source
-
file > traced
-
file



options :



-
o


print the variable in octal



-
x


print the variable in hex




-
u

print the variable in unsigned format




-
e

print the variable in floating point format


-
f <function
-
names>

trace only the selected function names


-
v <function
-
names>


complement of option
-
f

trace all functions except




those specified in the list



9
2

92

92

TCS Confidential






e.g.,





$ ctrace < test.c > ctest.c




test.c

-


source file



ctest.c

-


traced file


Compile the traced file as




$ cc ctest.c


creates in a default file

a.out


Run the executable



$ a.out <arguments>


arguments
are optional


Outputs the values of variables and statements as desired by the
user



9
3

93

93

TCS Confidential



PROF and GPROF


*


Improves efficiency and debugging


*


gprof

is available in BSD versions


*


The program should have been compiled with
-
p

option


for
prof
and
-
g

option for
gprof


*


prof

-

refers default input file
mon.out


*


gprof

-

refers default input file
gmon.out


*


Gives an idea as to which function used up the maximum


time

PROFILING TOOLS :

9
4

94

94

TCS Confidential





Example of

prof




$ cc
-
p test.c



$ a.out



$ prof a.out



Generates an output like....


%time


cumsecs


seconds #calls msec/call


name

91.2


9.34

9.34


20100



_func
2

7.2


10.08

0.74


1



acnt

1.0


10.18

0.10


100


_func
1

0.0


10.24

0.06


1



_main



%time



Percentage of time consumed by the function


cumsecs

Running sum or cumulative sum of the functions called


seconds



Seconds consumed by the function


calls



Number of times the function was invoked


name



Function name


9
5

95

95

TCS Confidential



* Beautifies the C
-
program code


* Indents all the statements


* Improves readability


* Invoked as




$ cb < source
-
file > target
-
file



e.g.,



$ cb < test.c > test
1
.c



test.c

-

source file



test
1
.c

-

Beautified version of
test.c

CB
-

C Beautification

9
6

96

96

TCS Confidential




Chapter
4


Shell Features







9
7

97

97

TCS Confidential


Shell Features




1
.

Various kind of shells


2
.

Shell commands


3
.

Shell Startup Dot files


4
.

Shell variables


5
.

C Shell


6
.

Test conditions


7
.

Shell programming
-

Bourne Shell


8
.

Shell programming
-

C Shell

Shell Features

9
8

98

98

TCS Confidential





1
.


Bourne Shell

sh


2
.


C Shell

csh


3
.


Korn Shell

ksh


4
.


Restricted Shell

rsh


5
.


Visual Shell

vsh

Various types of Shells :

9
9

99

99

TCS Confidential


Simple commands using shell Metacharacters


Exit status
-


return value
0
-

Successful


-


return value >
0
-

Failure


Redirection

Default input from Keyboard



Default output on the Screen


<


-

Read from a file


>


-

Redirect it to a file


>>


-

Append to a file




n>&m


-

Merge output from file descriptor
n

with file





descriptor
m




n<&m


-

Merge input from file descriptor
n
with file





descriptor
m




where n and m are file descriptors for the




stdin, stdout and stderr files


Pipeline


sequence of commands separated by
|

Shell Commands

1
0
0

100

100

TCS Confidential



;



sequential execution


&



asynchronous execution


&&



following list executed if preceding pipeline returns



zero value


||


following list executed if preceding pipeline returns



nonzero value


*



matches
0
or more characters


?


matches any single character in filenames


$var


value of shell variable var


var=variable

assign variable to var



1
0
1

101

101

TCS Confidential




#



rest of the line is a comment


‘...‘


run command within backward quote


‘...’


treats ... literally



“...”


treats ... literally except for $ and ‘...‘ and
\


e.g.,


$ (sleep
5
; echo “The date is `date` “ ) & date


The Output of the above command

will be


6345


Wed Sep
23 10
:
20
:
45
EDT
1991

$The date is Wed Sep
23 10
:
20
:
50
EDT
1991



1
0
2

102

102

TCS Confidential

PATH




*

If full path is given while executing the command, the command

is

executed from the path specified


*

Else it is searched in the Shell variable