UNIX SYSTEM PR OGR AMMING Sub ject Code:10CS62
I.A. Mark s : 25
Hours/Week : 04
Exam Hours: 03
Total Hours : 52
Exam Mark s: 100
PAR T – A UNIT – 1
6 Hour ours U NIX
Introduction:
and
ANSI
Stan a ndards:
The
ANSI
C
Stan andard,
The
ANSI/ISO C++ Standar ds, Dif f er ence between A NSI C a nd C++, T he POSIX
Standar ds, The POSIX.1 FIPS Standard, The X/Open Standard s. U NIX
and
POSIX
APIs :
T he
POSIX
APIs,
T he
U NIX
an d
P OS I X
Development Envir onment, API Common Character istics. UNIT – 2
6 H o u rs
m, The UNIX and POSIX UNIX Files: File T y pes, The U NIX and POSIX File S yste m, File Attr i butes, Inodes in U NIX System V, Application Progra m Inter f ace to Fi Files, U NIX Kernel Sup port for Files, Re lationship of C Stream Pointer s and File
Descr i ptor s, Dir ector y Files, Har d and Symboli c Link s.
UNIT – 3
7 Hours
UNIX File APIs: Gener al File APIs, File and Record Lock ing, Di Directory File APIs,
Device File APIs PIs, FIFO File APIs, Symbolic Link File APIs, Gener al File Class,
regf ile Class for Re Regular F iles, dir f ile Class for Di Directory Files, FI FO File Class, Device File Class ass, Sy Symbolic Link File Class, File
Listing Progr am .
UNIT – 4
7 Hours
UNIX Processes: The Envir onment of a U NIX Process: Intr oduction, main function, Pr ocess Ter mination, Command-Line Arguments, Envir onment List, Me mory Layout
of a C Progr am, Shar ed Li br ar ies, Memory Allocation, Envir onment Var ia bles, setjmp and longj mp Functions,
Processes.
Dept.of CS&E,SJBIT
getr limit,
se tr limit Functions, U NIX Ker nel Support fo for
PAR T - B UNIT – 5
7 Hour ours
Process Control : Intr oduction, Process Identifier s, for k, vfork, e xit, wait, wait pid,
wait3, wait4 Functions, Race Conditions, exec Functions, Changing User IDs and Gr oup
IDs,
Inter preter
Files,
system
Function,
Process Accounting, User
Identificati on, Process Times, I/O Redire ction. Process
Relationshi ps:
Gr oups,
Sessions,
Intr oduction,
Contr olling
Terminal
Terminal,
Logins, Network
tcgetpgrp
and
Logins, Process
tcsetpgr p Functions,
Job
Contr ol, Shell Exec e cution of Progra ms, Orphaned Process Gr ou ps.
UNIT – 6
7 Hours
Support ort for for Signals, Signals and Daemon Proc o cesses: Signals: The UNIX Kernel Sup signal, Signal Mask, sigact ion, Th The SI SIGCHLD Si Signal and t he wait pid Function, The si sigsetj mp and siglongj mp Functions, Kill, Alarm, Inter val Timer s, POSIX.lb
Timers. Daemon
Processes:
Intr oduction,
Daemon
Char acter istic s,
Coding
Rule s, Er r or
Logging, Client-Se rve r M odel.
UNIT – 7
6 Hour ours
Interprocess Communication ion – 1: Over view of IPC Methods, Pipe s, popen, pclose Functions, Co pr ocesses, FIFOs, System V IPC, Message Queue s, Sema phor es.
UNIT – 8 Interprocess
6 Hours Co mm unication
–
2:
Shar ed
Memor y,
Client-Server Proper ties,
Str eam Pi pe s, Passing File Descr i ptor s, An Open Ser ver -Vers ion 1, Client-Server Connection Functions.
Dept.of CS&E,SJBIT
Text Books: 1.
Terrence Chan: U NIX Syste m Progr amming U sing C++, Prentice Hall India,
1999. (Chapters 1, 5, 6, 7, 8, 9, 10) 2.
W. Richard Stevens: Advanced Pr ogr amming in the U NIX nd
Envir onment, 2
Edition, Pearso n Educ ation, 2005. (Chapters 7, 8, 9, 13, 14, 15)
Ref erence Books: 1.
nd
Mar c J. Rochk ind: Advanced U NIX Progr amming, 2
Edition, Pear son
Education, 2005. 2. Maur ice J Bach: The Design of the UNIX Operating System, Pear son
Education, 1987. 3. Ur esh Vahalia: U NIX Internals: The New Frontiers, Pear so s on Education, 2001.
Dept.of CS&E,SJBIT
Table of contents Sl no
Chapter Description
Page no
1
UNIT 1 – Intr oduction…………………………….….………………….
1- 6
2
UNIT 2 – Unix Files…………….…..……………………………………
7-9
3
UNIT 3 – Unix File API’s………………………………………………..
10-36
4
UNIT 4 – Unix Pr ocesses..………………………………………………
37-41
5
UNIT 5 – Pr ocess Contr ol……………………………………………….
42-73
6
UNIT 6 – Signals & Daemon Pr ocess……..……………….……………
74-107
7
pr ocess Communication………………………………… UNIT 7 – Inter
108-139
8
UNIT 8 – Networ k IPC: Sock ets…………….…………………………..
140-147
Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
INTR ODUCTION 1.1 UNIX AND ANSI Standards The ISO (Inter national Standar ds Or ganization) def ines “standar ds ar e documented agr eements containing technical s pecif ications or other pr ecise cr iter ia to be used consistently as r ules, guidelines or def initions of char acter istics to ensur e that mater ials, pr oducts, pr ocesses and ser vices ar e f it f or their pur pose”. Most of f icial com puter standar ds ar e set by one of the f ollowing or ganizations:
A NSI (Amer ican National Standar ds Institute)
ITU
IEEE (Institute of Electr ical and Electr onic Engineer s)
ISO
VESA (Video Electr onics Standar ds
(Inter national Telecommunication Union)
(Inter national Standar ds Or ganization)
1.2 The ANSI C Standard This standar d was pr o posed by Amer ican A NSI in the year 1989 f or C pr ogr amming Language standar d called X3.159-1989 to standar dize the C pr ogr amming language constr ucts and li br ar ies.
1.3 Ma jor dif f erences between ANSI C and K & R C
A NSI C su p por ts Function Pr ototy ping
A NSI C su p por t of the const & volatile data ty pe qualif ier
A NSI C su p por t wide char acter s and inter nationalization, Def ines setlocale f unction
A NSI C per mits f unction pointer s to be used without der ef er encing
A NSI C def ines a set of pr e pr ocessor sym bols
A NSI C def ines a set of standar d li br ar y f unctions and associated header s.
1.4 The ANSI / ISO C++ Standard The C++ language is one of the OOP languages. It was develo ped by B jar ne Str oustr u p at At &T Bell La bor ator ies. C++ is an extension of C with a ma jor addition of the class constr uct f eatur es of Simula 67. The thr ee most im por tant f acilities that C++ adds on to C ar e classes, function over loading, & o per ator over loading.
Dept.of CS&E,SJBIT
In 1989, B jar ne Str oustr u p pu blished “The Annot at ed C ++ Re f er ence M anual ” , this manual become the base f or the dr af t A NSI C++ standar d. WG21 committee of the ISO joined the A NSI X3J16 committee to develo p a unif y A NSI/ISO C++ standar d. A dr af t ver sion of A NSI/ISO standar d was pu blished in 1994.
1.5 Ma jor Dif f erences between ANSI and C++
Function Declar ation or Function Pr ototy pe
Functions that tak e a var ia ble num ber of ar guments
Ty pe saf e link age , Link age Dir ectives
1.6 POSIX Standards POSIX is acr onym f or Por ta ble O per ating System Inter f ace. Ther e ar e thr ee su bgr ou ps in POSIX. They ar e : POSIX.1 :
Committee pr o poses a standar d f or base o per ating system APIs.
This standar d is f or mally k nown as the IEEE standar d 1003.1-1990.
This standar d s pecif ies the APIs f or the f ile mani pulation and
pr ocesses
(f or
Pr ocess Cr eation and Contr ol). POSIX.1b:
Committee pr o poses a standar d f or r eal time o per ating system APIs
This standar d is f or mally k nown as the IEEE standar d 1003.4-1993
This
standar d
s pecif ies
the
APIs
f or the
inter pr ocess
communication
(Sema phor es,Message Passing Shar ed Memor y). POSIX.1c:
Committee pr o poses a standar d f or multithr eaded pr ogr amming
inter f ace
This standar d s pecif ies the APIs f or Thr ead Cr eation, Contr ol, and Cleanu p, Thr ead Scheduling,Thr ead Synchr onization and f or Signal Handling .
To ensur e a user pr ogr am conf or ms to the POSIX.1 standar d, the user should def ine the manif ested constant _ POSIX _ SOUR CE at the beginning of each pr ogr am( bef or e the inclusion of any header f iles) as: #def ine _ POSIX _ SOUR CE
Dept.of CS&E,SJBIT
or 2
s pecif y the –D _ POSIX _ SOUR CE o ption to a C++ com piler dur ing
com pilation.
$g++ –D _ POSIX _ SOUR CE f ilename.c pp In gener al a user pr ogr am that must be str ictly POSIX.1and POSIX.1 b com pliant may be wr itten as f ollows:
#def ine _ POSIX _ SOUR CE #def ine _ POSIX _ C _SOUR CE 199309L #include
#include int main( ) { .... }
POSIX Feature Test Macros Feature Test Macro
Ef f ects if def ined on a System It allow us to star t multi ple jo bs(gr ou ps of pr ocesses) fr om a single ter minal and contr ol which jo bs can _POSIX _ JOB _ CO NTR OL access the ter minal and which jo bs ar e to r un in the back gr ound. Hence It su p por ts BSD ver sion Jo b Contr ol Featur e. Each pr ocess r unning on the system k ee ps the saved set-UID and set-GID, so that it can change ef f ective _POSIX _ SAVED _ IDS user ID and gr ou p ID to those values via set uid and set g id APIs r es pectively. If the def ined value is -1, user s may change owner shi p of f iles owned by them. Other wise only user s with _POSIX _ CHOW N _ R ESTR ICTED s pecial pr evilege may change owner shi p of any f iles on a system. If the def ined value is -1, any long path name passed to _POSIX _ NO _ TR U NC an API is silently tr uncated to NAME _ MAX bytes, other wise er r or is gener ated. If the def ined value is -1, ther e is no disa bling char acter _POSIX _ VDISABLE f or s pecial char acter s f or all ter minal device f iles, other wise the value is the disa bling char acter value.
Dept.of CS&E,SJBIT
Limits Check ing at Compile Time and at R un Time
The POSIX.1 and POSIX.1 b standar ds s pecif y a num ber of par ameter s that descr i be ca pacity limitations of the system.
Limits ar e def ined in .
These ar e pr ef ixed with the name _POSIX _
sysconf , pathcomf and f pathconf
To f ind out the actual im plemented conf igur ation limits
System wide using s y scon f dur ing r un time
On individual o b jects dur ing r un time using, pat hcon f and f pat hcon f . #include long sysconf (int par amet er ); long f pathconf (int f i l d es, int fli mi t _name)); long pathconf (const char * pat h, int fl i mi t _name);
For pathconf (), the pat h ar gument points to the pathname of a f ile or dir ector y.
For f pathconf (), the f il d e s ar gument is an o pen f ile descr i ptor .
1.7 The POSIX.1 FIPS Standard FIPS stands f or Feder al Inf or mation Pr ocessing Standar d. This standar d was develo ped by National Institute of Standar ds and Technology. The latest ver sion of this standar d, FIPS 1511, is based on the POSIX.1- 1998 standar d. The FIPS standar d is a r estr iction of the POSIX.1-1998 standar d, Thus a FIPS 151-1 conf or ming system is also POSIX.1-1998 conf or ming, but not vice ver sa. FIPS 151-1 conf or ming system r equir es f ollowing f eatur es to be im plemented in all FIPS conf or ming systems. _POSIX _ JOB _CO NTR OL _POSIX _ SAVED _ IDS
_POSIX _ JOB _ CO NTR OL must be def ined. _POSIX _ SAVED _ IDS must be def ined. _ POSIX _ CHOW N _ R ESTR ICTED must be def ined and its value is not -1, it means user s with s pecial _POSIX _ CHOW N _ R ESTR ICTED pr evilege may change owner shi p of any f iles on a system. If the def ined value is -1, any long path name passed to _POSIX _ NO _ TR U NC an API is silently tr uncated to NAME _ MAX bytes, other wise er r or is gener ated.
Dept.of CS&E,SJBIT
4
POSIX _ VDISABLE must be def ined and its value is not -1. Must be def ined and its value is not -1, Long path name _POSIX _ NO _ TR U NC is not su p por t. NGR OUP _ MAX Sym bol’s value must be at least 8. The r ead and wr ite API should r etur n the num ber of bytes that have been tr ansf er r ed af ter the APIs have been The gr ou p ID of a newly cr eated f ile must inher it the gr ou p ID of its containing dir ector y. _POSIX _ VDISABLE
Context Switching
A u ser mod e is the nor mal execution context of any user pr ocess, and it allows the pr ocess to access its s pecif ic data only. A k er nel mod e is the pr otective execution envir onment that allows a user pr ocess to access ker nels data in a r estr icted manner . When the APIs execution com pletes, the user pr ocess is switched back to the user mode. This context switching f or each API call ensur es that pr ocess access k er nels data in a contr olled manner and minimizes any chance of a r unway user a p plication may damage an entir e system. So in gener al calling an APIs is mor e time consuming than calling a user f unction due to the context switching. Thus f or those time cr itical a p plications, user should call their system APIs only if it is necessar y. An APIs common Characteristics
Most system calls r etur n a s pecial value to indicate that they have f ailed. The s pecial value is ty pically -1, a null pointer , or a constant such as EOF that is def ined f or that pur pose. To f ind out what k ind of er r or it was, you need to look at the er r or code stor ed in the var ia ble er r no. This var ia ble is declar ed in the header f ile er r no.h as shown below. volatile int errno o
o
The var ia ble er r no contains the system er r or num ber . void perror (con st char *me s sa g e) The f unction per r or is declar ed in stdio.h.
Dept.of CS&E,SJBIT
5
Following ta ble shows Some Er r or Codes and their meaning: Errors EPER M
Meaning API was a bor ted because the calling pr ocess does not have the su per user pr ivilege. EI NTR An APIs execution was a bor ted due to signal inter r u ption. EIO An In put/Out put er r or occur r ed in an APIs execution. E NOEXEC A pr ocess could not execute pr ogr am via one of the Exec API. EBADF An API was called with an invalid f ile descr i ptor . ECHILD A pr ocess does not have any child pr ocess which it can wait on. EAGAI N An API was a bor ted because some system r esour ce it is r equested was tem por ar ily unavaila ble. The API should call again later . E NOMEM An API was a bor ted because it could not allocate dynamic memor y. EACCESS The pr ocess does not have enough pr ivilege to per f or m the o per ation. EFAULT A pointer points to an invalid addr ess. EPIPE An API attem pted to wr ite data to a pi pe which has no r eader . E NOE NT An invalid f ile name was s pecif ied to an API.
Dept.of CS&E,SJBIT
Page
UNIX SYSTEM PROGRAMMING
10CS62
UNIT – 2 UNIX FILES UNIX / POSIX file Types
The dif f er ent ty pe’s f iles availa ble in U NIX / POSIX ar e:
Regular f iles
Exam ple: All .exe f iles, C, C++, PDF Document f iles.
Dir ector y f iles
Exam ple: Folder s in Windows.
Device f iles o
Block Device f iles:
A physical device that tr ansmits block of data at a time.
For exam ple: f lo p py devices CDR OMs, har d disk s. o
Char acter Device f iles: A physical device that tr ansmits data in a char acter based manner . For exam ple: Line pr inter s, modems etc.
FIFO f iles
Link Files
Exam ple: PIPEs.
Hard Link s
It is a U NIX path or f ile name, by def ault f iles ar e having only one har d link Symbolic Link s
Sym bolic link s ar e called sof t link s. Sof t link ar e cr eated in the same manner as har d link s, but it r equir es –s o ption to the l n command. Sym bolic link s ar e just lik e shor tcuts in windows. Dif f erences between Hard link s and Symbolic Link s Hard Link
1. Do not cr eate new inode.
Sof t Link s
1. Cr eate a new inode.
2. Cannot link dir ector ies unless 2. Can link dir ector ies. su per
user pr ivileges.
3. Cannot link f ile acr oss f ile systems.
3. Can link f iles acr oss f ile systems.
4. Incr ease the har d link count.
4. Does not change the har d link count.
5. Always r ef er to the old f ile only, 5.
Dept.of CS&E,SJBIT
Always
r ef er ence
to
the
latest Page 7
10CS62
means
har d link s can be br ok en by ver sion
removal of
of the f iles to which they link .
one or mor e link s.
UNIX K ernel supports f or f ile / K ernel Data structure f or f ile manipulation
If o pen call succeeds, k er nel esta blish the path between pr e pr ocess ta ble to inode ta ble thr ough file ta ble The Ste ps involved in this pr ocess ar e: Step 1: The k er nel will sear ch the pr ocess f ile descr i ptor ta ble and look f or f ir st unused
entr y, if an entr y is f ound, that entr y will be designated to r ef er ence the f ile. Step 2:The k er nel scan the f ile ta ble in its k er nel s pace to f ind an unused entr y that can be
assigned to r ef er ence the f ile. If an unused entr y is f ound, the f ollowing events will occur . The pr ocess’s f ile ta ble entr y will be set to point to this f ile ta ble entr y. o
The f ile ta ble entr y will be set to point to the inode ta ble entr y wher e the inode recor d of the f ile is stor ed.
o
The f ile ta ble entr y will contain the cur r ent f ile pointer of the o pen f ile.
o
The f ile ta ble entr y will contain o pen mode that s pecif ies that the f ile is o pen for r ead-only, wr ite-only or r ead-wr ite etc.
o
The r ef er ence count in the f ile ta ble entr y is set to 1. The r ef er ence count k ee ps tr ack of how many f ile descr i ptor s f r om any pr ocess ar e r ef er encing the entr y.
o
The r ef er ence count of the in-memor y inode of the f ile is incr eased by 1. This count s pecif ies how many f ile ta ble entr ies ar e pointing to that inode.
If either ste p1 or ste p2 f ails, the o pen f unction will r etur n with a -1 f ailur e status, no file descr i ptor ta ble or f ile ta ble entr y will be allocated. The f igur e shows a pr ocess’s f ile descr i ptor ta ble, the k er nel f ile ta ble and the inode af ter the pr ocess has o pened thr ee f iles: abc f or r ead only, and x yz for r ead- wr ite and x yz again f or wr ite only.
Dept.of CS&E,SJBIT
8
UNIX SYSTEM PROGRAMMING
File Descriptor Table
10CS62
File Table
r rc=1 rw rc=1
Process Space
Inode Table
rc=1
abc
rc=2
xyz
w rc=1
The r ef er ence count of an allocated f ile ta ble entr y is usually 1, but a pr ocess may When a pr ocess calls the f unction cl o se to close an o pened f ile, the f ollowing sequence of events will occur . 1) The k er nel sets the cor r es ponding f ile descr i ptor ta ble entr y to be unused. 2) It decr ements the r ef er ence count in the cor r es ponding f ile ta ble entr y by 1. If the ref er ence count is still non-zer o, go to ste p 6. 3) The f ile ta ble entr y is mar k ed as unused. 4) The r ef er ence count in the cor r es ponding f ile inode ta ble entr y is set decr emented by one. If the count is still non-zer o go to ste p 6. 5) If the har d link count of the inode is not zer o, it r etur ns to the caller with a success status other wise, it mar k s the inode ta ble entr y as unused and de- allocates all the physical disk stor age of the f ile. 6) It r etur ns to the caller to the pr ocess with 0 (success) statuses.
Dept.of CS&E,SJBIT
Page 9
UNIX SYSTEM PROGRAMMING
10C S 62
UNIT – 3 UNIX FILE API’S 3.1 General File APIs The f ile APIs that ar e availa ble to per f or m var ious o per ations on f iles in a f ile system ar e:
FILE APIs o pen ( ) read ( ) wr ite ( ) lseek ( ) close ( ) stat ( ) fstat ( ) chmod ( ) chown ( ) utime ( ) link ( ) unlink ( ) umask ( )
USE This API is used by a pr ocess to o pen a f ile f or data access. The API is used by a pr ocess to r ead data f r om a f ile The API is used by a pr ocess to wr ite data to a f ile The API is used by a pr ocess to allow r andom access to a f ile The API is used by a pr ocess to ter minate connection to a f ile The API is used by a pr ocess to quer y f ile attr i butes The API is used by a pr ocess to change f ile access per missions. The API is used by a pr ocess to change UID and/or GID of a f ile The API is used by a pr ocess to change the last modif ication and access time stam ps of a f ile The API is used by a pr ocess to cr eate a har d link to a f ile. The API is used by a pr ocess to delete har d link of a f ile The API is used by a pr ocess to set def ault f ile cr eation mask .
Open: It is used to o pen or cr eate a f ile by esta blishing a connection between the calling pr ocess and a f ile. Prototype: #include < sys/ty pes.h>
#include #include int open(const char * pat h _ name, int acce s s _ mod e, mode _ t per mi s sion);
path _ name : The pathname of a f ile to be o pened or cr eated. It can be an a bsolute path name or
relative path name. The pathname can also be a sym bolic link name. access _ mode: An integer values in the f or m of manif ested constants which s pecif ies how the
f ile is to be accessed by calling pr ocess. The manif ested constants can be classif ied as access mode f lags and access modif ier f lags.
Dept.of CS&E,SJBIT
Page 10
UNIX SYSTEM PROGRAMMING
10C S 62
Access mode f lags:
O _ R DONLY: O pen the f ile f or r ead only. If the f ile is to be o pened f or r ead only then the f ile
should alr eady exist in the f ile system and no modif ier f lags can be used.
O _ WR ONLY: O pen the f ile f or wr ite only. If the f ile is to be o pened f or wr ite only, then any of
the access modif ier f lags can be s pecif ied.
O _ R DWR : O pen the f ile f or r ead and wr ite. If the f ile is to be o pened f or wr ite only, then any of
the access modif ier f lags can be s pecif ied. Access modif ier f lags ar e o ptional and can be s pecif ied by bitwise-OR ing them with one of the a bove access mode f lags to alter the access mechanism of the f ile.
Access Modif ier Flags:
O _ APPE ND : A p pends data to the end of the f ile. If this is not s pecif ied, data can be wr itten anywher e in the f ile.
O _ CR EAT
:
Cr eate the f ile if it does not exist. If the f ile exists it has no
ef f ects. However
if the f ile does not exist and O _ CR EATE is not s pecif ied, o pen will a bor t with a f ailur e r etur n status.
O _ EXCL
: Used with O _ CR EAT, if the f ile exists, the call f ails. The test f or existence and
the cr eation if the f ile does not exists.
O _ TR U NC
:
If the f ile exits, discar ds the f ile contents and sets the f ile size to zer o.
O _ NOCTTY
:
S pecies not to use the named ter minal device f ile as the calling pr ocess contr ol
ter minal.
O _ NO NBLOCK : S pecif ies that any su bsequent r ead or wr ite on the f ile should be non- block ing.
Exam ple, a pr ocess is nor mally block ed on r eading an em pty pi pe or on wr iting to a pi pe that is full. It may be used to s pecif y that such r ead and wr ite o per ations ar e non- block ing. Exam ple: int f desc = o pen(“/usr /xyz/ pr og1”, O _ R DWR |O _ APPE ND,0); If a f ile is to be o pened f or r ead-only, the f ile should alr eady exist and no other modif ier f lags can be used. O _ APPE ND, O _ TR U NC, O _ CR EAT and O _ EXCL ar e a p plica ble f or r egular f iles, wher eas O _ NO NBLOCK is f or FIFO and device f iles only, and O _ NOCTTY is f or ter minal device f ile only. Dept.of CS&E,SJBIT
Page 11
UNIX SYSTEM PROGRAMMING
10C S 62
Permission:
The per mission ar gument is r equir ed only if the O _ CR EAT f lag is set in the access _ mode ar gument. It s pecif ies the access per mission of the f ile f or its owner , gr ou p and all the other peo ple.
Its data ty pe is int and its value is octal integer value, such as 0764. The lef t-most, middle and right-most bits s pecif y the access per mission f or owner , gr ou p and other s r es pectively.
In each octal digit the lef t-most, middle and r ight-most bits s pecif y r ead, wr ite and execute per mission r es pectively.
For exam ple 0764 s pecif ies 7 is f or owner , 6 is f or gr ou p and 4 is f or other . 7 = 111 s pecif ies r ead, wr ite and execution per mission f or owner . 6 = 110 s pecif ies r ead, wr ite per mission f or gr ou p. 4 = 100 s pecif ies r ead per mission f or other s. Each bit is either 1, which means a r ight is gr anted or zer o, f or no such r ights.
POSIX.1 def ines the per mission data ty pe as mode _ t and its value is manif ested constants which ar e aliases to octal integer values. For exam ple, 0764 per mission value should be s pecif ied as: S _ IR WXU|S _ IR GR P|S _ IWGR P|S _ IR OTH
P er mi s sion value is modif ied by its calling pr ocess uma sk value. An uma sk value s pecif ies some access r ights to be mask ed of f (or tak en away) automatically on any f iles cr eated by pr ocess.
The f unction pr ototy pe of the uma sk API is: mode _ t
umask (mode _ t
new _ umask );
It tak es new mask value as ar gument, which is used by calling pr ocess and the f unction r etur ns the old umask value. For exam ple, mode _ t
old _ mask = umask (S _ IXGR P | S _ IWOTH |S _ IXOTH);
The a bove f unction sets the new umask value to “no execute f or gr ou p” and “no wr ite-execute for other s”.
The o pen f unction tak es its per mission ar gument value and bitwise-A NDs it with the one’s com plement of the calling pr ocess umask value. Thus, actual _ permission = permission & ~ umask _ value
Exam ple: actual _ permission = 0557 & (~ 031) = 0546
Dept.of CS&E,SJBIT
Page 12
UNIX SYSTEM PROGRAMMING
10C S 62
The r etur n value of o pen f unction is -1 if the API f ails and er r no contains an er r or status value. If the API succeeds, the r etur n value is f ile descr i ptor that can be used to r ef er ence the f ile and its value should be between 0 and OPE N _ MAX-1.
Creat: The cr eat system call is used to cr eate new r egular f iles. Its pr ototy pe is: #include < sys/ty pes.h>
#include int creat (const char * pat h _ name, mode _ t mode); 1. The path _ name ar gument is the path name of a f ile to be cr eated. 2. The mode ar gument is same as that f or o pen API. Since O _ CR EAT f lag was added to o pen API it was used to both cr eate and o pen r egular f iles. So, the cr eat API has become o bsolute. It is r etained f or back war d-com pati bility with ear ly ver sions of U NIX. The cr eat f unction can be im plemented using the o pen f unction as: #def ine creat (path _ name, mode) open(path _ name, O _ WRONLY|O _ CR EAT|O _ TR UNC, mode)
read: This f unction f etches a f ixed size block of data f r om a f ile r ef er enced by a given f ile descr i ptor . Its pr ototy pe is: #include #include ssize _ t r ead (int f desc ,void* buf , size _ t size);
f desc: is an integer f ile descr i ptor that r ef er s to an o pened f ile.
buf : is the addr ess of a buf f er holding any data r ead.
size: s pecif ies how many bytes of data ar e to be r ead f r om the f ile.
Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10C S 62
**Note: read f unction can r ead text or binar y f iles. This is why the data ty pe of buf is a univer sal
pointer (void *). For exam ple the f ollowing code r eads, sequentially one or mor e r ecor d of str uct sam ple-ty ped data f r om a f ile called d base: str uct sam ple { int x; dou ble y; char * a;} var X; int f d = o pen(“d base”, O _ R DO NLY); while ( r ead(f d, &var X, sizeof (var X))>0)
The r etur n value of r ead is the num ber of bytes of data successf ully r ead and stor ed in the buf ar gument. It should be equal to the si z e value.
If a f ile contains less than si z e bytes of data r emaining to be r ead, the r etur n value of r ead will be less than that of si z e. If end-of -f ile is r eached, r ead will r etur n a zer o value.
si z e _ t is def ined as int in header , user s should not set si z e to exceed I NT _ MAX in any r ead function call.
If a r ead f unction call is inter r u pted by a caught signal and the OS does not r estar t the system call automatically, POSIX.1 allows two possi ble behavior s: 1. The r ead f unction will r etur n -1 value, er r no will be set to EI NTR , and all the data will be discar ded. 2. The r ead f unction will r etur n the num ber of bytes of data r ead pr ior to the signal inter r u ption. This allows a pr ocess to continue r eading the f ile.
The r ead f unction may block a calling pr ocess execution if it is r eading a FIFO or device f ile and data is not yet availa ble to satisf y the r ead r equest. User s may s pecif y the O _ NO NBLOCK or O _ NDELAY f lags on a f ile descr i ptor to r equest non block ing r ead o per ations on the cor r es ponding f ile.
write: The wr ite f unction puts a f ixed size block of data to a f ile r ef er enced by a file descr i ptor Its pr ototy pe is: #include #include ssize _ t wr ite (int f desc , const void* buf , size _ t size);
f desc: is an integer f ile descr i ptor that r ef er s to an o pened f ile.
buf : is the addr ess of a buf f er which contains data to be wr itten to the f ile.
Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10C S 62
size: s pecif ies how many bytes of data ar e in the buf ar gument. **Note: wr ite f unction can r ead text or binar y f iles. This is why the data ty pe of buf is a
univer sal pointer (void *). For exam ple, the f ollowing code f r agment wr ites ten r ecor ds of str uct sam ple-ty pes data to a f ile called d base2: str uct sam ple { int x; dou ble y; char * a;} var X[10]; int f d = o pen(“d base2”, O _ WR O NLY); wr ite(f d, (void*)var X, sizeof var X);
The r etur n value of wr it e is the num ber of bytes of data successf ully wr itten to a f ile. It should be equal to the si z e value.
If the wr ite will cause the f ile size to exceed a system im posed limit or if the f ile system disk is full, the r etur n value of wr ite will be the actual num ber of bytes wr itten bef or e the f unction was a bor ted.
If a signal ar r ives dur ing a wr ite f unction call and the OS does not r estar t the system call automatically, the wr ite f unction may either r etur n a -1 value and set er r no to EI NTR or r etur n the num ber of bytes of data wr itten pr ior to the signal inter r u ption.
The wr ite f unction may per f or m non block ing o per ation if the O _ NO NBLOCK or O _ NDELAY flags ar e set on the f desc ar gument to the f unction.
close: The close f unction disconnects a f ile f r om a pr ocess. Its pr ototy pe is: #include int close (int f desc);
f desc: is an integer f ile descr i ptor that r ef er s to an o pened f ile.
The r etur n value of close is zer o if the call succeeds or -1 if it f ails.
The close f unction f r ees unused f ile descr i ptor s so that they can be r eused to r ef er ence other files.
The close f unction will deallocate system r esour ces which r educes the memor y r equir ement of a pr ocess.
Dept.of CS&E,SJBIT
Page 15
UNIX SYSTEM PROGRAMMING
10C S 62
If a pr ocess ter minates without closing all the f iles it has o pened, the k er nel will close f iles f or the pr ocess.
fcntl: The f cntl f unction hel ps to quer y or set access contr ol f lags and the close-on-exec f lag of any f ile descr i ptor . User s can also use f cntl to assign multi ple f ile descr i ptor s to r ef er ence the same f ile. Its pr ototy pe is: #include int f cntl (int f desc ,int cmd, ….);
f desc: is an integer f ile descr i ptor that r ef er s to an o pened f ile.
cmd: s pecif ies which o per ation to per f or m on a f ile r ef er enced by the f desc ar gument.
The thir d ar gument value, which may be s pecif ied af ter cmd is de pendent on the actual cmd value.
The possi ble cmd values ar e def ined in the header . These values and their uses ar e: cmd value F _ GETFL F _ SETFL
F _ GETFD
F _ SETFD
F _ DUPFD
Us e
Retur ns the access contr ol f lags of a f ile descr i ptor f desc. Sets or clear s access contr ol f lags that ar e s pecif ied in the thir d ar gument to f cntl. The allowed access contr ol f lags ar e O _ APPE ND and O _ NO NBLOCK . Retur ns the close-on-exec f lag of a f ile r ef er enced by f desc. If a r etur n value is zer o, the f lag is of f , other wise the retur n value is nonzer o and the f lag is on. The close-onexec f lag of a newly o pened f ile is of f by def ault. Sets or clear s the close-on-exec f lag of a f ile descr i ptor f desc. The thir d ar gument to f cntl is integer value, which is 0 to clear , or 1 to set the f lag. Du plicates the f ile descr i ptor f desc with another f ile descr i ptor . The thir d ar gument to f cntl is an integer value which s pecif ies that the du plicated f ile descr i ptor must be gr eater than or equal to that value. The r etur n value of fcntl, in this case is the du plicated f ile descr i ptor .
The f cntl f unction is usef ul in changing the access contr ol f lag of a f ile descr i ptor .
Dept.of CS&E,SJBIT
Page 16
UNIX SYSTEM PROGRAMMING
10C S 62
For example: Af ter a f ile is o pened f or block ing r ead-wr ite access and the pr ocess needs to
change the access to non block ing and in wr ite-a p pend mode, it can call f cntl on the f ile’s descr i ptor as: int cur _ f lags = f cntl(f desc, FGETFL); int r c = f cntl(f desc, F _ SETFL, cur _ f lag | O _ APPE ND |O _ NO NBLOCK );
The close-on-exec f lag of a f ile descr i ptor s pecif ies that if the pr ocess that owns the descr i ptor calls the exec API to execute dif f er ent pr ogr am, the f desc should be closed by the k er nel bef or e the new pr ogr am r uns or not.
The exam ple r e por ts the close-on-exec f lag of a f desc, sets it to on af ter war ds: cout<
The f cntl f unction can also be used to du plicate a f desc with another f desc. The r esults ar e two f desc r ef er ence the same f ile with same access mode and shar e the same f ile pointer to r ead or wr ite the f ile. This is usef ul in the r edir ection of standar d in put or out put to r ef er ence a f ile. Exam ple: R ef er ence standar d in put of a pr ocess to a f ile called FOO int f desc = open(“FOO”, O _ R DONLY);
//open FOO f or read
close(0);
//close standard input
if (f cntl(f desc, F _ DUPFD, 0)==-1) perror(“f cntl”);
//stdin f rom FOO
char buf [256]; int rc = read(0,buf ,256);
//read data f rom FOO
The du p and du p2 f unctions in U NIX per f or m the same f ile du plication f unction as f cntl. They can be im plemented using f cntl as: #def ine dup(f desc)
fcntl(f desc, F _ FUPFD,0 )
#def ine dup2(f desc1,f d2)
close(f d2),f cntl(f desc, F _ DUPFD, f d2)
The du p f unction du plicates a f desc with the lowest unused f desc of a calling pr ocess. The du p2 f unction will du plicate a f desc using a f d2 f desc, r egar dless of whether f d2 is used to ref er ence another f ile.
lseek :
Dept.of CS&E,SJBIT
Page 17
UNIX SYSTEM PROGRAMMING
10C S 62
The lseek system call can be used to change the f ile of f set to a dif f er ent value. It allows a pr ocess to per f or m r andom access of data on any o pened f ile. Lseek is incom pati ble with FIFO f iles, char acted device f iles and sym bolic link f iles. Its pr ototy pe is: #include #include _ t pos, int whence); of f _ t lseek (int f desc , of f
f desc: is an integer f ile descr i ptor that r ef er s to an o pened f ile.
pos: s pecif ies a byte of f set to be added to a r ef er ence location in der iving the new f ile of f set
value.
whence: s pecif ies the r ef er ence location. Whence value
SEEK _ CUR
Ref erence location
cur r ent f ile pointer addr ess
SEEK _ SET
The beginning of a f ile
SEEK _ E ND
The end of a file
**NOTE: a. It is illegal to s pecif y a negative pos value with the whence value set to SEEK _ SET as this
will set negative of f set. b. If an lseek call will r esult in a new f ile of f set that is beyond end-of -f ile, two outcomes ar e
possi ble: 1. If a f ile is o pened f or r ead only the lseek will f ail. 2. If a f ile is o pened f or wr ite access, lseek will succeed and it will extend the f ile size u p to the new f ile of f set addr ess.
The r etur n value of lseek is the new f ile of f set addr ess wher e the next r ead of wr ite o per ation will occur , or -1 if lseek call f ails. The iostr eam class def ines t el l g and seek g f unctions to allow user s to r andomly access data f r om any isotr eam class. These f unctions can be im plemented using the l seek function as f ollows: #include #include #include Dept.of CS&E,SJBIT
Page 18
UNIX SYSTEM PROGRAMMING
10C S 62
str eam pos iostr eam::tellg() { retur n (str eam pos)lseek (this->f ileno(),(of f _ t)0,SEEK _ CUR ); } iostr eam&iostr eam::seek g(str eam pos pos,seek _ dir r ef _ loc) { if (r ef _ loc == ios:: beg) (void)lseek (this->f ileno(), (of f _ t) pos, SEEK _ SET); else if (r ef _ loc == ios::cur ) (void)lseek (this->f ileno(), (of f _ t) pos, SEEK _ CUR ); else if (r ef _ loc == ios::end) (void)lseek (this->f ileno(), (of f _ t) pos, SEEK _ E ND); retur n *this; }
The iostr eam::tellg sim ply calls lseek to r etur n the cur r ent f ile pointer associated with an iostr eam o b ject. The f ile descr i ptor of an iostr eam o b ject const char* is o btained f r om the f ileno mem ber f unction.
The iostr eam::seek g r elies on lseek to alter the f ile pointer associated with an iostr eam o b ject. The ar guments ar e f ile of f set and a r ef er ence location f or the of f set. This f unction also conver ts seek _ dir value to an lseek whence value. Ther e is one-to-one ma p ping of the seek _ dir values to the whence values used by lseek : seek _ dir value
lseek whence value
ios:: beg
SEEK _ SET
ios::cur
SEEK _ CUR
ios::end
SEEK _ E ND
link : The link f unction cr eates a new link f or an existing f ile . This f unction does not cr eate a new f ile. It cr eate a new path name f or an existing f ile. Its pr ototy pe is: #include int link (const char* cur _ link ,const char* new _ link )
cur _ link : is a path name of an existing f ile.
Dept.of CS&E,SJBIT
Page 19
UNIX SYSTEM PROGRAMMING
10C S 62
new _ link : is a new path name to be assigned to the same f ile.
If this call succeeds, the har d link count attr i bute of the f ile will be incr eased by 1.
link cannot be used to cr eate har d link s acr oss f ile systems. It cannot be used on dir ector y f iles unless it is called by a pr ocess that has su per user pr evilege. The l n command is im plemented using the link API. The pr ogr am is given below: #include #include int main(int ar gc,char * ar gv[]) { if (ar gc!=3) { pr intf ("usage:%s",ar gv[0]); pr intf ("\n"); retur n 0; } if (link (ar gv[1],ar gv[2]) == -1) { per r or ("link "); retur n 1; } retur n 0; }
unlink : This f unction deletes a link of an existing f ile. It decr eases the har d link count attr i butes of the named f ile, and r emoves the f ile name entr y of the link f r om a dir ector y f ile. If this f unction succeeds the f ile can no longer be r ef er enced by that link . File will be r emoved by the f ile system if the har d link count of the f ile is zer o and no pr ocess has f desc r ef er encing that f ile. Its pr ototy pe is: #include int unlink (const char* cur _ link )
cur _ link : is a path name of an existing file.
The r etur n value is 0 if it succeeds or -1 if it f ails.
Dept.of CS&E,SJBIT
Page 20
UNIX SYSTEM PROGRAMMING
10C S 62
The f ailur e can be due to invalid link name and calling pr ocess lack s access per mission to remove the path name. It cannot be used to r emove dir ector y f iles unless the calling pr ocess has su per user pr ivilege. A NSI C def ines r emove f unction which does the similar o per ation of unlink . If the ar gument to the r emove f unctions is em pty dir ector y it will r emove the dir ector y. The pr ototy pe of r ename function is: #include int r ename (const char* old _ path _ name ,const char* new _ path _ name) The r ename will f ail when the new link to be cr eated is in a dif f er ent f ile system than the or iginal file. The mv command can be im plemented using the link and unlink APIs by the pr ogr am given below: #include #include #include int main(int ar gc, char * ar gv[]) { if (ar gc!=3 || !str cm p(ar gv[1],ar gv[2])) cer r <<“usage:”<\n”; else if (link (ar gv[1], ar gv[2])==0) retur n unlink (ar gv[1]); retur n -1; }
stat, f stat: These f unctions r etr ieve the f ile attr i butes of a given f ile. The f ir st ar gument of st at is f ile path name wher e as f st at is a f ile descr i ptor . The pr ototy pe is given below: #include #include int stat (const char * path _ name,str uct stat* statv) int f stat (const int f desc,str uct stat* statv)
Dept.of CS&E,SJBIT
Page 21
UNIX SYSTEM PROGRAMMING
10C S 62
The second ar gument to stat & f stat is the addr ess of a str uct stat-ty ped var ia ble. The declar ation of str uct stat is given below: str uct stat { dev _ ts
t _ dev; //f ile system ID
ino _ t
st _ ino;
//File inode num ber
mode _ t
st _ mode;
//contains f ile ty pe and access f lags
nlink _ t
st _ nlink ;
//har d link count
uid _ t
st _ uid;
//f ile user ID
gid _ t
st _ gid;
//f ile gr ou p ID
dev _ t
st _ r dev;
//contains ma jor and minor device num ber s
of f _ t
st _ size;
//f ile size in num ber of bytes
time _ t
st _ atime;
//last access time
time _ t
st _ mtime;
//last modif ication time
time _ t
st _ ctime;
//last status change time
};
The r etur n value of both f unctions is 0 if it succeeds or -1 if it f ails. Possi ble f ailur es may be that a given f ile path name of f ile descr i ptor is invalid, the calling pr ocess lack s per mission to access the f ile, or the f unction inter r u pted by a signal. If a path name ar gument s pecif ied to stat is a sym bolic link f ile, stat will r esolve the link and access the non sym bolic link f ile. Both the f unctions cannot be used to o btain the attr i butes of sym bolic link f ile.
To o btain the attr i butes of sym bolic link f ile lstat f unction was invented. Its pr ototy pe is: int lstat (const char * path _ name,str uct stat* statv)
The U NIX ls command is im plemented by the pr ogr am given below: #include #include #include #include #include < pwd.h> #include
Dept.of CS&E,SJBIT
Page 22
UNIX SYSTEM PROGRAMMING
10C S 62
static char xt bl[10] = "r wxr wxr wx"; static void dis play _ f ile _ ty pe ( ostr eam& of s, int st _ mode ) { switch (st _ mode &S _ IFMT) { case S _ IFDIR :
of s << 'd'; r etur n;
/* dir ector y f ile */
case S _ IFCHR :
of s << 'c'; r etur n;
/* char acter device f ile */
case S _ IFBLK :
of s << b ' '; r etur n;
/* block device f ile */
case S _ IFR EG:
of s << ' '; r etur n;
/* r egular f ile */
case S _ IFL NK :
of s << 'l'; r etur n;
/* sym bolic link f ile */
case S _ IFIFO:
of s << p ' '; r etur n;
/* FIFO f ile */
} } /* Show access per mission f or owner , gr ou p, other s, and any s pecial f lags */ static void dis play _ access _ per m ( ostr eam& of s, int st _ mode ) { char amode[10]; for (int i=0, j= (1 << 8); i < 9; i++, j>>=1) amode[i] = (st _ mode& j) ? xt bl[i] : '-';
/* set access per mission */
if (st _ mode&S _ ISUID) amode[2] = (amode[2]=='x') ? 'S' : 's'; if (st _ mode&S _ ISGID) amode[5] = (amode[5]=='x') ? 'G' : 'g'; if (st _ mode&S _ ISVTX) amode[8] = (amode[8]=='x') ? 'T' : 't'; of s << amode << ' '; } /* List attr i butes of one f ile */ static void long _ list (ostr eam& of s, char * path _ name) { str uct stat
statv;
str uct gr ou p*gr _ p; str uct passwd* pw _ p; if (lstat ( path _ name, &statv)) Dept.of CS&E,SJBIT
Page 23
UNIX SYSTEM PROGRAMMING
10C S 62
{ cer r <<”Invalid path name:”<< path _ name<
/* dis play har d link count */
gr _ p = getgr gid(statv.st _ gid);
/* conver t GID to gr ou p name */
pw _ p = get pwuid(statv.st _ uid);
/*conver t UID to user name */
of s << ' ' <<( pw _ p-> pw _ name ? pw _ p-> pw _ name:statv.st _ uid) << ' ' <<(gr _ p->gr _ name ? gr _ p->gr _ name:statv.st _ gid)<< ' '; if ((statv.st _ mode&S _ IFMT) == S _ IFCHR || (statv.st _ mode&S _ IFMT)==S _ IFBLK ) of s << MAJOR (statv.st _ r dev) << ',' <<
MI NOR (statv.st _ r dev);
else of s << statv.st _ size;
/* show f ile size or ma jor /minor no. */
of s << ' ' << ctime (&statv.st _mtime);
/* pr int last modif ication time */
of s << ' ' << path _ name << endl;
/* show f ile name */
} /* Main loo p to dis play f ile attr i butes one f ile at a time */ int main (int ar gc, char * ar gv[]) { if (ar gc==1) cer r << "usage: " << ar gv[0] << " ...\n"; else while (--ar gc >= 1) long _ list( cout, *++ar gv); retur n 0; }
access: The access f unction check s the existence and/or access per mission of user to a named f ile. The pr ototy pe is given below: #include int access (const char* path _ name, int flag); Dept.of CS&E,SJBIT
Page 24
UNIX SYSTEM PROGRAMMING
10C S 62
path _ name: The pathname of a f ile. flag: contains one or mor e of the f ollowing bit-flags. Bit Flag
Use
F _ OK
Check s whether a named f ile exists.
R _ OK
Check s whether a calling pr ocess has r ead per mission
W _ OK
Check s whether a calling pr ocess has wr ite per mission
X _ OK
Check s whether a calling pr ocess has execute per mission
The f lag ar gument value to access call is com posed by bitwise-OR ing one or mor e of the a bove bit-flags. The f ollowing statement check s whether a user has r ead and wr ite per missions on a file /usr /s j b/f ile1.doc: int r c = access(“/usr /s j b/f ile1.doc”, R _ OK |W _ OK );
If a f lag value is F _ OK , the f unction r etur ns 0 if the f ile exists and -1 other wise. If a f lag value is any com bination of R _ OK , W _ OK and X _ OK , the access f unction uses the calling pr ocess r eal user ID and r eal gr ou p ID to check against the f ile user ID and gr ou p ID. The f unction r etur ns 0 if all the r equested per mission is per mitted and -1 other wise.
The f ollowing pr ogr am uses access to deter mine, f or each command line ar gument, whether a named f ile exists. If a named f ile does not exist, it will be cr eated and initialized with a char acter str ing “Hello wor ld”. #include #include #include int main(int ar gc, char *ar gv[]) { char buf [256]; int f desc,len; while(--ar gc>0) { if (access(*++ar gv,F _ OK )) {
//a br and new f ile
fdesc = o pen(*ar gv, O _ WR O NLY|O _ CR EAT, 0744); Dept.of CS&E,SJBIT
Page 25
UNIX SYSTEM PROGRAMMING
10C S 62
wr ite(f desc, “Hello wor ld\n”, 12); } else { fdesc = o pen(*ar gv, O _ R DO NLY); while(len = r ead(f desc, buf ,256)) wr ite(1, buf , len); } close(f desc); } }
chmod, f chmod: The chmod and f cmod f unctions change f ile access per missions f or owner , gr ou p and other s and also set-UID, set-GID and stick y f lags. A pr ocess that calls one of these f unctions should have the ef f ective user ID of either the su per user or the owner of the f ile. The pr ototy pe of these f unctions is given below: #include #include #include int chmod (const char * path _ name, mode _ t f lag); int f chmod (int f dsec, mode _ t f lag); The chmod f unction uses path name of a f ile as a f ir st ar gument wher eas f chmod uses f desc as the f ir st ar gument. The f lag ar gument contains the new access per mission and any s pecial f lags to be set on the f ile. For example: The f ollowing f unction tur ns on the set-UID f lag, r emoves gr ou p wr ite per mission
and other s r ead and execute per mission on a f ile named /usr /s j b/ pr og1.c #include #include
Dept.of CS&E,SJBIT
Page 26
UNIX SYSTEM PROGRAMMING
10C S 62
#include void change _ mode( ) { str uct stat statv; int f lag = (S _ IWGR P|S _ IR OTH|S _ IXOTH); if (stat(“/usr /s j b/ pr og1.c”, &statv)) per r or (“stat”); else { flag = (statv.st _ mode & ~f lag) | S _ ISUID; if (chmod(“usr /s j b/ pr og1.c”, f lag)) per r or (“chmod”); } }
chown, f chown, lchown: The chown and f chown f unctions change the user ID and gr ou p ID of f iles. They dif f er only in their f ir st ar gument which r ef er to a f ile by either a path name or a f ile descr i ptor . The lchown function changes the owner shi p of sym bolic link f ile. The chown f unction changes the owner shi p of the f ile to which the sym bolic link f ile ref er s. The f unction pr ototy pes of these f unctions ar e given below: #include #include int chown (const char* path _ name, uid _ t uid, gid _ t gid); int f chown (int f desc, uid _ t uid, gid _ t gid); int lchown (const char* path _ name, uid _ t uid, gid _ t gid);
1. path _ name: is the path name of a f ile. 2. uid: s pecif ies the new user ID to be assigned to the f ile. 3. gid : s pecif ies the new gr ou p ID to be assigned to the f ile. If the actual value of uid or gid ar gument is -1 the ID of the f ile is not changed.
Dept.of CS&E,SJBIT
Page 27
UNIX SYSTEM PROGRAMMING
10C S 62
3.2 File and R ecord Lock ing: U NIX systems allow multi ple pr ocesses to r ead and wr ite the same f ile concur r ently which pr ovides data shar ing among pr ocesses. It also r ender s dif f iculty f or any pr ocess in deter mining when data in a f ile can be over r idden by another pr ocess. In some of the a p plications lik e a data base manager , wher e no other pr ocess can wr ite or r ead a f ile while a pr ocess is accessing a data base f ile. To over come this dr aw back , U NIX and POSIX systems su p por t a f ile lock ing mechanism. File lock ing is a p plica ble only f or r egular f iles. It allows a pr ocess to im pose a lock on a f ile so that other pr ocesses cannot modif y the f ile until it is unlock ed by the pr ocess. A pr ocess can im pose a wr ite lock or a read lock on either a por tion of a file or an entir e f ile. The dif f er ence between wr ite lock s and r ead lock s is that when a wr ite lock is set, it pr events other pr ocesses f r om setting any over la p ping r ead or wr ite lock s on the lock ed r egion of a f ile. On the other hand, when a r ead lock is set, it pr events other pr ocesses f r om setting any over la p ping wr ite lock s on the lock ed r egion of a f ile. The intention of a wr ite lock is to pr event other pr ocesses f r om both r eading and wr iting the lock ed r egion while the pr ocess that sets the lock is modif ying the r egion. A wr ite lock is also k nown as an e xcl u sive l ock . The use of a r ead lock is to pr event other pr ocesses f r om wr iting to the lock ed r egion while the pr ocess that sets the lock is r eading data f r om the r egion. Other pr ocesses ar e allowed to lock and r ead data f r om the lock ed r egions. Hence, a r ead lock is also called a shar ed l ock .
3.2.1 Mandatory Lock Mandator y lock s ar e enf or ced by an o per ating system k er nel. If a mandator y exclusive lock is set on a f ile, no pr ocess can use the r ead or wr it e system calls to access data on the lock ed r egion. If a mandator y shar ed lock is set on a r egion of a f ile, no pr ocess can use the wr it e system call to modif y the lock ed r egion. It is used to synchr onize r eading and wr iting of shar ed f iles by multi ple pr ocesses: If a pr ocess lock s u p a f ile, other pr ocesses that attem pts to wr ite to the lock ed r egions ar e block ed until the for mer pr ocess r eleases its lock .
Dept.of CS&E,SJBIT
Page 28
UNIX SYSTEM PROGRAMMING
10C S 62
Mandator y lock s may cause pr o blems: If a r unaway pr ocess sets a mandator y exclusive lock on a f ile and never unlock s it, no other pr ocesses can access the lock ed r egion of the f ile until either the r unaway pr ocess is killed or the system is r e booted. System V.3 and V.4 su p por t mandator y lock s.
3.2.2 Advisory Lock An advisor y lock is not enf or ced by a k er nel at the system call level. This means that even though lock (r ead or wr ite) may be set on a f ile, other pr ocesses can still use the r ead or wr it e APIs to access the f ile. To mak e use of advisor y lock s, pr ocesses that mani pulate the same f ile must coo per ate such that they follow this pr ocedur e f or ever y r ead or wr ite o per ation to the f ile: a. Tr y to set a lock at the r egion to be accessed. If this f ails, a pr ocess can either wait f or the lock r equest to become successf ul or go do something else and tr y to lock the f ile again later . b. Af ter a lock is acquir ed successf ully, r ead or wr ite the lock ed r egion release the lock c. The dr aw back of advisor y lock s ar e that pr ogr ams that cr eate pr ocesses to shar e f iles must f ollow the a bove f ile lock ing pr ocedur e to be coo per ative. This may be dif f icult to contr ol when pr ogr ams ar e o btained f r om dif f er ent sour ces. All U NIX and POSIX systems su p por t advisor y lock s. U NIX System V and POSIX.I use the f cnt l API f or f ile lock ing. The pr ototy pe of the f cnt l API is : #include int f cntl(int f desc, int cmd _ f lag, …);
The f d e sc ar gument is a f ile descr i ptor f or a f ile to be pr ocessed. The cmd f la g ar g ument def ines which o per ation is to be per f or med. cmd F l a g
Us e
F _ SETLK
Sets a f ile lock . Do not block if this cannot succeed immediately
F _ SETLK W
Sets a f ile lock and block s the calling pr ocess until the lock is acquir ed
F _ GETLK
Quer ies as to which pr ocess lock ed a s pecif ied region of a f ile
Dept.of CS&E,SJBIT
Page 29
UNIX SYSTEM PROGRAMMING
10C S 62
For f ile lock ing, the thir d ar gument to f cnt l i s an addr ess of a st r uct fl ock -t y ped var ia ble. This var ia ble s pecif ies a r egion of a f ile wher e the lock is to be set, unset, or quer ied. The st r uct f l ock is declar ed in the as: str uct f lock { shor t l _ ty pe; // what lock to be set or to unlock f ile shor t l _ whence;
// a r ef er ence address f or the next f ield
of f _ t l _ star t; //of f set f r om the l _ whence r ef er ence addr ess of f _ t l _ len;
// how many bytes in the lock ed r egion
pid _ t l _ pid;
//PID of a pr ocess which has lock ed the f ile
};
The possi ble values of l _ ty pe ar e: y pe value l _ t
Us e
F _ RDLCK
Sets a a r ead (shar ed) lock on a s pecif ied r egion
F _ WR LCK
Sets a wr ite (exclusive) lock on a s pecif ied r egion
F _ U NLCK
Unlock s a s pecif ied r egion
The possi ble values of l _ whence and their uses ar e: l _whence value
Us e
SEEK _ CUR
The l _ st ar t value is added to the cur r ent f ile pointer addr ess.
SEEK _ CUR
The ! _ st ar t value is added to the cur r ent f ile pointer Use addr ess
SEEK _ SET
The l _ st ar t value is added to byte 0 of the f ile
SEEK _ E ND
The l _ st ar t value ts'added to the end (cur r ent size) of the f ile
3.2.3 Lock Promotion and Lock splitting: If a pr ocess sets a r ead lock on a f ile, f or exam ple f r om addr ess 0 to 256, then sets a wr ite lock on the f ile f r om addr ess 0 to 512, the pr ocess will own only one wr ite lock on the f ile f r om 0 to 512. The pr evious r ead lock f r om 0 to 256 is now cover ed by the wr ite lock , and the pr ocess does not own two lock s on the r egion f r om 0 to 256. This pr ocess is called lock pr omot ion. Dept.of CS&E,SJBIT
Page 30
UNIX SYSTEM PROGRAMMING
10C S 62
Fur ther mor e, if the pr ocess now unlock s the f ile f r om 128 to 480, it will own two wr ite lock s on the f ile: one f r om 0 to 127 and the other f r om 481 to 512. This pr ocess is called lock s pl it t in g .
The pr ocedur e f or setting the mandator y lock s f or U NIX system V3 and V4 ar e: The f ollowing f il e _ l ock .C pr ogr am illustr ates a use of f cnt l for f ile lock ing: #include #include #include #include #include
int main (int ar gc, cnar * ar gv[]) { str uct f lock
fvar ;
int
f de sc ;
while (--ar gc > 0) {
/* do the f ollowing f or each f ile */
if ((f desc=o pen(*++ar gv,O _ R DWR ))==-1 ) { per r or ("o pen"); continue; } fvar .l _ ty pe
= F _ WR LCK ;
fvar .l _ whence = SEEK _ SET; fvar .l _ star t
= 0;
fvar .l _ len
= 0;
/* Attem pt to set an exclusive (wr ite) lock on the entir e f ile */ while (f cntl(f desc, FSETLK ,&fvar )==-1) { /* Set lock f alls, f ind out who has lock ed the f ile */ while (f cntl(f desc,F _ GETLK ,&f var )!=-1 && fvar .l_ ty pe != F _ U NLCK ){ cout<<*ar gv<<”lock ed by”<
= 0;
}/* while ther e ar e lock s set by other pr ocesses */ } /* while set lock un-successf ul */
Dept.of CS&E,SJBIT
Page 31
UNIX SYSTEM PROGRAMMING
10C S 62
Lock the f ile OK . Now pr ocess data in the f ile */ /* Now unlock the entir e f ile */ fvar .l_ ty pe
= F _ U NLCK ;
fvar .l_whence
= SEEK _ SET;
fvar .l _ star t
= 0;
fvar .l _ len
= 0;
if (f cntl(f dosc, F _ SETLK W,&f ear )==-1) per r or ("f cntl"); }
retur n 0; ) /* main */
3.3 Directory File APIs Dir ector y f iles in U NIX and POSIX systems ar e used to help user s in or ganizing their f iles into some str uctur e based on the s pecif ic use of f ile. They ar e also used by the o per ating system to conver t f ile path names to their inode num ber s. Dir ector y f iles ar e cr eated in BSD U NIX and POSIX.1 by mk dir API: #include #include int mk d ir ( const char* path _ name, mode t mode ); 1. The path _ name ar gument is the path name of a dir ector y to be cr eated. 2. The mode ar gument s pecif ies the access per mission f or the owner , gr ou p and other s to be assigned to the f ile. 3. The r etur n value of mk dir is 0 if it succeeds or -1 if it f ails. U NIX System V.3 uses the mk nod API to cr eate dir ector y f iles. U NIX System V.4 sup por ts both the mk d ir and mk nod A P I s f or cr eating dir ector y f iles. The dif f er ence between the two A P I s i s that a dir ector y cr eated by mk nod does not contain the "." and ".." link s. On the other hand, a dir ector y cr eated by mk d ir has the "." and ".." link s
cr eated in one atomic o per ation, and it is r eady to be used. A dir ector y f ile is a r ecor d-or iented f ile, wher e each r ecor d stor es a f ile name and the mode num ber of a f ile that r esides in that dir ector y. Dept.of CS&E,SJBIT
Page 32
UNIX SYSTEM PROGRAMMING
10C S 62
The f ollowing por ta ble f unctions ar e def ined f or dir ector y f ile br owsing. These f unctions ar e def ined in both the and header s. #include #if def ined (BSD) && ! _ POSIX _ SOUR CE #include ty pedef str uct dir ect Dir ent; #else #include ty pedef str uct dir ent Dir ent; #endif DIR * o pend i r (const char * path _ name); Dir ent* r ead d ir (DIR * dir _ f desc); int cl o sed ir (DIR * dir _ f desc); void r ewi nd d i r (DIR * dir _ f desc); The uses of these f unctions ar e: opendir: O pens a dir ector y f ile f or r ead-only. R etur ns a f ile handle DIR * f or f utur e r ef er ence of the f ile. readdir: R eads a r ecor d f r om a dir ector y f ile r ef er enced by di r _ f d e sc and r etur ns that r ecor d inf or mation.
closedir: Closes a dir ector y f ile r ef er enced by di r _f de sc . rewinddir: Resets the f ile pointer to the beginning of the dir ector y f ile r ef er enced by di r _ f d e sc . The next call to r e a d d i r will r ead the f ir st r ecor d f r om the f ile.
U NIX systems su p por t additional f unctions f or r andom access of dir ector y f ile r ecor ds. These functions ar e not su p por ted by POSIX.1: telldir: Retur ns the f ile pointer of a given dir _ f desc. seek dir: Changes the f ile pointer of a given dir _ fdesc to a s pecif ied addr ess.
Dir ector y f iles ar e r emoved by the rmd ir AP I. It s pr ot oty pe is gi v en below: #include int rmd i r (const char* path _ name);
Dept.of CS&E,SJBIT
Page 33
UNIX SYSTEM PROGRAMMING
10C S 62
The f ollowing l i st _ d ir .C pr ogr am illustr ates uses of the mk d ir , o pend ir , r ead d ir , cl o sed ir , and rm di r AP Is: #include #include #include #include #include #include #if def ined (BSD) && ! _ POSIX _ SOUR CE #include ty pedef str uct dir ect Dir ent; #else #include ty pedef str uct dir ent Dir ent; #endif
int main (int ar gc, char * ar gv[ ]) { Dir ent* d p; DIR *
dir _ f desc;
while (--ar gc > 0 ) {
/* do the f ollowing f or each f ile */
if ( !(dir _fdesc = o pendir ( *++ar gv ) )) { if (mk dir ( *ar gv, S _ IR WXU|S _ IR WXG|S _ IR WXO) == -1 ) per r or ( "o pendir " ); continue; } /*scan each dir ector y f ile twice*/ for (int i=0;i < 2 ; i + + ) { for ( int cnt=0; dp=r eaddir ( dir _ f desc );) { if (i) cout << dp->d _ name << endl; if (str cm p( dp->d _ name, ".") && str cm p( dp->d _ name, ".. ") ) cnt++;
/*count how many f iles in dir ector y*/
if (!cnt) { rmdir ( *ar gv ); br eak ;) /* em pty dir ector y */
Dept.of CS&E,SJBIT
Page 34
UNIX SYSTEM PROGRAMMING rewinddir ( dir fdesc );
10C S 62
/ reset pointer f or second r ound */
} closedir ( dir f desc ); } }
3.4 Device File APIs Device f iles ar e used to inter f ace physical devices with a p plication pr ogr ams. S pecif ically, when a pr ocess r eads or wr ites to a device f ile, the k er nel uses the ma jor and minor device num ber s of a f ile to select a device dr iver f unction to car r y out the actual data tr ansf er . Device f iles may be char acter - based or block - based. U NIX systems def ine the mk nod API to create device files. #include #include int mk nod ( const char* path _ name, mode t mode, int device _ id ); 1. The path _ name ar gument is the path name of a dir ector y to be cr eated. 2. The mode ar gument s pecif ies the access per mission f or the owner , gr ou p and other s to be assigned to the f ile. 3. The d evice _ id contains the ma jor and minor device num ber s and is constr ucted in most U NIX systems as follows: The lowest byte of a d evice _ id is set to a minor device num ber and the next byte is set to the ma jor device num ber . For exam ple, to cr eate a block device file called SCSI5 with ma jor and minor num ber s of 15 and 3, r es pectively, and access rights of r ead-wr ite-execute f or ever yone, the mk nod system call is: mk nod("SCSI5", S _ IFBLK | S _ IR WXU | S _ IR WXG | S _ IR WXO, (15<<8) 13);
4. The ma jor and minor device num ber s ar e extended to f our teen and eighteen bits, res pectively. 5. In U NIX, if a calling pr ocess has no contr olling ter minal and it o pens a char acter device f ile, the k er nel will set this device f ile as the contr olling ter minal of the pr ocess. How-ever , if the O _ NOCTTY f lag is set in the o pen call, such action will be su p pr essed. Dept.of CS&E,SJBIT
Page 35
UNIX SYSTEM PROGRAMMING
10C S 62
6. The O _ NO NBLOCK f lag s pecif ies that the o pen call and any su bsequent r ead or wr it e calls to a device f ile should be non block ing to the pr ocess. The f ollowing t e st mk nod .C pr ogr am illustr ates use of the mk nod , o pen , r ead , wr it e , and cl o se APIs on a block device f ile. #include #include #include #include #include #include #include int main( int ar gc, char * ar gv[ ] ) { if (ar gc!=4){ cout << "usage: " << ar gv[0] << " \n”; retur n 0; } int ma jor = atoi( ar gv[2]), minor = atoi( ar gv[3] ); (void) mk nod( ar gv[1], S _ IFCHR | S _ IR WXU | S _ IR WXG | S _ IR WXO, ( ma jor <<8) | minor ); int r c=1, f d = o pen(ar gv[1], O _ R DWR | O _ NO NBLOCK | O _ NOCTTY ); char buf [256]; while ( r c && f d != -1 ) if (( r c = r ead( f d, buf , sizeof ( buf )) ) < 0 ) per r or ( "r ead" ); else if ( r c) cout << buf << endl; close(f d);
}
Dept.of CS&E,SJBIT
Page 36
UNIX SYSTEM PROGRAMMING
10CS62
UNIT - 5 PR OCESS CONTR OL 5.1 Process identif iers
Ever y pr ocess has a unique pr ocess ID, a non negative integer
S pecial pr ocesses : pr ocess ID 0 scheduler pr ocess also k nown as swa p per pr ocess ID 1 init pr ocess init pr ocess never dies ,it’s a nor mal user pr ocess run with su per user pr ivilege pr ocess ID 2 pagedaemon
#include #include pid _ t get pid (void); pid _ t get p pid (void); uid _ t getuid (void); uid _ t geteuid (void); gid _ t getgid (void); gid _ t getegid (void);
Fork f unction
The only way a new pr ocess is cr eated by U NIX k er nel is when an existing pr ocess calls the f or k f unction
#include #include pid _ t f or k (void);
The new pr ocess cr eated by f or k is called child pr ocess
The f unction is called once but r etur ns twice
The r etur n value in the child is 0
The r etur n value in par ent is the pr ocess ID of the new child
The child is a co py of par ent Page 42
Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
Child gets a co py of par ents text, data , hea p and stack
Instead of com pletely co pying we can use COW co py on wr ite technique
10CS62
#include #include "our hdr .h" int glo b = 6; /* exter nal var ia ble in initialized data */ char
buf [ ] = "a wr ite to stdout\n";
int main(void) { int
var ;
/* automatic var ia ble on the stack */ pid _ t pid; var = 88; if (wr ite(STDOUT _ FILE NO, buf , sizeof ( buf )-1) != sizeof ( buf )-1) er r _ sys("wr ite er r or "); pr intf (" bef or e f or k \n"); if ( ( pid = f or k ()) < 0) er r _ sys("f or k er r or "); else if ( pid == 0) {
/* child */ glo b++;
/* modif y var ia bles */
var ++; } else slee p(2); /* par ent */ pr intf (" pid = %d, glo b = %d, var = %d\n", get pid(), glo b, var ); exit(0); } Out put Page 43 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
file shar ing
For k cr eates a du plicate co py of the f ile descr i ptor s o pened by par ent
Ther e ar e two ways of handling descr i ptor s af ter f or k
1. The par ent waits f or the child to com plete 2. Af ter f or k the par ent closes all descr i ptor s that it doesn’t need and the does the same thing Besides o pen f iles the other pr o per ties inher ited by child ar e
Real user ID, gr ou p ID, ef f ective user ID, ef f ective gr ou p ID
Su p plementar y gr ou p ID
Pr ocess gr ou p ID
Session ID
Contr olling ter minal
set-user -ID and set-gr oup-ID
Cur r ent wor k ing dir ector y
Root dir ector y
File mode cr eation mask
Signal mask and dis positions
The close-on-exec f lag f or any o pen f ile descr i ptor s Page 44
Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
Envir onment
Attached shar ed memor y segments
Resour ce limits
10CS62
The dif f er ence between the par ent and child
The r etur n value of f or k
The pr ocess ID
Par ent pr ocess ID
The values of tms _ utime , tms _ stime , tms _ cutime , tms _ ustime is 0 f or child
file lock s set by par ent ar e not inher ited by child
Pending alr ams ar e clear ed f or the child
The set of pending signals f or the child is set to em pty set
The f unctions of f or k
1. A pr ocess can du plicate itself so that par ent and child can each execute dif f er ent sections of code 2. A pr ocess can execute a dif f er ent pr ogr am
vf ork
It is same as f or k
It is intended to cr eate a new pr ocess when the pur pose of new pr ocess is to exec a new pr ogr am
The child r uns in the same addr ess s pace as par ent until it calls either exec or exit
vf or k guar antees that the child r uns f ir st , until the child calls exec or exit
int glo b = 6; /* exter nal var ia ble in initialized data */ int main(void) { int
var ; Page 45
Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
/* automatic var ia ble on the stack */ pid _ t pid; var = 88; pr intf (" bef or e vf or k \n"); if ( ( pid = vf or k ()) < 0) er r _ sys("vf or k er r or "); else if ( pid == 0) {
/* child */
glo b++; /* modif y par ent's var ia bles */ var ++; _exit(0);
/* child ter minates */
} /* par ent */ pr intf (" pid = %d, glo b = %d, var = %d\n", get pid(), glo b, var ); exit(0); }
5.2 exit f unctions
Nor mal ter mination
1. Retur n f r om main 2. Calling exit – includes calling exit handler s 3. Calling _ exit – it is called by exit f unction
A bnor mal ter mination
1. Calling a bor t – SIGABR T 2. When pr ocess r eceives cer tain signals
Exit status is used to notif y par ent how a child ter minated
When a par ent ter minates bef or e the child, the child is inher ited by init pr ocess
If the child ter minates bef or e the par ent then the inf or mation a bout the is o btained by par ent when it executes wait or wait pid
Page 46 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
The inf or mation consists of the pr ocess ID, the ter mination status and amount of CPU time tak en by pr ocess
A pr ocess that has ter minated , but whose par ents has not yet waited f or it, is called a zom bie
When a pr ocess inher ited by init
Init executes one of the wait f unctions to f etch the ter mination status
ter minates it doesn’t become a zom bie
5.3 Wait and waitpid f unctions
When a child id ter minated the par ent is notif ied by the k er nel by sending a SIGCHLD signal
The ter mination of a child is an asynchr onous event
The par ent can ignor e or can pr ovide a f unction that is called when the signal occur s
The pr ocess that calls wait or wait pid can 1. Block 2. Retur n immediately with ter mination status of the child 3. Retur n immediately with an er r or #include #include pid _ t wait (int *statloc); pid _ t wait pid ( pid _ t pid,int *statloc , int o ptions );
Statloc is a pointer to integer
If statloc is not a null pointer ,the ter mination status of the ter minated pr ocess is stor ed in the location pointed to by the ar gument
The integer status r etur ned by the two f unctions give inf or mation a bout exit status, signal num ber and a bout gener ation of cor e f ile
Macr os which pr ovide inf or mation a bout how a pr ocess ter minated
Pr ogr am to demonstr ate the use of the exit status #include "a pue.h" #include Page 47 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
Void pr _ exit(int status) { if (WIFEXITED(status)) pr intf ("nor mal ter mination, exit status = %d\n",WEXITSTATUS(status)); else if (WIFSIG NALED(status)) pr intf ("a bnor mal ter mination, signal num ber = %d%s\n",WTER MSIG(status), #if def WCOR EDUMP WCOR EDUMP(status) ? " (cor e f ile gener ated)" : ""); #else ""); #endif
else if (WIFSTOPPED(status)) pr intf ("child sto p ped, signal num ber = %d\n",WSTOPSIG(status)); }
WIFEXITED
TR UE – if child ter minated nor mally WEXITSTATUS – is used to f etch the lower 8 bits of ar gument child passed to exit or _ exit
WIFSIG NALED
TR UE – if child ter minated a bnor mall y WTER MSIG – is used to f etch the signal num ber that caused ter mination WCOR EDUMP – is tr ue is cor e f ile was gener ated
WIFSTOPPED
TR UE – for a child that is cur r ently sto p ped WSTOPSIG -- is used to f etch the signal num ber that caused child to sto p
5.4 Waitpid
The inter pr etation of pid in wait pid de pends on its value Page 48
Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
1. Pid == -1 – waits f or any child 2. Pid > 0
– waits f or child whose pr ocess ID equals pid
3. Pid == 0 – waits f or child whose pr ocess gr ou p ID equals that of calling pr ocess 4. Pid < -1 – waits f or child whose pr ocess gr ou p ID equals to a bsolute value of pid
Wait pid hel ps us wait f or a par ticular pr ocess
It is non block ing ver sion of wait
It su p por ts jo b contr ol
W NOHA NG
Wait pid will not block if the child s pecif ied is not availa ble
WU NTR ACED
su p por ts jo b contr ol
#include
#include
#include
"our hdr .h"
Int main(void) { pid _ t pid; int
status;
if ( ( pid = f or k ()) < 0) er r _ sys("f or k er r or "); else if ( pid == 0)
/* child */
exit(7); if (wait(&status) != pid) /* wait f or child */ er r _ sys("wait er r or "); Page 49 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
pr _ exit(status); /* and pr int its status */ if ( ( pid = f or k ()) < 0) er r _ sys("f or k er r or "); else if ( pid == 0)
/* child */
a bor t(); /* gener ates SIGABR T */ if (wait(&status) != pid) /* wait f or child */ er r _ sys("wait er r or "); pr _ exit(status); /* and pr int its status */ if ( ( pid = f or k ()) < 0) er r _ sys("f or k er r or "); else if ( pid == 0)
/* child */
status /= 0; /* divide by 0 gener ates SIGFPE */ if (wait(&status) != pid) /* wait f or child */ er r _ sys("wait er r or "); pr _ exit(status); /* and pr int its status */
exit(0); }
5.5 Waitid
#include int waitid(idty pe _ t idty pe, id _ t id, siginf o _ t *inf o p, int o ptions); Retur ns: 0 if OK , 1 on er r or Page 50 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
5.6 wait3 and wait4 f unctions
These f unctions ar e same as wait pid but pr ovide additional inf or mation a bout the resour ces used by the ter minated pr ocess #include #include #include #include pid _ t wait3 (int *statloc ,int o ptions, str uct r usage *r usage ); pid _ t wait4 ( pid _ t pid ,int *statloc ,int o ptions, str uct r usage *r usage );
5.7 Race condition
Page 51 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
Race condition occur s when multi ple pr ocess ar e tr ying to do something with
shar ed data and f inal out come de pends on the or der in which the pr ocesses r un Pr ogr am with r ace condition #include
#include
"our hdr .h"
static void char atatime(char *); int main(void) { pid _ t pid; if ( ( pid = f or k ()) < 0) er r _ sys("f or k er r or "); else if ( pid == 0) { char atatime("out put f r om child\n"); } else { char atatime("out put f r om par ent\n"); } exit(0); } static void char atatime(char *str ) { char int
* ptr ; c;
set buf (stdout, NULL); /* set un buf f er ed */ for ( ptr = str ; c = * ptr ++; ) putc(c, stdout); Page 52 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
} /*alter ed pr ogr am*/ #include
#include
"our hdr .h"
static void char atatime(char *); Int main(void) { pid _ t pid; TELL _ WAIT(); if ( ( pid = f or k ()) < 0) er r _ sys("f or k er r or "); else if ( pid == 0) { WAIT _ PAR E NT();
/* par ent goes f ir st */
char atatime("out put f r om child\n"); } else { char atatime("out put f r om par ent\n"); TELL _ CHILD( pid); } exit(0); } static void char atatime(char *str ) { char int
* ptr ; c;
set buf (stdout, NULL); /* set un buf f er ed */ for ( ptr = str ; c = * ptr ++; ) putc(c, stdout); Page 53 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
}
5.8 exec f unctions
Exec r e places the calling pr ocess by a new pr ogr am
The new pr ogr am has same pr ocess ID as the calling pr ocess
No new pr ogr am is cr eated , exec just r e places the cur r ent pr ocess by a new pr ogr am #include int exec1 ( const char * pathname, const char *ar g0 ,… /*(char *) 0*/); int execv (const char * pathname, char * const ar gv[ ]); int execle (const char * pathname, const char *ar g0 ,… /*(char *) 0, char *const env p[ ] */);
int execve ( const char * pathname, char *const ar gv[ ] , char *const env p [ ]); int execl p (const char *f ilename, const char *ar g0 ,… /*(char *) 0*/); int execv p (const char *f ilename ,char *const ar gv[ ] );
#include
#include
#include
"our hdr .h"
char
*env _ init[ ] =
{ "USER =unk nown", "PATH=/tm p", NULL }; int main(void) { pid _ t pid; if ( ( pid = f or k ()) < 0) er r _ sys("f or k er r or "); else if ( pid == 0) { /* s pecif y pathname, s pecif y envir onment */ if ( execle ("/home/stevens/ bin/echoall", Page 54 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
"echoall", "myar g1", "MY AR G2", (char *) 0, env _ init) < 0) er r _ sys("execle er r or "); } if (wait pid( pid, NULL, 0) < 0) er r _ sys("wait er r or ");
if ( ( pid = f or k ()) < 0) er r _ sys("f or k er r or ");
else if ( pid == 0) { /* s pecif y f ilename, inher it envir onment */ if (execl p("echoall", "echoall", "only 1 ar g", (char *) 0) < 0) er r _ sys("execl p er r or "); } exit(0); }
Changing user IDs and group IDs
Pr ototy pe #include #include int setuid (uid _ t uid); int setgid (gid _ t gid);
Rules
Page 55 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
1. If the pr ocess has su per user pr ivilege, the setuid f unction sets – r eal user ID, ef f ective user ID , saved set-user -ID to uid 2. If the pr ocess doesnot have su per user pr ivilege, but uid equals either r eal user ID or saved set-user -ID, setuid sets only ef f ective user ID to uid 3. If neither of the two conditions is tr ue, er r no is set to EPER M and an er r or is retur ned ID
exec
exec
Set-user -ID bit of f
Set-user -Id bit on
Real user ID
unchanged
unchanged
Ef f ective user ID
unchanged
Set f r om user ID o pr ogr am f ile
Saved set user ID co pied f r om ef f ective
co pied f r om ef f ective
user ID
user ID
ID
Su per user
Un pr ivileged user
Real user ID
Set to uid
unchanged
Ef f ective user ID
Set to uid
Set to uid
Saved set-user ID
Set to uid
unchanged
5.9 setreuid and setregid #include #include int setr euid (uid _ t r uid, uid _ t euid); int setr egid (gid _ t r gid,gid _ t egid);
seteuid and setegid Page 56 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
#include #include int seteuid (uid _ t euid); int setegid (gid _ t egid); Interpreter f iles
Files which begin with a line of the f or m #! pathname [ o ptional ar gument ] most common exam ple : #! / bin/ bash
The actual f ile execed by k er nel is the one s pecified in the pathname
/*exam ple of inter pr eter f ile*/ #!/ bin/awk -f BEGI N { for (i = 0; i < AR GC; i++) pr intf "AR GV[%d] = %s\n", i, AR GV[i] exit }
Uses of inter pr eter f iles
1. They hide the f act that cer tain pr ogr ams ar e scr i pts in some other language 2. They pr ovide an ef f iciency gain 3. They hel p us wr ite shell scr i pts using shells other than / bin/sh
5.10 system f unction
It hel ps us execute a command str ing within a pr ogr am
System is im plemented by calling f or k , exec and waid pid #include int system (const char *cmdstr ing);
Retur n values of system f unction
-1 – if either f or k f ails or wait pid r etur ns an er r or other than EI NTR Page 57
Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
127 -- If exec f ails [as if shell has executed exit ]
ter mination status of shell -- if all thr ee f unctions succeed
#include
#include
#include
#include
int system(const char *cmdstr ing) /* ver sion without signal handling */ { pid _ t pid; int
status;
if (cmdstr ing == NULL) retur n(1); /* always a command pr ocessor with Unix */ if ( ( pid = f or k ()) < 0) { status = -1; /* pr o ba bly out of pr ocesses */
} else if ( pid == 0) {
/* child */ execl("/ bin/sh", "sh", "-c", cmdstr ing, (char *) 0); _exit(127);
/* execl er r or */
} else {
/* par ent */ while (wait pid( pid, &status, 0) < 0) if (er r no != EI NTR ) { status = -1; Page 58
Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
/* er r or other than EI NTR f r om wait pid() */ br eak ; } } retur n(status); }
/*calling system f unction*/ #include
#include
#include
"our hdr .h"
int main(void) { int
status;
if ( (status = system("date")) < 0) er r _ sys("system() er r or "); pr _ exit(status); if ( (status = system("nosuchcommand")) < 0) er r _ sys("system() er r or "); pr _ exit(status); if ( (status = system("who; exit 44")) < 0) er r _ sys("system() er r or "); pr _ exit(status); exit(0); }
5.11 Process accounting
Pr ocess accounting : when ena bled k er nel wr ites an accounting r ecor d each time a pr ocess ter minates
Accounting r ecor ds : 32 bytes of binar y data
Dept . of ISE, SJBIT Dept.of CS&E,SJBIT
Page 59
UNIX SYSTEM PROGRAMMING
10CS62
Str uct acct { char ac _ f lag; char ac _ stat; uid _ t ac _ uid; gid _ t ac _ gid; dev _ t ac _ ttty; time _ t ac _ btime; com p _ t ac _ utime; com p _ t ac _ stime; com p _ t ac _ etime; com p _ t ac _ mem; com p _ t ac _ io; com p _ t ac _ r w; char ac _ comm; }
/* pr og: to gener ate accounting data */
#include #include #include "our hdr .h" #def ine ACCTFILE
"/var /adm/ pacct"
static unsigned long
com pt2ulong(com p _ t);
int main(void) { str uct acct
acdata;
FILE
*f p;
if ( (f p = f o pen(ACCTFILE, "r ")) == NULL) er r _ sys("can't o pen %s", ACCTFILE); Page 60 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
while (f r ead(&acdata, sizeof (acdata), 1, f p) == 1) {
pr intf ("%-*.*s e = %6ld, char s = %7ld, " "stat = %3u: %c %c %c %c\n", sizeof (acdata.ac _ comm), sizeof (acdata.ac _ comm), acdata.ac _ comm, com pt2ulong(acdata.ac _ etime), com pt2ulong(acdata.ac _ io), (unsigned char )
acdata.ac _ stat,
#if def ACOR E /* SVR 4 doesn't def ine ACOR E */ acdata.ac _ f lag & ACOR E ? 'D' : ' ', #else ' ', #endif #if def AXSIG /* SVR 4 doesn't def ine AXSIG */ acdata.ac _ f lag & AXSIG ? 'X' : ' ', #else ' ', #endif acdata.ac _ f lag & AFOR K ? 'F' : ' ', acdata.ac _ f lag & ASU ? 'S' : ' '); } if (f er r or (f p)) er r _ sys("r ead er r or "); exit(0); } Page 61 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
static unsigned long com pt2ulong(com p _ t com ptime) /* conver t com p _ t to unsigned long */ { unsigned long val; int
ex p;
val = com ptime & 017777; /* 13- bit f r action */ ex p = (com ptime >> 13) & 7; /* 3- bit ex ponent (0-7) */ while (ex p-- > 0) val *= 8; retur n(val); }
5.12 User identif ication
To o btain the login name #include char *getlogin (void); Pr ocess times #include clock _ t times (str uct tms * buf ); Str uct tms { clock _ t tms _ utime; clock _t tms _ stime; clock _ t tms _ cutime; clock _ t tms _ cstime; }
Page 62 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
#include #include "our hdr .h" static void pr _ times (clock _ t, str uct tms *, str uct tms *); static void
do _ cmd(char *);
int main (int ar gc, char *ar gv[ ]) {
int
i;
for (i = 1; i < ar gc; i++) do _ cmd(ar gv[i]); /* once f or each command-line ar g */ exit(0); } static void do _ cmd (char *cmd) /* execute and time the "cmd" */ { str uct tms
tmsstar t, tmsend;
clock _ t
star t, end;
int
status;
f pr intf (stder r , "\ncommand: %s\n", cmd); if ( (star t = times(&tmsstar t)) == -1) /* star ting values */ er r _ sys("times er r or "); if ( (status = system(cmd)) < 0) /* execute command */ er r _ sys("system() er r or "); if ( (end = times(&tmsend)) == -1) /* ending values */ er r _ sys("times er r or "); pr _ times(end-star t, &tmsstar t, &tmsend); Page 63 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
pr _ exit(status); } static void pr _ times (clock _ t r eal, str uct tms *tmsstar t, str uct tms *tmsend) { static long clk tck = 0; if (clk tck == 0) /* f etch clock tick s per second f ir st time */ if ( (clk tck = sysconf ( _ SC _ CLK _ TCK )) < 0) er r _ sys("sysconf er r or "); f pr intf (stder r , " r eal: %7.2f \n", r eal / (dou ble) clk tck ); f pr intf (stder r , " user : %7.2f \n",(tmsend->tms _ utime - tmsstar t> tms _ utime) / (dou ble) clk tck ); f pr intf (stder r , " sys: %7.2f \n", (tmsend->tms _ stime - tmsstar t->tms _ stime) / (dou ble) clk tck ); f pr intf (stder r , " child user : %7.2f \n",(tmsend->tms _ cutime - tmsstar t-> tms _ cutime) / (dou ble) clk tck ); f pr intf (stder r , " child sys:
%7.2f \n", (tmsend->tms _ cstime - tmsstar t-> tms _ cstime) /
(dou ble) clk tck ); }
5.12 Process groups
A pr ocess gr ou p is a collection of one or mor e pr ocesses.
Each pr ocess gr ou p has a unique pr ocess gr ou p ID.
Pr ocess gr ou p IDs ar e similar to pr ocess IDs---they ar e positive integer s and they can be stor ed in a pid _ t data ty pe.
The f unction get pgr p r etur ns the pr ocess gr ou p ID of the calling pr ocess.
Each pr ocess gr ou p can have a pr ocess leader . The leader is identif ied by having its pr ocess gr ou p ID equal its pr ocess ID. #include Page 64
Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
#include pid _ t get pgr p (void);
It is possi ble f or a pr ocess gr ou p leader to cr eate a pr ocess gr ou p, cr eate pr ocesses in the gr ou p, and then ter minate.
The pr ocess gr ou p still exists, as long as ther e is at least one pr ocess in the gr ou p, regar dless whether the gr ou p leader ter minates or not
pr ocess gr ou p lif etime — the per iod of time that begins when the gr ou p is cr eated and ends when the last pr ocess in the gr ou p leaves the gr ou p
A pr ocess joins an existing pr ocess gr ou p, or cr eates a new pr ocess gr ou p by calling set pgid. #include #include int set pgid ( pid _ t pid , pid _ t p g id );
This sets the pr ocess gr ou p ID to pgid of the pr ocess pid. If the two ar guments ar e equal, the pr ocess s pecif ied by pid becomes a pr ocess gr ou p leader .
A pr ocess can set the pr ocess gr ou p ID of only itself or one of its childr en. If pid is 0, the pr ocess ID of the caller is used. Also if p g id is 0, the pr ocess ID s pecif ied by pid is used as the pr ocess gr ou p ID.
In most jo b-contr ol shells this f unction is called af ter a f or k to have the par ent set the pr ocess gr ou p ID of the child, and to have the child set its own pr ocess gr ou p ID.
5.12 SESSIONS
A Session is a collection of one or mor e gr ou ps.
The pr ocesses in a pr ocess gr ou p ar e usually gr ou ped together into a pr ocess gr ou p by a shell pi peline.
A pr ocess esta blishes a new session by calling the set sid function. #include #include pid _ t setsid (void) Page 65
Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
If the calling pr ocess is not a pr ocess gr ou p leader , this f unction cr eates a new session. Thr ee things ha p pen: 1. The pr ocess becomes the session leader of this new session. 2. The pr ocess becomes the pr ocess gr ou p leader of a new pr ocess gr ou p. The new pr ocess gr ou p ID is the pr ocess ID of the calling pr ocess. 3. The pr ocess has no contr olling ter minal.
Contr olling ter minal
char acter istics of sessions and pr ocess gr ou ps
A session can have a single contr olling ter minal.
The session leader that esta blishes the connection to the contr olling ter minal is called the contr olling pr ocess.
The pr ocess gr ou ps within a session can be divided into a single f or egr ound pr ocess gr ou p and one or mor e back gr ound pr ocess gr ou ps.
If a session has a contr olling ter minal, then it has a single f or egr ound pr ocess gr ou p, and all other pr ocess gr ou ps in the session ar e back gr ound pr ocess gr ou ps.
Whenever we ty pe our ter minal’s inter r u pt k ey or quit k ey this causes either the inter r u pt signal or the quit signal to be sent to all pr ocesses in the f or egr ound pr ocess gr ou p.
If a modem disconnect is detected by the ter minal inter f ace, the hang-u p signal is sent to the contr olling pr ocess
5.13 tcgetpgrp and tcsetpgrp Functions
We need to tell the k er nel which pr ocess gr ou p is the f or egr ound pr ocess gr ou p, so that the ter minal device dr iver k nows wher e to send the ter minal in put and the ter minal- gener ated signals #include #include pid _ t tcget pgr p(int f il ed e s); int tcset pgr p(int f il ed e s, pid _ t p g r pid );
Page 66 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
The f unction t c g et p g r p r etur ns the pr ocess gr ou p ID of the f or egr ound pr ocess gr ou p associated with the ter minal o pen on f iledes.
If the pr ocess has a contr olling ter minal, the pr ocess can call tcset pgr p to set the foregr ound pr ocess gr ou p ID to pgr pid..
5.14 Job Control
To allow us to star t multi ple jo bs f r om a single ter minal and contr ol which jo bs can access the ter minal and which jo bs ar e to be r un in the back gr ound.
It r equir es 3 f or ms of su p por t:
A shell that su p por ts jo b contr ol.
The ter minal dr iver in the k er nel must su p por t jo b contr ol.
Su p por t f or cer tain jo b-contr ol signals
A jo b is just a collection of pr ocesses, of ten a pi peline of pr ocesses.
When we star t a back gr ound jo b, the shell assigns it a jo b identif ier and pr ints one or mor e pr ocess IDs.
$ mak e all > Mak e.out & [1]
1475
$ pr *.c | l pr & [2]
1490
$
just pr ess R ETUR N
[2] + Done
pr *.c | l pr &
[1] + Done
mak e all > Mak e.out &
The r eason why we have to pr ess R ETUR N is to have the shell pr int its pr om pt. The shell doesn’t pr int the changed status of back gr ound jo bs at any r andom time -- only r ight bef or e it pr ints its pr om pt, to let us enter a new command line.
Enter ing the sus pend k ey (Ctr l + Z) causes the ter minal dr iver to send the SIGTSTP signal to all pr ocesses in the f or egr ound pr ocess gr ou p. The ter minal dr iver r eally look s f or 3 s pecial char acter s, which gener ate signals to the f or egr ound pr ocess gr ou p:
•
The inter r u pt char acter gener ates SIGI NT Page 67
Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
•
The quit char acter gener ates SIGQUIT
•
The sus pend char acter gener ates SIGTSTP
10CS62
PR OGR AM $cat tem p.f oo & [1]
star t in back gr ound, but It’ll r ead
1681
$
we pr ess R ETUR N
[1] + Sto p ped (tty in put)
cat > tem p.f oo &
$ f g %1
br ing jo b num ber 1 to f or egr ound
cat > tem p.f oo
the shell tells us which jo b is now in the f or egr ound
hello, wor ld
enter one line
^D
ty pe our end-of -file
$ cat tem p.f oo
fr om standar d in put
check that the one line put into the f ile
hello, wor ld Page 68 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
What ha p pens if a back gr ound jo b out puts to the contr olling ter minal?
This o ption we can allow or disallow. Nor mally we use the stty(1) command to change this o ption.
$ cat tem p.f oo & [1]
execute in back gr ound
1719
$ hello, wor ld
the out put f r om the back gr ound a p pear s af ter the pr om pt we pr ess r etur n
[1] + Done
cat tem p.f oo &
$ stty tosto p
disa ble a bility of back gr ound jo bs to out put to contr olling ter minal
[1]
1721
$
we pr ess r etur n and f ind the jo b is sto p ped
[1] + Sto p ped(tty out put)
cat tem p.f oo &
Shell Execution Of Pr ogr ams
Bour ne shell doesn’t su p por t jo b contr ol ps –x j gives the f ollowing out put
PPID 1
PID
PGID
SID
TPGID COMMA ND
163
163
163
163
-sh
163 168
163
163
163
ps
Both the shell and the ps command ar e in the same session and f or egr ound pr ocess gr ou p(163). The par ent of the ps command is the shell.
A pr ocess doesn’t have a ter minal pr ocess contr ol gr ou p.
A pr ocess belongs to a pr ocess gr ou p, and the pr ocess gr ou p belongs to a session. The session may or may not have a contr olling ter minal.
The f or egr ound pr ocess gr ou p ID is an attr i bute of the ter minal, not the pr ocess. If ps f inds that the session does not have a contr olling ter minal, it pr ints -1. If we execute the command in the back gr ound, Ps –x j & Page 69
Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
The only value that changes is the pr ocess ID. ps –x j | cat1
PPID
PID
PGID
1
163
163
SID 163
TPGID
COMMA ND
163
-sh
163
200
163
163
163
cat1
200
201
163
163
163
ps
The last pr ocess in the pi peline is the child of the shell, and the f ir st pr ocess in the pi peline is a child of the last pr ocess.
If we execute the pi peline in the back gr ound ps –x j | cat1 &
Only the pr ocess IDs change.
Since the shell doesn’t handle jo b contr ol, the pr ocess gr ou p ID of the back gr ound pr ocesses r emains 163, as does the ter minal pr ocess gr ou p ID.
Orphaned process groups
We k now that a pr ocess whose par ent ter minates is called an or phan and is inher ited by the init pr ocess.
Sometimes the entir e pr ocess gr ou ps can be or phaned.
This is a jo b-contr ol shell. The shell places the f or egr ound pr ocess in its own pr ocess gr ou p( 512 in the exam ple) and the shell stays in its own pr ocess gr ou p(442). The child inher its the pr ocess gr ou p of its par ent(512). Af ter the f or k ,
The par ent slee ps f or 5 seconds. This is the (im per f ect) way of letting the child execute bef or e the par ent ter minates
The child esta blishes a signal handler f or the hang-u p signal (SIGHUP). This is so we can see if SIGHUP is sent to the child.
The child itself the sto p signal(SIGTSTP) with the k ill f unction.
When the par ent ter minates, the child is or phaned, so the child’s par ent pr ocess ID becomes 1, the init pr ocess ID.
At this point the child is now a mem ber of an or phaned pr oce s s g r ou p. Page 70
Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
Since the pr ocess gr ou p is or phaned when the par ent ter minates, it is r equir ed that ever y pr ocess in the newly or phaned pr ocess gr ou p that is sto p ped be sent the hang-up signal (SIGHUP) f ollowed by the continue signal.
This causes the child to be continued, af ter pr ocessing the hang-u p signal. The def ault action f or the hang-u p signal is to ter minate the pr ocess, which is why we have to pr ovide a signal handler to catch the signal
Cr eating an or phaned pr ocess gr ou p #include
#include
#include
#include
#include
"our hdr .h"
static void
sig _ hu p(int);
static void
pr _ ids(char *);
int main(void) { Page 71 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
char
10CS62
c;
pid _ t pid; pr _ ids(" par ent"); if ( ( pid = f or k ()) < 0) er r _ sys("f or k er r or "); else if ( pid > 0) {
/* par ent */ slee p(5);
exit(0); } else {
/* child */ pr _ ids("child"); signal(SIGHUP, sig _ hu p);
/* esta blish signal handler */ kill (get pid(), SIGTSTP);
pr _ ids("child");
/* this pr ints only if we'r e continued */ if (r ead(0, &c, 1) != 1) pr intf ("r ead er r or f r om contr ol ter minal,er r no = %d\n", er r no);
exit(0);
} } static void
sig _ hu p (int signo)
{ pr intf ("SIGHUP r eceived, pid = %d\n", get pid()); retur n; } static void pr _ ids (char *name) { pr intf ("%s: pid = %d, p pid = %d, pgr p = Page 72 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
d\n", name, get pid(), get p pid(), get pgr p()); ff lush(stdout); } /* OUTPUT $ a.out Par ent: pid = 512, p pid=442, pgr p = 512 Child: par ent = 513, p pid = 512, pgr p = 512 $ SIGHUP r eceived, pid = 513 Child: pid = 513 , p pid = 1, pgr p = 512 Read er r or f r om contr ol ter minal, er r no = 5 */
The par ent pr ocess ID of the child has become 1.
Af ter calling pr _ ids in the child, the pr ogr am tr ies to r ead f r om standar d in put. When the back gr ound pr ocess gr ou p tr ies to r ead f r om its contr olling ter minal, SIGTTI N is gener ated f r om the back gr ound pr ocess gr ou p.
The child becomes the back gr ound pr ocess gr ou p when the par ent ter minates, since the par ent was executed as a f or egr ound jo b by the shell
Page 73 Dept.of CS&E,SJBIT
UNIX SYSTEM PROGRAMMING
10CS62
UNIT – 6 SIGNALS AND DEAMON PR OCESSES 6.1. Introduction
1. Signals ar e tr igger ed by events and ar e posted on a pr ocess to notif y it that some thing has ha p pened and r equir es some action. 2. Signals can be gener ated f r om a pr ocess, a user , or the U NIX k er nel. 3. Exam ple:a. A pr ocess per f or ms a divide by zer o or der ef er ences a NULL pointer . b. A user hits or k ey at the key boar d. 4. A par ent and child pr ocesses can send signals to each other f or pr ocess synchr onization. 5. Thus, signals ar e the sof twar e ver sion of har dwar e inter r u pts. 6. Signals ar e def ined as integer f lags in the header f ile. 7. The f ollowing ta ble 6.1 lists the POSIX – def ined signals f ound in most U NIX systems. Name
Descr i ption
Def ault action
SIGALR M timer ex pir ed (alar m)
ter minate
SIGABR T
a bnor mal ter mination (a bor t)
ter minate+cor e
SIGFPE
ar ithmetic exce ption
ter minate+cor e
SIGHUP
contr olling ter minal hangup
ter minate
SIGILL
illegal machine instr uction
ter minate+cor e
SIGI NT
ter minal inter r u pt char acter or k eys
ter minate
SIGK ILL
kill a pr ocess, k ill -9 < pid> command.
ter minate
SIGPIPE
wr ite to pi pe with no r eader s
ter minate
SIGQUIT
ter minal quit char acter
ter minate+cor e
SIGSEGV
segmentation f ault - invalid memor y r ef er ence
ter minate+cor e
SIGTER M ter minate pr ocess, k ill < pid> command
Dept.of CS&E,SJBIT
ter minate
Page 74
UNIX SYSTEM PROGRAMMING
10CS62
SIGUSR 1
user -def ined signal
ter minate
SIGUSR 2
user -def ined signal
ter minate
SIGCHLD change in status of child
ignor e
SIGCO NT continue sto p ped pr ocess
continue/ignor e
SIGSTOP
stop a pr ocess execution
sto p pr ocess
SIGTTI N
sto p a back gr ound pr ocess when it read f r om its contr ol tty
sto p pr ocess
SIGTSTP
sto p a pr ocess execution by the ctr l-z k ey
sto p pr ocess
SIGTTOU
sto p a back gr ound pr ocess when it wr ites to its contr ol sto p pr ocess tty
8. When a signal is sent to a pr ocess, it is pend ing on the pr ocess to handle it. 9. The pr ocess can r eact to signals in one of the thr ee ways. a. Acce pt the def ault action of the signal – most signals ter minate the pr ocess. b. Ignor e the signal. c. Invok e a user def ined f unction – The f unction is called si g nal hand er r out ine and the signal is said to be cau g ht when the f unction is called. If the f unction f inishes execution without ter minating the pr ocess, the pr ocess will continue execution f r om the point it was inter r u pted by the signal. 10. Most signals can be caught or ignor ed exce pt the SIGK ILL and SIGSTOP signals. 11. A com panion signal to SIGSTOP is SIGCO NT, which r esumes a pr ocess execution af ter it has been sto p ped, both SIGSTOP and SIGCO NT signals ar e used f or jo b contr ol in U NIX. 12. A pr ocess is allowed to ignor e cer tain signals so that it is not inter r u pted while doing cer tain mission – cr itical wor k . 13. Exam ple:- A DBMS pr ocess u pdating a data base f ile should not be inter r u pted until it is f inished, else data base f ile will be cor r u pted, it should r estor e signal handling actions f or signals when f inished mission – cr itical wor k . 14. Because signals ar e gener ated asynchr onously to a pr ocess, a pr ocess may s pecif y a per signal handler f unction, these f unction would then be called when their cor r es ponding signals ar e caught. 15. A common pr actice of a signal handler f unction is to clean u p a pr ocess wor k envir onment, such as closing all in put – out put f iles, bef or e ter minating gr acef ully.
Dept.of CS&E,SJBIT
Page 75
UNIX SYSTEM PROGRAMMING
10CS62
6.2. The UNIX K ernel Supports of Signals 1. In Unix System V.3, each entr y in the k er nel pr ocess ta ble slot has an ar r ay of signal flags, one f or each def ined in the system. 2. When a signal is gener ated f or a pr ocess, the k er nel will set the cor r es ponding signal flag in the pr ocess ta ble slot of the r eci pient pr ocess. 3. If the r eci pient pr ocess is aslee p (waiting a child to ter minate or executing pau se API) the k er nel will awak en the pr ocess by scheduling it. 4. When the r eci pient pr ocess r uns, the k er nel will check the pr ocess U-ar ea that contains an ar r ay of signal handling s pecif ications, wher e each entr y of the ar r ay cor r es ponds to a signal def ined in the system. 5. The k er nel will consult the ar r ay to f ind out how the pr ocess will r eact to the pending signal. 6. If the ar r ay entr y contains a zer o value, the pr ocess will acce pt the def ault action of the signal, and the k er nel will discar d it. 7. If the ar r ay entr y contains a one value, the pr ocess will ignor e the signal. 8.
Finally, if the ar r ay entr y contains any other value, it is used as the f unction pointer for a used def ined signal hander r outine.
9. The k er nel will setu p the pr ocess to execute the f unction immediately, and the pr ocess will r etur n to its cur r ent point of execution (or to some other place if signal hander does a long jum p), if the signal hander does not ter minate the pr ocess. 10. If ther e ar e dif f er ent signals pending on a pr ocess, the or der in which they ar e sent to a r eci pient pr ocess in undef ined. 11. If multi ple instances of a signal ar e pending on a pr ocess, it is im plementation – de pendent on whether a single instance or multi ple instances of the signal will be deliver ed to the pr ocess. 12. In U NIX System V.3, each signal f lag in a pr ocess ta ble slot r ecor ds only whether a signal is pending, but not how many of them ar e pr esent.
6.3. signal 1. All U NIX systems and A NSI – C su p por t the signal API, which can be used to def ine the per -signal handing method. 2. The f unction pr ototy pe of the signal is: Dept.of CS&E,SJBIT
Page 76
UNIX SYSTEM PROGRAMMING
10CS62
#include void (*signal (int signal _ num, void (*handler )(int))(int); si g nal _ num is the signal identif ier lik e SIGI NT or SIGTER M def ined in the . hand l er is the f unction pointer of a user def ined signal handler f unction. This f unction should tak e an integer f or mal ar gument and does not r etur n any value.
3. Exam ple below attem pts to catch the SIGTER M, ignor es the SIGI NT, and acce pts the def ault action of the SIGSEGV signal. 4. The pause API sus pends the calling pr ocess until it is inter r u pted by a signal and the cor r es ponding signal handler does a r etur n: #include #include void catch _ sig(int sig _ num) { signal(sig _ sum, catch _ sig); cout << “catch _ sig:” << sig _ num << endl; } int main() { signal(SIGTER M, catch _ sig); signal(SIGI NT, SIG_IG N); signal(SIGSEGV, SIG _ DFL); pause(); // wait f or signal inter r u ption } 5. The SIG _ IG N and SIG _ DFL ar e manif est constants def ined in #def ine SIG _ DFL void (*)(int)0 // Def ault action #def ine SIG _ IG N void (*)(int)1 // Ignor e the signal 6. The r etur n value of signal API is the pr evious signal handler f or the signal. 7. U NIX system V.3 and V.4 su p por t the sigset API, which has the same pr ototy pe and similar use a signal. #include void (*sigset (int signal _ num, void (*handler )(int))(int); the sigset ar guments and r etur n value is the same as that of signal.
Dept.of CS&E,SJBIT
Page 77
UNIX SYSTEM PROGRAMMING
10CS62
8. Both the f unctions set signal handling methods f or any named signal; but, signal API is unr elia ble and sigset is r elia ble. 9. This means that when a signal is set to be caught by a signal handler via sigset, when multi ple instances of the signal arr ive one of them is handled while other instances ar e block ed. Fur ther , the signal handler is not r eset to SIG _ DFT when it is invok ed. 6.4. Signal Mask
1. Each pr ocess in U NIX or POSIX.1 system has signal mask that def ines which signals ar e block ed when gener ated to a pr ocess. 2. A block ed signal de pends on the r eci pient pr ocess to un block it and handle it accor dingly. 3. If a signal is s pecif ied to be ignor ed and block ed, it is im plementation de pendent on whether the signal will be discar ded or lef t pending when it is sent to the pr ocess. 4. A pr ocess initially inher its the par ent’s signal mask when it is cr eated, but any pending signals f or the par ent pr ocess ar e not passed on. 5. A pr ocess may quer y or set its signal mask via the sig pr ocmask API: #include int sig pr ocmask (int cmd, const sigset _ t *new _ mask , sigset _ t *old _ mask ); new _ ma sk def ines a set of to be set or r eset in a calling pr ocess signal mask . cm d s pecif ies how the new _ mask value is to be used by the API. The possi ble values cmd ar e:
cmd value
Meaning Over r ides the calling pr ocess signal mask with the value s pecif ied in SIG _ SETMASK the new _ mask ar gument Adds the signals s pecif ied in the new _ mask ar gument to the calling SIG _ BLOCK pr ocess signal mask Removes the signals s pecif ied in the new _ mask ar gument to the SIG _ U NBLOCK calling pr ocess signal mask 6. If the actual ar gument to new _ mask ar gument is a NULL pointer , the cmd ar gument will be ignor ed, and the cur r ent pr ocess signal mask will not be alter ed. 7. The old _ mask ar gument is the addr ess of a si g set _ t var ia ble that will be assigned the calling pr ocess’s or iginal signal mask pr ior to a si g pr ocma sk call. If the actual ar gument to old _ mask is a NULL pointer , no pr evious signal mask will be r etur ned. 8. The r etur n value of sig pr ocmask call is zer o if it succeeds or -1 if it f ails.
Dept.of CS&E,SJBIT
Page 78
UNIX SYSTEM PROGRAMMING
10CS62
9. The segset _ t is a data ty pe def ined in . It contains a collection of bit f lags, with each bit f lag r e pr esenting one signal def ined in the system. 10. The BSD U NIX and POSIX.1 def ine a set of API k nown as si g set o ps f unctions, which set, r eset, and quer y the pr esence of signals in a sigset _ t ty ped var ia ble. #include int sigem ptyset(sigset _ t *sigmask ); int sigaddset(sigset _ t *sigmask , const int signal _ num); int sigdelset(sigset _ t *sigmask , const int signal _ num); int sigf illset(sigset _ t sigmask ); int sigismem ber (const sigset _ t *sigmask , const int signal _ num); 11. The sigem ptyset API clear s all signal f lags in the sigmask ar gument. 12. The sigaddset API sets the f lag cor r es ponding to the signal _ num signal in sigmask . 13. The sigdelset API clear s the f lag cor r es ponding to the signal _ num signal in sigmask . 14. The sigf illset API sets all the f lags in the sigmask . 15. The r etur n value of the sigem ptyset, sigaddset, sigdelset, and sigf illset calls is zer o if the call succeed or -1 if they f ail. 16. The sigismem ber API r etur ns 1 if the f lag cor r es ponding to the signal _ num signal in the sigmask is set, zer o if not set, and -1 if the call f ails. 17. The f ollowing exam ple check s whether the SIGI NT signal is pr esent in a pr ocess signal mask and adds it to the mask if it is not ther e. Then clear s the SIGSEGV signal fr om the pr ocess signal mask . #include #include int main() { sigset _ t sigmask ; sigem ptyset(&sigmask ); /*initialize set*/ if (sig pr ocmask (0, 0, &mask ) == -1) { /*get cur r ent signal mask */ per r or (“sig pr ocmask ”); exit(1); } else sigaddset(&sigmask , SIGI NT); /*set SIGI NT f lag*/ sigdelset(&sigmask , SIGSEGV); /*clear SIGSEGV f lag*/ if (sig pr ocmask (SIG _ SETMASK , &sigmask , 0) == -1) per r or (“sig pr ocmask ”); /*set a new signal mask */ }
Dept.of CS&E,SJBIT
Page 79
UNIX SYSTEM PROGRAMMING
10CS62
18. If ther e ar e multi ple instances of the same signal pending f or the pr ocess, it is im plementation de pendent whether one or all of those instances will be deliver ed to the pr ocess. 19. A pr ocess can quer y which signals ar e pending f or it via the sig pending API #include int sig pending(sigset _ t *sigmask ); sigmask is assigned the set of signals pending f or the calling pr ocess by the API. sig pending r etur ns a zer o if it succeeds and a -1 value if it f ails. 20. U NIX system V.3 and V.4 su p por t the f ollowing APIs as sim plif ied means f or signal mask mani pulation. #include int sighold(int signal _ num); int sigr else(int signal _ num); int sigignor e(int signal _ num); int sig pause(int signal _ num); 21. The sighold API adds the named signal signal _ num to the calling pr ocess signal mask . 22. The sigr else API r emoves the named signal signal _ num to the calling pr ocess signal mask . 23. The sigignor e API sets the signal handling method f or the named signal signal _ num to SIG _ DFT. 24. The sig pause API r emoves the named signal signal _ num f r om the calling pr ocess signal mask and sus pends the pr ocess until it is inter r u pted by a signal.
6.5 sigaction 1. The sigaction API is a r e placement f or the signal API in the latest U NIX and POSIX systems. 2. The sigaction API is called by a pr ocess to set u p a signal handling method f or each signal it wants to deal with. 3. sigaction API r etur ns the pr evious signal handling method f or a given signal. 4. The sigaction API pr ototy pe is: #include int sigaction(int signal _ num, str uct sigaction *action, str uct sigaction *old _ action);
Dept.of CS&E,SJBIT
Page 80
UNIX SYSTEM PROGRAMMING
10CS62
5. The str uct sigaction data ty pe is def ined in the header as: str uct sigaction { void (*sa _ handler )(int); sigset _ t sa _ mask ; int sa _ f lag; }; 6. The sa _ handler f ield can be set to SIG _ IG N, SIG _ DFL, or a user def ined signal handler f unction. 7. The sa _ mask f ield s pecif ies additional signals that pr ocess wishes to block when it is handling signal _ num signal. 8. The signal _ num ar gument designates which signal handling action is def ined in the act ion ar gument. 9. The pr evious signal handling method f or signal _ num will be r etur ned via the old _ action ar gument if it is not a NULL pointer . 10. If action ar gument is a NULL pointer , the calling pr ocess’s existing signal handling method f or signal _ num will be unchanged. 11. The f ollowing C pr ogr am illustr ates the use of sigaction: #include #include #include #include void callme ( int sig _ num ) { cout << “catch signal:” << sig _ num << endl; } int main ( int ar gc, char *ar gv[] ) { sigset _ t sigmask ; str uct sigaction action, old _ action; sigem ptyset(&sigmask ); if ( sigaddset( &sigmask , SIGTER M) == -1 || sig pr ocmask ( SIG _ SETMASK , &sigmask , 0) == -1) per r or (“Set signal mask ”); sigem ptyset( &action.sa _ mask );
Dept.of CS&E,SJBIT
Page 81
UNIX SYSTEM PROGRAMMING
10CS62
sigaddset( &action.sa _ mask , SIGSEGV); action.sa _ handler = callme; action.sa _ f lags = 0; if (sigaction (SIGI NT, &action, &old _ action) == -1) per r or (“sigaction”); pause(); /* wait f or signal inter r u ption*/ cout << ar gv[0] << “exits\n”; } 12. In the a bove exam ple, the pr ocess signal mask is set with SIGTER M signal. The pr ocess then def ines a signal handler f or the SIGI NT signal and also s pecif ies that the SIGSEGV signal is to be block ed when the pr ocess is handling the SIGI NT signal. The pr ocess then ter minates its execution via the pause API. 13. The out put of the a bove pr ogr am would be as: % cc sigaction.c –o sigaction % ./sigaction & [1] 495 % k ill –I NT 495 catch signal: 2 sigaction exits [1] Done sigaction 14. The sa _ f lag f ield of the str uct sigaction is used to s pecif y s pecial handling f or cer tain signals. 15. POSIX.1 def ines only two values f or the sa _ f lag: zer o or SA _ NOCHLDSTOP. 16. The SA _ NOCHLDSTOP f lag is an integer liter al def ined in the header and can be used when signal _ num is SIGCHLD. 17. The ef f ect of the SA _ NOCHLDSTOP f lag is that the k er nel will gener ate the SIGCHLD signal to a pr ocess when its child pr ocess has ter minated, but not when the child pr ocess has been sto p ped. 18. If the sa _ f lag value is set to zer o in sigaction call f or SIGCHLD, the k er nel will send the SIGCHLD signal to the calling pr ocess whenever its child pr ocess is either ter minated or sto p ped. 19. U NIX S ystem V.4 def ines additional f lags f or the sa _ f lags f ield. These f lags can be used to s pecif y the U NIX System V.3 style of signal handling method: sa _ f lags value SA _ R ESETHA ND
Ef f ect on handling signal _ num If signal _ num is caught, the sa _ handler is set to SIG _ DFL bef or e the signal handler f unction is called, and signal _ num will not be
Dept.of CS&E,SJBIT
Page 82
UNIX SYSTEM PROGRAMMING
SA _ R ESTAR T
10CS62
added to the pr ocess signal mask when the signal handler f unction is executed. If a signal is caught while a pr ocess is executing a system call, the ker nel will r estar t the system call af ter the signal handler r etur ns. If this f lag is not set in the sa _ f lags, af ter the signal handler r etur ns, the system call will be a bor ted with a r etur n value of -1 and will set er r no to EI NTR
6.6. The SIGCHLD Signal and the waitpid API 1. When a child pr ocess ter minates or sto ps, the k er nel will gener ate a SIGCHLD signal to its par ent pr ocess. 2. De pending u pon how the par ent sets u p signal handling of the SIGCHLD signal, dif f er ent events may occur : a. Par ent acce pts the def ault action of the SIGCHLD signal: The SIGCHLD signal does not ter minate the par ent pr ocess. It af f ects only the par ent pr ocess if it ar r ives at the same time the par ent pr ocess is sus pended by the wait pid system call. In this case, the par ent pr ocess is awak ened, the API will r etur n child’s exit status and pr ocess ID to the par ent, and the k er nel will clear u p the pr ocess ta ble slot allocated f or the child pr ocess. Thus, with this setu p, a par ent pr ocess can call wait pid API r e peatedly to wait f or each child it cr eated. b. Par ent ignor es the SIGCHLD signal: The SIGCHLD signal will be discar ded, and the par ent will not be distur bed, even if it is executing the wait pid system call. The ef f ect of this setu p is that if the par ent calls wait pid API, the API will sus pend the par ent until all its child pr ocesses have ter minated. Fur ther mor e, the child pr ocess ta ble slots will be clear ed by the k er nel, and the API will r etur n -1 value to the par ent pr ocess. c. Pr ocess catches the SIGCHLD signal: The signal handler f unction will be called in the par ent whenever the child pr ocess ter minates. Fur ther mor e, if the SIGCHLD signal ar r ives while the par ent pr ocess is executing the wait pid system call, af ter the signal handler r etur ns, the wait pid API may be r estar ted to collect the child exit status and clear its pr ocess ta ble slot. On the other hand, the API may be a bor ted and the child pr ocess ta ble slot not f r eed, de pending u pon the par ent setu p of the signal action f or the SIGCHLD signal. 6.7. The sigset jmp and siglong jmp APIs
1. The sigset jm p and siglong jm p APIs have similar f unctions as their cor r es ponding set jm p and long jm p APIs. 2. The sigset jm p and siglong jm p APIs ar e def ined in POSIX.1 and on most U NIX systems that su p por t signal mask .
Dept.of CS&E,SJBIT
Page 83
UNIX SYSTEM PROGRAMMING
10CS62
3. The f unction pr ototy pes of the APIs ar e: #include int sigset jm p ( sig jm p buf env, int save _ sigmask ); int siglong jm p ( sig jm p buf env, int r et _ val ); 4. The sigset jm p and siglong jm p ar e cr eated to su p por t signal mask pr ocessing. S pecif ically, it is im plementation de pendent on whether a pr ocess signal mask is saved and r estor ed when it invok es the set jm p and long jm p APIs r es pectively. 5. The sigset jm p API behaves similar ly to the set jm p API, exce pt that it has a second ar gument, save _ si g ma sk , which allows a user to s pecif y whether a calling pr ocess signal mask should be saved to the pr ovided env ar gument. 6. If the save _ si g ma sk ar gument is nonzer o, the caller ’s signal mask is saved, else signal mask is not saved. 7. The siglong jm p API does all o per ations as the long jm p API, but it also r estor es a calling pr ocess signal mask if the mask was saved in its env ar gument. 8. The r et _ val ar gument s pecif ies the r etur n value of the cor r es ponding sigset jm p API when called by siglong jm p API. Its value should be nonzer o num ber , and if it is zer o the siglong jm p API will r eset it to 1. 9. The siglong jm p API is usually called f r om user -def ined signal handling f unctions. This is because a pr ocess signal mask is modif ied when a signal handler is called, and siglong jm p should be called to ensur e that the pr ocess signal mask is r estor ed pr o per ly when “ jum ping out” f r om a signal handling f unction. 10. The f ollowing C pr ogr am illustr ates the use of sigset jm p and siglong jm p APIs. #include #include #include #include #include sig jm p _ buf
env;
void callme ( int sig _ num ) { cout << “catch signal:” << sig _ num << endl; siglong jm p ( env, 2 ); } int main ( int ar gc, char *ar gv[] )
Dept.of CS&E,SJBIT
Page 84
UNIX SYSTEM PROGRAMMING
10CS62
{ sigset _ t sigmask ; str uct sigaction action, old _ action; sigem ptyset(&sigmask ); if ( sigaddset( &sigmask , SIGTER M) == -1 || sig pr ocmask ( SIG _ SETMASK , &sigmask , 0) == -1) per r or (“Set signal mask ”); sigem ptyset( &action.sa _ mask ); sigaddset( &action.sa _ mask , SIGSEGV); action.sa _ handler = (void (*)())callme; action.sa _ f lags = 0; if (sigaction (SIGI NT, &action, &old _ action) == -1) per r or (“sigaction”); if (sigset jm p( env, 1) != 0 ) { cer r << “R etur n f r om signal inter r u ption\n”; retur n 0; } else cer r << “R etur n f r om f ir st time sigset jm p is called\n”; pause();
/* wait f or signal inter r u ption*/
} 11. The pr ogr am sets its signal mask to contain SIGTER M, and then sets u p a signal tr a p for the SIGI NT signal. 12. The pr ogr am then calls sigset jm p to stor e its code location in the env glo bal var ia ble. Note the sigset jmp call returns a zero value when directly called in user program and not via siglong jmp. 13. The pr ogr am sus pends its execution via the pause API. 14. When ever the user inter r u pts the pr ocess f r om the k ey boar d, the callme f unction is called. 15. The callme f unction calls siglong jm p API to tr ansf er f low back to the sigset jm p function in main, which now r etur ns a 2 value. 16. The sam ple out put of the a bove pr ogr am is: % cc sigset jm p.c % ./a.out &
Dept.of CS&E,SJBIT
Page 85
UNIX SYSTEM PROGRAMMING
10CS62
[1] 377 Retur n f r om f ir st time sigset jm p is called % k ill –I NT 377 catch signal: 2 Retur n f r om signal inter r u ption [1] Done a.out %
6.8. k ill 1. A pr ocess can send signal to a r elated pr ocess via the k ill API. 2. This is a sim ple means of IPC or contr ol. 3. The sender and r eci pient pr ocesses must be r elated such that either sender pr ocess r eal or ef f ective user ID matches that of the r eci pient pr ocess, or the sender has su pr ivileges. 4. For exam ple, a par ent and child pr ocess can send signals to each other via the k ill API. 5. The k ill API is def ined in most U NIX system and is a POSIX.1 standar d. 6. The f unction pr ototy pe is as: #include int k ill ( pid _ t pid, int signal _ num ); 7. The si g _ num ar gument is the integer value of a signal to be sent to one or mor e pr ocesses designated by pid . 8. The possi ble values of pid and its use by the k ill API ar e: pid value
Ef f ects on the k ill API
A positive value
pid is a pr ocess ID. Sends signal _ num to that pr ocess. Sends signal _ num to all pr ocesses whose pr ocess gr ou p ID is the 0 same as the calling pr ocess. Sends signal _ num to all pr ocesses whose r eal user ID is the same as the ef f ective user ID of the calling pr ocess. If the calling pr ocess ef f ective user ID is su user ID, signal _ num will be sent to all pr ocesses in the system ( exce pt pr ocesses – 0 and 1). The later case is -1 used when the system is shutting down – k er nel calls the k ill API to ter minate all pr ocesses exce pt 0 and 1. Note: POSIX.1 does not specif y the behavior of the k ill API when the pid value is -1. This ef f ect is for UNIX systems only. Sends signal _ num to all pr ocesses whose pr ocess gr ou p ID matches A negative value the a bsolute value of pid. Dept.of CS&E,SJBIT
Page 86
UNIX SYSTEM PROGRAMMING
10CS62
9. The r etur n value of k ill is zer o if it succeeds or -1 if it f ails. 10. The f ollowing C pr ogr am illustr ates the im plementation of the U NIX k ill command. #include #include #include #include #include int main ( int ar gc, char *ar gv[] ) { int pid, sig = SIGTER M; if (ar gc == 3) { if ( sscanf (ar gv[1], “%d”, &sig) != 1 ) { //get signal num ber cer r << “Invalid num ber :” << ar gv[1] << endl; retur n -1; } ar gv++; ar gc--; } while (--ar gc > 0) if (sscanf (*++ar gv, “%d”, & pid) == 1) { //get pr ocess ID if ( k ill ( pid, sig) == -1 ) per r or (“k ill”); } else cer r << “Invalid pid:” << ar gv[0] << endl; retur n 0; }
6.9. alarm 1. The alar m API can be called by a pr ocess to r equest the k er nel to send the SIGALR M signal af ter a cer tain num ber of r eal clock seconds. 2. The alar m API is def ined in most U NIX systems and is a POSIX.1 standar d. 3. The function pr ototy pe of the API is as: #include unsigned int alar m ( unsigned int time _ inter val ); 4. The t ime _ int er val ar gument is the num ber of CPU seconds ela pse time, af ter which the k er nel will send the SIGALR M signal to the calling pr ocess. 5. If a t ime _ int er val value is zer o, it tur ns of f the alar m clock .
Dept.of CS&E,SJBIT
Page 87
UNIX SYSTEM PROGRAMMING
10CS62
6. The r etur n value of the alar m API is the num ber of CPU seconds lef t in the pr ocess timer , as set by a pr evious alar m system call. 7. The ef f ect of the pr evious alar m API call is canceled, and the pr ocess timer is r eset with new alar m call. 8. A pr ocess alar m clock is not passed on to its f or k ed child pr ocess, but an exec’ed pr ocess r etains the same alar m clock value as was pr ior to the exec API call. 9. The alar m API can be used to im plement the slee p API. #include #include #include void wak eu p() {} unsigned int slee p ( unsigned int timer ) { str uct sigaction action; action.sa _ handler = wak eu p; action.sa _ f lags = 0; sigem ptyset ( &action.sa _ mask ); if ( sigaction (SIGALR M, &action, 0) == -1 ) { per r or (“sigaction”); retur n -1; } (void)alar m( timer ); (void) pause( ); } 10. The slee p f unction a bove sets u p a signal handler f or the SIGALR M, calls the alar m API to r equest the k er nel to send the SIGALR M signal af ter the t imer inter val, and finally, sus pends its execution via the pause system call. 11. The wak eu p signal handler f unction is called when the SIGALR M signal is sent to the pr ocess. When it r etur ns, the pause system call will be a bor ted, and the calling pr ocess will r etur n f r om the slee p f unction. 12. BSD U NIX def ines the ualar m f unction, which is the same as the alar m API exce pt that the ar gument and r etur n value of the ualar m f unction ar e in micr osecond units.
Dept.of CS&E,SJBIT
Page 88
UNIX SYSTEM PROGRAMMING
10CS62
6.10. Interval Timers 1. The use of the alar m API is to set u p a inter val timer in a pr ocess. 2. The inter val timer can be used to schedule a pr ocess to do some task s at f ixed time inter val, to time the execution of some o per ations, or limit the time allowed f or the execution of some task s. 3. The f ollowing pr ogr am illustr ates how to set u p a r eal-time clock inter val timer using the alar m API. #include #include #include #def ine I NTER VAL 5 void callme ( int sig _ no ) { alar m( I NTER VAL ); /* do scheduled task s */ } int main ( ) { str uct sigaction action; sigem ptyset( &action.sa _ mask ); action.sa _ handler = (void (*)( ))callme; action.sa _ f lags = SA _ R ESTAR T; if ( sigaction( SIGALR M, &action, 0) == -1) { per r or (“sigaction”); retur n 1; } if ( alar m( I NTER VAL ) == -1 ) per r or (“alar m”); else while (1) { /* do nor mal o per ation */ } retur n 0; } 4. The sigaction API is called to set u p callme as the signal handling f unction f or the SIGALR M signal.
Dept.of CS&E,SJBIT
Page 89
UNIX SYSTEM PROGRAMMING
10CS62
5. The pr ogr am then invok es the alar m API to send itself the SIGALR M signal af ter 5 real clock seconds. 6. The pr ogr am then goes of f to per f or m its nor mal o per ation in an inf inite loo p. 7. When the timer ex pir er s, the callme f unction is invok ed, which r estar ts the alar m clock f or another 5 seconds and then does the scheduled task s. 8. When the callme f unction r etur ns, the pr ogr am continues its “nor mal” o per ation until another timer ex pir ation. 9. BSD U NIX invented the setitimer API, which pr ovides ca pa bilities additional to those of the alar m API. 10. The set it imer r esolution time is in micr oseconds, wher eas the r esolution time f or alar m is in seconds. 11. The alar m API can be used to set u p r eal-time clock timer per pr ocess. The setitimer API can be used to def ine u p to thr ee dif f er ent ty pes of timer s in a pr ocess: a. Real time clock timer . b. Timer based on the user time s pent by a pr ocess c. Timer based on the total user and system times s pent by a pr ocess. 12. The getitimer API is also def ined in BSD and System V U NIX f or user s to quer y the timer values that ar e set by the setitimer API 13. The setitimer and getitimer f unction pr ototy pes ar e: #include int setitimer ( int which, const str uct itimer val *val, str uct itimer val *old ); int getitimer ( int which, str uct itimer val *old ); 14. The which ar gument s pecif y which timer to pr ocess, the possi ble values ar e: whi ch
argument value
ITIMER _ R EAL ITIMER _ VIR TUAL ITIMER _ PR OF
Timer type
Timer based on r eal-time clock . Gener ates a SIGALR M signal when ex pir es Timer based on user -time s pent by a pr ocess. Gener ates a SIGVTALR M signal when it ex pir es Timer based on total user and system times s pent by a pr ocess. Gener ates a SIGPR OF signal when it ex pir es
15. The st r uct it imer val data ty pe is def ined in the header as:
Dept.of CS&E,SJBIT
Page 90
UNIX SYSTEM PROGRAMMING
str uct itimer val { str uct timer val it _ inter val; str uct timer val it _ value; }
10CS62
//timer inter val //cur r ent value
16. For setitimer API, the val.it _ value is the time to set the named timer , and the val.it _ inter val is the time to r eload the timer when it ex pir es. 17. The val.it _ inter val may be set to zer o if the timer is to r un only once and if the val.it _ value is set to zer o, it sto ps the named timer if it is r unning. 18. For getitimer API, old.it _ value and the old.it _ inter val r etur n the named timer ’s remaining time and the r eload time, r es pectively. 19. The old ar gument of the setitimer API is lik e the old ar gument of the getitimer API. 20. If this is an addr ess of a str uct itimer val ty ped var ia ble, it r etur ns the pr evious timer value, if set to NULL the old timer value will not be r etur ned. 21. The ITIMER _ VIR TUAL and ITIMER _ PR OF timer s ar e pr imar y usef ul in timing the total execution time of selected user f unctions, as the timer r uns only while the user pr ocess is r unning or the k er nel is executing system f unctions on behalf of the user pr ocess for the ITIMER _ PR OF timer . 22. Both the APIs r etur n zer o on success or -1 value if they f ail. 23. Timer s set by the setitimer API in a par ent pr ocess ar e not inher ited by its child pr ocesses, but these timer s ar e r etained when a pr ocess exec’s a new pr ogr am. 24. The f ollowing pr ogr am illustr ates the use of setitimer API. #include #include #include #include #def ine I NTER VAL 2 void callme ( int sig _ no ) { /* do some scheduled task s */ } int main ( ) { str uct itimer val val;
Dept.of CS&E,SJBIT
Page 91
UNIX SYSTEM PROGRAMMING
10CS62
str uct sigaction action; sigem ptyset( &action.sa _ mask ); action.sa _ handler = (void (*)( ))callme; action.sa _ f lags = SA _ R ESTAR T; if ( sigaction( SIGALR M, &action, 0) == -1) { per r or (“sigaction”); retur n 1; } val.it _ inter val.tv _ sec = I NTER VAL; val.it _ inter val.tv _ usec = 0; val.it _ value.tv _ sec = I NTER VAL; val.it _ value.tv _ usec = 0; if ( setitimer ( ITIMER _ R EAL, &val, 0 ) == -1 ) per r or (“setitimer ”); else while (1) { /* do nor mal o per ation */ } retur n 0; } 25. Since the setitimer and alar m APIs r equir e that user s set u p signal handling to catch timer ex pir ation, they should not be used in con junction with the slee p API, because slee p API may modif y the signal handling f unction f or the SIGALR M signal. 6.11. POSIX.1b Timers
1. POSIX.1 b def ines a set of APIs f or inter val timer mani pulation. 2. The POSIX.1 b timer s ar e mor e f lexi ble and power f ul than U NIX timer s in f ollowing ways: a. User s may def ine multi ple inde pendent timer s per system clock . b. The timer r esolution is in nanoseconds. c. User s may s pecif y, on a timer basis, the signal to be r aised when a timer ex pir es. d. The timer inter val may be s pecif ied as either an a bsolute or a r elative time. 3. Ther e is a limit on how many POSIX timer s can be cr eated per pr ocess, this is TIMER _ MAX constant def ined in header .
Dept.of CS&E,SJBIT
Page 92
UNIX SYSTEM PROGRAMMING
10CS62
4. POSIX timer s cr eated by a pr ocess ar e not inher ited by its child pr ocess, but ar e retained acr oss the exec system call. 5. A POSIX.1 timer does not use the SIGALR M signal when it ex pir es, it can be used saf ely with the slee p API in the same pr ogr am. 6. The POSIX.1 b APIs f or timer mani pulation ar e: #include #include int timer _ cr eate(clock id _ t clock , str uct sigevent *s pec, timer _ t *timer _ hdr p); int timer _ settime(timer _ t timer _ hdr , int f lag, str uct itimr s pec *val, str uct itimr s pec *old); int timer _ gettime(timer _ t timer _ hdr , str uct itimr s pec *old); int timer _ getover r un(timer _ t timer _ hdr ); int timer _ delete(timer _ t timer _ hdr ); 7. The timer _ cr eate API is used to dynamically cr eate a timer and r etur ns its handler . 8. The clock ar gument s pecif ies which system clock would be the new timer based on, its value may be CLOCK _ R EALTIME f or cr eating a r eal time clock timer – this def ined by POSIX.1 b – other values ar e system de pendent. 9. The s pec ar gument def ines what action to tak e when the timer ex pir es. The str uct sigevent data ty pe is def ined as: str uct sigevent { int int union sigval };
sigev _ notif y; sigev _ signo; sigev _ value;
10. The sigev _ signo f ield s pecif ies a signal num ber to be r aised at the timer ex pir ation. Its valid only when the sigev _ notif y f ield is set to SIGEV _ SIG NAL. 11. If sigev _ notif y f ield is set to SIGEV _ NO NE, no signal is r aised by the timer when it ex pir es. 12. Because multi ple timer s may gener ate the same signal, the sigev _ value f ield is used to contain any user def ined data to identif y that a signal is r aised by a s pecif ic timer . The data str uctur e of the sigev _ f ield is: union sigval {
Dept.of CS&E,SJBIT
Page 93
UNIX SYSTEM PROGRAMMING
int void
10CS62
sival _ int; *sival _ ptr ;
} 13. For exam ple, a pr ocess may assign each timer a unique integer ID assigned to the s pec→ si g ev _ val ue. sival _ int field 14. To pass this data along with the signal ( si g ev _ si g no) when it is r aised, the SA _ SIGI NFO f lag should be set in an si g act ion call, which sets u p the handling f or the signal and the handling f unction pr ototy pe should be : void ( int signo, siginf o _ t* ev p, void *ucontext ); when the signal handler is called, the ev p→si _ value contains the data of the s pec→ si g ev _ val ue. The siginf o _ t data ty pe is def ined in 15. If s pec is set to NULL and the timer is based on CLOCK _ R EALTIME, then SIGALR M signal is r aised when the timer ex pir es. 16. Finally the t imer _ t ty ped var ia ble to hold the _ hd r p ar gument is an addr ess of a timer handler of the newly gener ated timer . This should not be NULL as it is used to call other POIXS.1 b timer APIs. 17. All POSIX.1 b timer APIs r etur n zer o on success and -1 if they f ail. 18. The timer _ settime star ts and sto ps a timer r unning. The timer _ gettime is used to quer y the cur r ent values of the timer . 19. The str uct itimer s pec data ty pe is def ined as: str uct itimer s pec { str uct timer s pec str uct timer s pec };
it _ inter val; it _ value;
and the str uct timer s pec is def ined as: str uct timer s pec { time _ t long };
tv _ sec; tv _ nsec;
20. The itimer s pec.it _ value s pecif ies the time r emaining in the timer , and itimer s pec.it _ inter val s pecif ies the new time to r eload the timer when it ex pir es. All
Dept.of CS&E,SJBIT
Page 94
UNIX SYSTEM PROGRAMMING
10CS62
times ar e s pecif ied in seconds via timer s pec.tv _ sec and in nanoseconds via timer s pec.tv _nsec. 21. The f lag par ameter in timer _ settime may be 0 or TIMER _ R EALTIME if the timer star t time (val ar gument) is r elative to the cur r ent time. 22. If the f lag value is TIMER _ ABSTIME, the timer star t time is an a bsolute time. 23. The old ar gument is used to o btain pr evious timer values, if NULL no timer values ar e r etur ned. 24. The old ar gument of timer _ gettime r etur ns the cur r ent values of named timer . 25. The timer _ getover r un API r etur ns the num ber of signals gener ated by a timer but was lost (over r un). Timer signals ar e not queued by the k er nel if they ar e r aised but not pr ocessed by tar get pr ocess, instead the k er nel r ecor ds the num ber of over r un signals per timer . 26. The timer _ destr oy is used to destr oy a timer cr eated by timer _ cr eate API.
6.12. Daemon Processes: Introduction 1. Daemons ar e pr ocesses that live f or a long time. They ar e of ten star ted when the system is bootstr a p ped and ter minate only when the system is shut down. 2. They do not have a contr olling ter minal; so we say that they r un in the back gr ound. U NIX systems have numer ous daemons that per f or m day-to-day activities. 3. Her e we look at the pr ocess str uctur e of daemons and how to wr ite a daemon. 4. Since a daemon does not have a contr olling ter minal, we need to see how a daemon can r e por t er r or conditions when something goes wr ong.
6.13. Daemon Characteristics 1. We look at some common system daemons and how they r elate to the conce pts of pr ocess gr ou ps, contr olling ter minals, and sessions. 2. The ps command pr ints the status of var ious pr ocesses in the system. We will execute: ps -a x j under BSD UNIX 3. The -a o ption shows the status of pr ocesses owned by other s, and -x shows pr ocesses that do not have a contr olling ter minal. The -j o ption dis plays the job-r elated inf or mation: the session ID, pr ocess gr ou p ID, contr olling ter minal, and ter minal pr ocess gr ou p ID.
Dept.of CS&E,SJBIT
Page 95
UNIX SYSTEM PROGRAMMING
10CS62
4. Under System V based systems, a similar command is p s -e f jc. 5. The out put f r om ps look s lik e PPID PID PGID SID TTY TPGID UID COMMAND
0
1
0
0
?
-1
0
ini t
1
2
1
1
?
-1
0
[k eventd ]
1
3
1
1
?
-1
0
[k a pmd ]
0
5
1
1
?
-1
0
[k swa pd ]
0
6
1
1
?
-1
0
[ bd f lush]
0
7
1
1
?
-1
0
[k u pdated ]
1
1009
1009
1009
?
-1 -1
32 por tmap
1
1048
1048
1048
?
-1
0
syslogd -m 0
1
1335
1335
1335
?
-1
0
xinetd - pidf ile /var /r un/xinetd p . id
1
1403
1
1
?
-1 -1
0
[nf sd ]
1
1405
1
1
?
-1
0
[lock d ]
1405 1406
1
1
?
-1
0
[r pciod ]
1
1853
1853
1853
?
-1
0
cr ond
1
2182
2182
2182
?
-1
0
/usr /s bin/cu ps d
6. The system pr ocesses de pend on the o per ating system im plementation. Anything with a par ent pr ocess ID of 0 is usually a k er nel pr ocess star ted as par t of the system bootstr a p pr ocedur e. (An exce ption to this is init, since it is a user -level command star ted by the k er nel at boot time.) 7. K er nel pr ocesses ar e s pecial and gener ally exist f or the entir e lif etime of the system. They r un with su per user pr ivileges and have no contr olling ter minal and no command line. 8. Pr ocess 1 is usually init, is a system daemon r es ponsi ble f or , among other things, star ting system ser vices s pecif ic to var ious r un levels. These ser vices ar e usually im plemented with the hel p of their own daemons. 9. On Linux, the k evenTD daemon pr ovides pr ocess context f or r unning scheduled functions in the k er nel.
Dept.of CS&E,SJBIT
Page 96
UNIX SYSTEM PROGRAMMING
10CS62
10. The k a pmd daemon pr ovides su p por t f or the advanced power management f eatur es availa ble with var ious com puter systems. 11. The k swa pd daemon is also k nown as the pageout daemon. It su p por ts the vir tual memor y su bsystem by wr iting dir ty pages to disk slowly over time. 12. The Linux k er nel f lushes cached data to disk using two additional daemons: bdf lush and k u pdated. 13. The por tma p per daemon, por tma p, pr ovides the ser vice of ma p ping R PC (R emote Pr ocedur e Call) pr ogr am num ber s to networ k por t num ber s. 14. The syslogd daemon is availa ble to any pr ogr am to log system messages f or an o per ator . The messages may be pr inted on a console device and also wr itten to a f ile. 15. The inetd daemon (xinetd) listens on the system's networ k inter f aces f or incoming requests f or var ious networ k ser ver s. 16. The nf sd, lock d, and r pciod daemons pr ovide su p por t f or the Networ k File System ( NFS). 17. The cr on daemon (cr ond) executes commands at s pecif ied dates and times. Numer ous system administr ation task s ar e handled by having pr ogr ams executed r egular ly by cr on. 18. The cu psd daemon is a pr int s pooler ; it handles pr int r equests on the system. 19. The k er nel daemons ar e star ted without a contr olling ter minal. The lack of a contr olling ter minal in the user -level daemons is pr o ba bly the r esult of the daemons having called setsid. 20. All the user -level daemons ar e pr ocess gr ou p leader s and session leader s and ar e the only pr ocesses in their pr ocess gr ou p and session. Finally, note that the par ent of most of these daemons is the init pr ocess.
6.14. Coding R ules 1. Some basic r ules to coding a daemon pr event unwanted inter actions f r om ha p pening. 2. We state these r ules and then show a f unction, d aemoni z e , that im plements them. a.
The f ir st thing to do is call umask to set the f ile mode cr eation mask to 0. The f ile mode cr eation mask that is inher ited could be set to deny cer tain per missions.
b.
Call f or k and have the par ent exit. This does sever al things.
Dept.of CS&E,SJBIT
Page 97
UNIX SYSTEM PROGRAMMING
10CS62
Fir st, if the daemon was star ted as a sim ple shell command, having the par ent ter minate mak es the shell think that the command is done. Second, the child inher its the pr ocess gr ou p ID of the par ent but gets a new pr ocess ID, so we are guar anteed that the child is not a pr ocess gr ou p leader . This is a pr er equisite f or the call to setsid that is done next. Call setsid to cr eate a new session. Thr ee ste ps occur . The pr ocess becomes a session leader of a new session, becomes the pr ocess gr ou p leader of a new pr ocess gr ou p has no contr olling ter minal.
c.
d.
Change the cur r ent wor k ing dir ector y to the r oot dir ector y. The cur r ent wor k ing dir ector y inher ited f r om the par ent could be on a mounted f ile system. Since daemons nor mally exist until the system is r e booted, if the daemon stays on a mounted f ile system, that f ile system cannot be unmounted.
e.
Unneeded f ile descr i ptor s should be closed. This pr events the daemon f r om holding o pen any descr i ptor s that it may have inher ited f r om its par ent.
f.
Some daemons o pen f ile descr i ptor s 0, 1, and 2 to /dev/null so that any li br ar y r outines that tr y to r ead f r om standar d in put or wr ite to standar d out put or standar d er r or will have no ef f ect. Since the daemon is not associated with a ter minal device, ther e is nowher e f or out put to be dis played; nor is ther e anywher e to r eceive in put f r om an inter active user . Even if the daemon was star ted f r om an inter active session, the daemon r uns in the back gr ound, and the login session can ter minate without af f ecting the daemon. If other user s log in on the same ter minal device, we wouldn't want out put f r om the daemon showing u p on the ter minal, and the user s wouldn't ex pect their in put to be r ead by the daemon.
Example
3. The f unction below can be called f r om a pr ogr am that wants to initialize itself as a daemon. #include #include #include #include #include void daemonize(const char *cmd) { int i, f d0, f d1, f d2; pid _ t pid; str uct r limit r l; str uct sigaction sa;
Dept.of CS&E,SJBIT
Page 98
UNIX SYSTEM PROGRAMMING
10CS62
/* Clear f ile cr eation mask . */ umask (0); /* Get maximum num ber of f ile descr i ptor s. */ if (getr limit(R LIMIT _ NOFILE, &r l) < 0) { pr intf ("%s: can't get f ile limit", cmd); exit(1); }
/* Become a session leader to lose contr olling TTY. */ if (( pid = f or k ()) < 0) { pr intf ("%s: can't f or k ", cmd); exit(1); } else if ( pid != 0) /* par ent */ exit(0); setsid(); /* Ensur e f utur e o pens won't allocate contr olling TTYs. */ sa.sa _ handler = SIG _ IG N; sigem ptyset(&sa.sa _ mask ); sa.sa _ f lags = 0; if (sigaction(SIGHUP, &sa, NULL) < 0) { per r or ("sigaction: can't ignor e SIGHUP"); exit(1); } if (( pid = f or k ()) < 0) { pr intf ("%s: can't f or k ", cmd); exit(1); } else if ( pid != 0) /* par ent */ exit(0); /* Change the cur r ent wor k ing dir ector y to the r oot so * we won't pr event f ile systems f r om being unmounted. */ if (chdir ("/") < 0) { per r or ("chdir: can't change dir ector y to /"); exit(1); } /* Close all o pen f ile descr i ptor s. */ if (r l.r lim _ max == R LIM _ I NFI NITY) rl.r lim _ max = 1024; for (i = 0; i < r l.r lim _ max; i++) close(i); /* Attach f ile descr i ptor s 0, 1, and 2 to /dev/null. */ fd0 = o pen("/dev/null", O _ R DWR );
Dept.of CS&E,SJBIT
Page 99
UNIX SYSTEM PROGRAMMING
10CS62
fd1 = du p(0); fd2 = du p(0); /* Initialize the log f ile. */ o penlog(cmd, LOG _ CO NS, LOG _ DAEMO N); if (f d0 != 0 || f d1 != 1 || f d2 != 2) { syslog(LOG _ ER R , "unex pected f ile descr i ptor s %d %d %d", fd0, f d1, f d2); exit(1); } } 4. If the daemonize f unction is called f r om a main pr ogr am that then goes to slee p, we can check the status of the daemon with the ps command: $ ./a.out $ ps -axj PPID PID 1 3346
PGID SID 3345 3345
TTY ?
TPGID UID COMMA ND -1 501 ./a.out
6.15. Error Logging 1. One pr o blem a daemon has is how to handle er r or messages. It can not sim ply wr ite to standar d er r or , since it should not have a contr olling ter minal. 2. The BSD s yslog facility is in 4.2BSD and most systems der ived f r om BSD su p por t syslog. 3. The syslog function is included as an XSI extension in the Single U NIX S pecif ication. 4. The BSD syslog facility is used by most daemons. Figur e below illustr ates its str uctur e.
Dept.of CS&E,SJBIT
Page 100
UNIX SYSTEM PROGRAMMING
10CS62
5. Ther e ar e thr ee ways to gener ate log messages: a. K er nel r outines can call the log f unction. These messages can be r ead by any user pr ocess that o pens and r eads the /dev/k log device. b. Most user pr ocesses (daemons) call the syslog f unction to gener ate log messages. This causes the message to be sent to the U NIX domain datagr am sock et /dev/log. c. A user pr ocess on this host, or on some other host that is connected to this host by a TCP/IP networ k , can send log messages to UDP por t 514. Note that the syslog f unction never gener ates these UDP datagr ams: they r equir e ex plicit networ k pr ogr amming by the pr ocess gener ating the log message. 6. Nor mally, the syslogd daemon r eads all thr ee for ms of log messages. 7. On star t-u p, this daemon r eads a conf igur ation f ile, usually /etc/syslog.conf , which deter mines wher e dif f er ent classes of messages ar e to be sent. 8. For exam ple, ur gent messages can be sent to the system administr ator (if logged in) and pr inted on the console, wher eas war nings may be logged to a f ile. 9. Our inter f ace to this f acility is thr ough the syslog f unction. #include void o penlog(const char *ident, int o ption, int f acility); void syslog(int pr ior ity, const char *for mat, ...); void closelog(void); int setlogmask (int mask pr i); 10. Calling o penlog is o ptional. If it's not called, the f ir st time syslog is called, o penlog is called automatically. 11. Calling closelog is also o ptional—it just closes the descr i ptor that was being used to communicate with the syslogd daemon. 12. Calling o penlog lets us s pecif y an ident that is added to each log message. This is nor mally the name of the pr ogr am (cr on, inetd, etc.). 13. The o ption ar gument is a bitmask s pecif ying var ious o ptions. 14. The availa ble o ptions ar e as f ollows: option
Description
LOG _ CO NS
If the log message can't be sent to syslogd via the U NIX domain datagr am, the message is wr itten to the console instead.
LOG _ NDELAY
O pen the U NIX domain datagr am sock et to the syslogd daemon immediately; don't wait until the f ir st message is logged. Nor mally, the
Dept.of CS&E,SJBIT
Page 101
UNIX SYSTEM PROGRAMMING
option
10CS62
Description
sock et is not o pened until the f ir st message is logged. Do not wait f or child pr ocesses that might have been cr eated in the pr ocess of logging the message. This pr events conf licts with LOG _ NOWAIT a p plications that catch SIGCHLD, since the a p plication might have retr ieved the child's status by the time that syslog calls wait. LOG _ ODELAY
Delay the o pen of the connection to the syslogd daemon until the f ir st message is logged.
LOG _ PER R OR
Wr ite the log message to standar d er r or in addition to sending it to syslogd. (Unavaila ble on Solar is.)
LOG _ PID
Log the pr ocess ID with each message. This is intended f or daemons that f or k a child pr ocess to handle dif f er ent r equests (as com par ed to daemons, such as syslogd, that never call f or k ).
15. The f acility ar gument f or o penlog is tak es on the f ollowing values. facility
LOG _ AUTH
Description
author ization pr ogr ams: login, su, getty, ...
LOG _ AUTHPR IV same as LOG _ AUTH, but logged to f ile with r estr icted per missions LOG _ CR O N
Cron and at
LOG _ DAEMO N
system daemons: inetd, r outed, ...
LOG _ FTP
the FTP daemon (f t pd)
LOG _ K ER N
messages gener ated by the k er nel
LOG _ LOCAL0
Reser ved f or local use
LOG _ LOCAL1
Reser ved f or local use
LOG _ LOCAL2
Reser ved for local use
LOG _ LOCAL3
Reser ved f or local use
LOG _ LOCAL4
Reser ved f or local use
LOG _ LOCAL5
Reser ved f or local use
LOG _ LOCAL6
Reser ved f or local use
Dept.of CS&E,SJBIT
Page 102
UNIX SYSTEM PROGRAMMING
facility
10CS62
Description
LOG _ LOCAL7
Reser ved f or local use
LOG _ LPR
line pr inter system: l pd, l pc, ...
LOG _ MAIL
the mail system
LOG _ NEWS
the Usenet networ k news system
LOG _ SYSLOG
the syslogd daemon itself
LOG _ USER
messages f r om other user pr ocesses (def ault)
LOG _ UUCP
the UUCP system
16. The r eason f or the f acility ar gument is to let the conf igur ation f ile s pecif y that messages f r om dif f er ent f acilities ar e to be handled dif f er ently. If we don't call o penlog, or if we call it with a f acility of 0, we can still s pecif y the f acility as par t of the pr ior ity ar gument to syslog. 17. The syslog is called to gener ate a log message. 18. The pr ior it y ar gument is a com bination of the facility listed a bove and and a level, listed below. These levels ar e or der ed by pr ior ity, f r om highest to lowest. level
Description
LOG _ EMER G
emer gency (system is unusa ble) (highest pr ior ity)
LOG _ ALER T
condition that must be f ixed immediately
LOG _ CR IT
cr itical condition (e.g., har d device er r or )
LOG _ ER R
er r or condition
LOG _ WAR NI NG war ning condition LOG _ NOTICE
nor mal, but signif icant condition
LOG _ I NFO
inf or mational message
LOG _ DEBUG
de bug message (lowest prior ity)
19. The for mat ar gument and any r emaining ar guments ar e passed to the vs pr intf f unction for f or matting.
Dept.of CS&E,SJBIT
Page 103
UNIX SYSTEM PROGRAMMING
10CS62
20. Any occur r ence of the two char acter s %m in the for mat ar e f ir st r e placed with the er r or message str ing (str er r or ) cor r es ponding to the value of er r no. 21. The setlogmask f unction can be used to set the log pr ior ity mask f or the pr ocess. This function r etur ns the pr evious mask . 22. When the log pr ior ity mask is set, messages ar e not logged unless their pr ior ity is set in the log pr ior ity mask . Note that attem pts to set the log pr ior ity mask to 0 will have no ef f ect. 23. The logger pr ogr am is also pr ovided by many systems as a way to send log messages to the syslog facility. This logger command is intended f or a shell scr i pt r unning none inter actively that needs to gener ate log messages. E x am pl e
24. In a (hy pothetical) line pr inter s pooler daemon, you might encounter the sequence o penlog("l pd", LOG _ PID, LOG _ LPR ); syslog(LOG _ ER R , "o pen er r or f or %s: %m", f ilename); 25. The f ir st call sets the ident str ing to the pr ogr am name, s pecif ies that the pr ocess ID should always be pr inted, and sets the def ault facility to the line pr inter system. 26. The call to syslog s pecif ies an er r or condition and a message str ing. 27. If we had not called o penlog, the second call could have been syslog(LOG _ ER R | LOG _ LPR , "o pen er r or f or %s: %m", f ilename); Her e, we s pecif y the pr ior ity ar gument as a com bination of a level and a facilit y. 28. In addition to syslog, many platf or ms pr ovide a var iant that handles var ia ble ar gument lists. #include #include void vsyslog(int pr ior ity, const char *for mat, va _ list ar g); 29. Most syslogd im plementations will queue messages f or a shor t time. If a du plicate message ar r ives dur ing this time, the syslog daemon will not wr ite it to the log. 30. Instead, the daemon will pr int out a message similar to "last message r e peated N times."
Dept.of CS&E,SJBIT
Page 104
UNIX SYSTEM PROGRAMMING
10CS62
6.16. Single-Instance Daemons 1. Some daemons ar e im plemented so that only a single co py of the daemon should be running at a time f or pr o per o per ation. 2. The daemon might need exclusive access to a device, f or exam ple. In the case of the cr on daemon, if multi ple instances wer e r unning, each co py might tr y to star t a single scheduled o per ation, r esulting in du plicate o per ations and pr o ba bly an er r or . 3. If the daemon needs to access a device, the device dr iver will sometimes pr event multi ple o pens of the cor r es ponding device node in /dev. 4. This r estr icts us to one co py of the daemon r unning at a time. If no such device is availa ble, however , we need to do the wor k our selves. 5. The f ile- and r ecor d-lock ing mechanism pr ovides the basis f or one way to ensur e that only one co py of a daemon is r unning. 6. If each daemon cr eates a f ile and places a wr ite lock on the entir e f ile, only one such wr ite lock will be allowed to be cr eated. 7. Successive attem pts to cr eate wr ite lock s will f ail, ser ving as an indication to successive co pies of the daemon that another instance is alr eady r unning. 8. File and r ecor d lock ing pr ovides a convenient mutual-exclusion mechanism. 9. If the daemon o btains a wr ite-lock on an entir e f ile, the lock will be r emoved automatically if the daemon exits. This sim plif ies r ecover y, r emoving the need f or us to clean u p f r om the pr evious instance of the daemon. E x am pl e
10. The f unction shown below illustr ates the use of f ile and r ecor d lock ing to ensur e that only one co py of a daemon is r unning. 11. Each co py of the daemon will tr y to cr eate a f ile and wr ite its pr ocess ID in it. This will allow administr ator s to identif y the pr ocess easily. 12. If the f ile is alr eady lock ed, the lock f ile function will f ail with er r no set to EACCES or EAGAIN, so we r etur n 1, indicating that the daemon is alr eady r unning. Other wise, we tr uncate the f ile, wr ite our pr ocess ID to it, and r etur n 0. 13. Tr uncating the f ile pr events data f r om the pr evious daemon a p pear ing as if it a p plies to the cur r ent daemon. #include #include
Dept.of CS&E,SJBIT
Page 105
UNIX SYSTEM PROGRAMMING
10CS62
#include #include #include #include #include #include #def ine LOCK FILE "/var /r un/daemon p . id" #def ine LOCK MODE (S _ IR USR |S _ IWUSR |S _ IR GR P|S _ IR OTH) exter n int lock f ile(int); int alr eady _ r unning(void) { int f d; char buf [16]; fd = o pen(LOCK FILE, O _ R DWR |O _ CR EAT, LOCK MODE); if (f d < 0) { syslog(LOG _ ER R , "can't o pen %s: %s", LOCK FILE, str er r or (er r no)); exit(1); } if (lock f ile(f d) < 0) { if (er r no == EACCES || er r no == EAGAI N) { close(f d); retur n(1); } syslog(LOG _ ER R , "can't lock %s: %s", LOCK FILE, str er r or (er r no)); exit(1); } ftr uncate(f d, 0); s pr intf ( buf , "%ld", (long)get pid()); wr ite(f d, buf , str len( buf )+1); retur n(0); }
6.17. Daemon Conventions 1. Sever al common conventions ar e f ollowed by daemons in the U NIX System.
If the daemon uses a lock f ile, the f ile is usually stor ed in /var /r un. Note, however , that the daemon might need su per user per missions to cr eate a f ile her e. The name of the f ile is usually name p . id, wher e name is the name of the daemon or the ser vice. For exam ple, the name of the cr on daemon's lock f ile is /var /r un/cr ond p . id.
Dept.of CS&E,SJBIT
Page 106
UNIX SYSTEM PROGRAMMING
10CS62
If the daemon su p por ts conf igur ation o ptions, they ar e usually stor ed in /etc. The conf igur ation f ile is named name.conf , wher e name is the name of the daemon or the name of the ser vice. For exam ple, the conf igur ation f or the syslogd daemon is /etc/syslog.conf . Daemons can be star ted f r om the command line, but they ar e usually star ted f r om one of the system initialization scr i pts (/etc/r c* or /etc/init.d/*). If the daemon should be r estar ted automatically when it exits, we can ar r ange f or init to r estar t it if we include a res pawn entr y f or it in /etc/inittab. If a daemon has a conf igur ation f ile, the daemon r eads it when it star ts, but usually won't look at it again. If an administr ator changes the conf igur ation, the daemon would need to be sto p ped and r estar ted to account f or the conf igur ation changes. To avoid this, some daemons will catch SIGHUP and r er ead their conf igur ation f iles when they r eceive the signal. Since they ar en't associated with ter minals and ar e either session leader s without contr olling ter minals or mem ber s of or phaned pr ocess gr ou ps, daemons have no r eason to ex pect to r eceive SIGHUP. Thus, they can saf ely r euse it.
6.18. ClientServer Model 1. A common use f or a daemon pr ocess is as a ser ver pr ocess. 2. We can call the s yslogd pr ocess a ser ver that has messages sent to it by user pr ocesses (clients) using a U NIX domain datagr am sock et. 3. In gener al, a ser ver is a pr ocess that waits f or a client to contact it, r equesting some ty pe of ser vice. 4. The ser vice being pr ovided by the syslogd ser ver is the logging of an er r or message.
Dept.of CS&E,SJBIT
Page 107
UNIX SYSTEM PROGRAMMING
10CS62
UNIT – 7 INTER PR OCESS COMMUNICATION 7.1. Introduction 1. We have seen the pr ocess contr ol pr imitives and saw how to invok e multi ple pr ocesses. 2. The only way f or these pr ocesses to exchange inf or mation is by passing o pen f iles acr oss a for k or an exec or thr ough the f ile system. 3. We will now descr i be other techniques f or pr ocesses to communicate with each other : IPC, or inter - pr ocess communication. 4. In the past, U NIX S ystem IPC was a hodge podge of var ious a p pr oaches, f ew of which wer e por ta ble acr oss all U NIX system im plementations. 5. Thr ough the POSIX and The O pen Gr ou p (f or mer ly X/O pen) standar dization ef f or ts, the situation has im pr oved, but dif f er ences still exist. 6. Ta ble 7.1 summar izes the var ious f or ms of IPC. Sno.
IPC type
1.
half -du plex pi pes
2.
FIFOs
3.
full-du plex pi pes
4.
named f ull-du plex pi pes
5.
message queues
6.
Sema phor es
7.
shar ed memor y
8.
Sock ets
9
STR EAMS
7. The f ir st seven f or ms of IPC in Ta ble 7.1 ar e usually r estr icted to IPC between pr ocesses on the same host. 8. The f inal two r ows—sock ets and STR EAMS—ar e the only two that ar e gener ally su p por ted f or IPC between pr ocesses on dif f er ent hosts.
Dept.of CS&E,SJBIT
Page 108
UNIX SYSTEM PROGRAMMING
10CS62
7.2. Pipes 1. Pi pes ar e the oldest f or m of U NIX System IPC and ar e pr ovided by all U NIX systems. Pi pes have two limitations. a.) Histor ically, they have been half du plex (i.e., data f lows in only one dir ection). Some systems now pr ovide f ull-du plex pi pes, but f or maximum por ta bility, we should never assume that this is the case. b.) Pi pes can be used only between pr ocesses that have a common ancestor . Nor mally, a pi pe is cr eated by a pr ocess, that pr ocess calls for k , and the pi pe is used between the par ent and the child. 2. Half -du plex pi pes ar e the most commonly used f or m of IPC. 3. Ever y time you ty pe a sequence of commands in a pi peline f or the shell to execute, the shell cr eates a se par ate pr ocess f or each command and link s the standar d out put of one to the standar d in put of the next using a pi pe. 4. A pi pe is cr eated by calling the pi pe function. #include int pi pe(int f iledes[2]); Retur ns: 0 if OK , -1 on er r or 5. Two f ile descr i ptor s ar e r etur ned thr ough the f iledes ar gument: f iledes[0] is o pen f or reading, and f iledes[1] is o pen f or wr iting. 6. The out put of f iledes[1] is the in put f or f iledes[0]. 7. Two ways to pictur e a half -du plex pi pe ar e shown in f igur e 7.1.
Figure 7.1: Two ways to view a half -duplex pipe
Dept.of CS&E,SJBIT
Page 109
UNIX SYSTEM PROGRAMMING
10CS62
8. The lef t half of the f igur e 7.1 shows the two ends of the pi pe connected in a single pr ocess. 9. The r ight half of the f igur e 7.1 em phasizes that the data in the pi pe f lows thr ough the ker nel. 10. The f stat f unction r etur ns a f ile ty pe of FIFO f or the f ile descr i ptor of either end of a pi pe. We can test f or a pi pe with the S _ ISFIFO macr o. 11. A pi pe in a single pr ocess is next to useless. 12. Nor mally, the pr ocess that calls pi pe then calls f or k , cr eating an IPC channel f r om the par ent to the child or vice ver sa. 13. What ha p pens af ter the f or k de pends on which dir ection of data f low we want. 14. For a pi pe f r om the par ent to the child, the par ent closes the r ead end of the pi pe (f d[0]), and the child closes the wr ite end (f d[1]). 15. Figur e 7.2 shows the r esulting ar r angement of descr i ptor s.
Figure 7.2: Pipe f rom parent to child
16. For a pi pe f r om the child to the par ent, the par ent closes f d[1], and the child closes fd[0]. 17. When one end of a pi pe is closed, the f ollowing two r ules a p ply. 1. If we r ead f r om a pi pe whose wr ite end has been closed, r ead r etur ns 0 to indicate an end of f ile af ter all the data has been r ead.
Dept.of CS&E,SJBIT
Page 110
UNIX SYSTEM PROGRAMMING
10CS62
2. If we wr ite to a pi pe whose r ead end has been closed, the signal SIGPIPE is gener ated. If we either ignor e the signal or catch it and r etur n f r om the signal handler , wr ite r etur ns -1 with er r no set to EPIPE. 18. When we ar e wr iting to a pi pe (or FIFO), the constant PIPE _ BUF s pecif ies the ker nel's pi pe buf f er size. E x am pl e
19. Pr ogr am 7.1 shows the code to cr eate a pi pe between a par ent and its child and to send data down the pi pe. #include #include #include int main(void) { int n; int f d[2]; pid _ t pid; char line[MAXLI NE]; if ( pi pe(f d) < 0) per r or (" pi pe er r or "); if (( pid = f or k ()) < 0) { per r or ("f or k er r or "); } else if ( pid > 0) { /* par ent */ close(f d[0]); wr ite(f d[1], "hello wor ld\n", 12); } else { /* child */ close(f d[1]); n = r ead(f d[0], line, MAXLI NE); wr ite(STDOUT _ FILE NO, line, n); } exit(0); } Program 7.1: Send data f rom parent to child over a pipe
20. In Pr ogr am 7.1, we called r ead and wr ite dir ectly on the pi pe descr i ptor s. 21. What is mor e inter esting is to du plicate the pi pe descr i ptor s onto standar d in put or standar d out put.
Dept.of CS&E,SJBIT
Page 111
UNIX SYSTEM PROGRAMMING
10CS62
22. Of ten, the child then r uns some other pr ogr am, and that pr ogr am can either r ead f r om its standar d in put (the pi pe that we cr eated) or wr ite to its standar d out put (the pi pe). E x am pl e
23. Recall the f ive f unctions TELL _ WAIT, WAIT _ PAR E NT, and WAIT _ CHILD.
TELL _ PAR E NT,
TELL _ CHILD,
24. Pr ogr am code 7.2 shows an im plementation of the f unctions using pi pes. #include #include #include static int pf d1[2], pf d2[2]; void TELL _ WAIT(void) { if ( pi pe( pf d1) < 0 || pi pe( pf d2) < 0) per r or (" pi pe er r or "); } void TELL _ PAR ENT( pid _ t pid) { if (wr ite( pf d2[1], "c", 1) != 1) per r or ("wr ite er r or "); } void WAIT _ PAR ENT(void) { char c; if (r ead( pf d1[0], &c, 1) != 1) per r or ("r ead er r or "); if (c != p ' ') { pr intf ("WAIT _ PAR E NT: incor r ect data"); exit(1); } } void TELL _ CHILD( pid _ t pid) { if (wr ite( pf d1[1], " p", 1) != 1) perr or ("wr ite er r or "); } void WAIT _ CHILD(void) { char c; if (r ead( pf d2[0], &c, 1) != 1) per r or ("r ead er r or ");
Dept.of CS&E,SJBIT
Page 112
UNIX SYSTEM PROGRAMMING
10CS62
if (c != 'c') pr intf ("WAIT _ CHILD: incor r ect data"); exit(1); } } Program 7.2: R outines to let a parent and child synchronize
25. We cr eate two pi pes bef or e the for k , as shown in pr ogr am 7.2.
Figure 7.3: Using two pipes f or parent—child synchronization
26. The par ent wr ites the char acter " p" acr oss the to p pi pe when TELL _ CHILD is called, and the child wr ites the char acter "c" acr oss the bottom pi pe when TELL _ PAR E NT is called. 27. The cor r es ponding WAIT _ xxx functions do a block ing read for the single char acter . 28. Note that each pi pe has an extr a r eader , which doesn't matter . That is, in addition to the child r eading f r om pf d1[0], the par ent also has this end of the to p pi pe o pen f or reading. This does not af f ect us, since the par ent doesn't tr y to r ead f r om this pi pe.
7.3. popen and pclose Functions 1. Since a common o per ation is to cr eate a pi pe to another pr ocess, to either r ead its out put or send it in put, the standar d I/O li br ar y pr ovides the po pen and pclose functions. 2. These two f unctions handle all the dir ty wor k that we have been doing our selves:
Creating a pi pe For k ing a child Closing the unused ends of the pi pe Executing a shell to r un the command And waiting f or the command to ter minate.
3. The pr ototy pes of these f unctions ar e: #include Dept.of CS&E,SJBIT
Page 113
UNIX SYSTEM PROGRAMMING
10CS62
FILE * po pen(const char *cmdstr ing, const char *ty pe); Retur ns: f ile pointer if OK , NULL on er r or int pclose(FILE *fp); Retur ns: ter mination status of cmdstr ing, or 1 on er r or 4. The f unction po pen does a for k and exec to execute the cmdstr ing, and r etur ns a standar d I/O f ile pointer . 5. If ar gument ty pe is "r ", the f ile pointer is connected to the standar d out put of cmdstr ing (f igur e 7.4)
Figure 7.4: R esult of fp = popen(cmdstring, "r")
6. If ty pe is "w", the f ile pointer is connected to the standar d in put of cmdstr ing, as shown in f igur e 7.5.
Figure 7.5. R esult of f p = popen(cmdstring, "w")
7. The pclose f unction closes the standar d I/O str eam, waits f or the command to ter minate, and r etur ns the ter mination status of the shell. 8. If the shell cannot be executed, the ter mination status r etur ned by pclose is as if the shell had executed exit(127). 9. The cmdstr ing is executed by the Bour ne shell, as in sh -c cmdstr ing 10. This means that the shell ex pands any of its s pecial char acter s in cmdstr ing. This allows us to say, f or exam ple, f p = po pen("ls *.c", "r "); or f p = po pen("cmd 2>&1", "r "); Dept.of CS&E,SJBIT
Page 114
UNIX SYSTEM PROGRAMMING
10CS62
E x am pl e: I m pl ement at i on o f po pen and pcl ose Funct i ons
11. Pr ogr am code 7.3 shows the im plementation of po pen and pclose. #include #include #include #include #include #include /* Pointer to ar r ay allocated at run-time. */ static pid _ t *child pid = NULL; static int maxf d; FILE * po pen(const char *cmdstr ing, const char *ty pe) { int i; int pf d[2]; pid _ t pid; FILE *f p; /* only allow "r " or "w" */ if ((ty pe[0] != 'r ' && ty pe[0] != 'w') || ty pe[1] != 0) { er r no = EI NVAL; /* r equir ed by POSIX */ retur n( NULL); } if (child pid == NULL) { /* f ir st time thr ough */ /* allocate zer oed out ar r ay f or child pids */ maxf d = sysconf ( _ SC _ OPE N _ MAX); if ((child pid = calloc(maxf d, sizeof ( pid _ t))) == NULL) retur n( NULL); } if ( pi pe( pf d) < 0) retur n( NULL); /* er r no set by pi pe() */ if (( pid = f or k ()) < 0) { retur n( NULL); /* er r no set by f or k () */ } else if ( pid == 0) { /* child */ if (*ty pe == 'r ') { close( pf d[0]); if ( pf d[1] != STDOUT _ FILE NO) { du p2( pf d[1], STDOUT _ FILE NO); close( pf d[1]); } } else {
Dept.of CS&E,SJBIT
Page 115
UNIX SYSTEM PROGRAMMING
10CS62
close( pf d[1]); if ( pf d[0] != STDI N _ FILE NO) { du p2( pf d[0], STDI N _ FILE NO); close( pf d[0]); } } /* close all descr i ptor s in child pid[] */ for (i = 0; i < maxf d; i++) if (child pid[i] > 0) close(i); execl("/ bin/sh", "sh", "-c", cmdstr ing, (char *)0); _exit(127); } /* par ent continues... */ if (*ty pe == 'r ') { close( pf d[1]); if ((f p = fdo pen( pf d[0], ty pe)) == NULL) retur n( NULL); } else { close( pf d[0]); if ((f p = f do pen( pf d[1], ty pe)) == NULL) retur n( NULL); } child pid[f ileno(f p)] = pid; /* r emem ber child pid f or this f d */ retur n(f p); }
int pclose(FILE *f p) { int f d, stat; pid _ t pid; if (child pid == NULL) { er r no = EI NVAL; retur n(-1); /* po pen() has never been called */ } fd = f ileno(f p); if (( pid = child pid[f d]) == 0) { err no = EI NVAL; retur n(-1); /* f p wasn't o pened by po pen() */ } child pid[f d] = 0;
Dept.of CS&E,SJBIT
Page 116
UNIX SYSTEM PROGRAMMING
10CS62
if (f close(f p) == EOF) retur n(-1); while (wait pid( pid, &stat, 0) < 0) if (er r no != EI NTR ) retur n(-1); /* er r or other than EI NTR f r om wait pid() */ retur n(stat); /* r etur n child's ter mination status */ } Program 7.3The popen and pclose f unctions
7.4. Co-processes 1. A U NIX system f ilter is a pr ogr am that r eads f r om standar d in put and wr ites to standar d out put. 2. Filters ar e nor mally connected linear ly in shell pi pelines. A f ilter becomes a co- pr ocess when the same pr ogr am gener ates the f ilter 's in put and r eads the f ilter 's out put. 3. The K or n shell pr ovides co- pr ocesses. The Bour ne shell, the Bour ne-again shell, and the C shell don't pr ovide a way to connect pr ocesses together as co- pr ocesses. 4. A co- pr ocess nor mally r uns in the back gr ound f r om a shell, and its standar d in put and standar d out put ar e connected to another pr ogr am using a pi pe. 5. Co- pr ocesses ar e also usef ul f r om a C pr ogr am. 6. Wher eas po pen gives us a one-way pi pe to the standar d in put or f r om the standar d out put of another pr ocess, with a co- pr ocess, we have two one-way pi pes to the other pr ocess: one to its standar d in put and one f r om its standar d out put. 7. We want to wr ite to its standar d in put, let it o per ate on the data, and then r ead f r om its standar d out put. E x am pl e 8. Let's look at co- pr ocesses with an exam ple. The pr ocess cr eates two pi pes: one is the standar d in put of the co- pr ocess, and the other is the standar d out put of the co- pr ocess. Figur e 7.7 shows this ar r angement.
Figure 7.7. Driving a co-process by writing its standard input and reading its standard output
Dept.of CS&E,SJBIT
Page 117
UNIX SYSTEM PROGRAMMING
10CS62
9. The pr ogr am 7.4 is a sim ple co- pr ocess that r eads two num ber s f r om its standar d in put, com putes their sum, and wr ites the sum to its standar d out put. We com pile this pr ogr am and leave the executa ble in the f ile add2. #include "a pue.h" int main(void) { int n, int1, int2; char line[MAXLI NE]; while ((n = r ead(STDI N _ FILE NO, line, MAXLI NE)) > 0) { line[n] = 0; /* null ter minate */ if (sscanf (line, "%d%d", &int1, &int2) == 2) { s pr intf (line, "%d\n", int1 + int2); n = str len(line); if (wr ite(STDOUT _ FILE NO, line, n) != n) er r _ sys("wr ite er r or "); } else { if (wr ite(STDOUT _ FILE NO, "invalid ar gs\n", 13) != 13) er r _ sys("wr ite er r or "); } } exit(0); } Program 7.4. Simple f ilter to add two numbers
The pr ogr am 7.5 invok es the add2 co- pr ocess af ter r eading two num ber s f r om its standar d in put. The value f r om the co- pr ocess is wr itten to its standar d out put. #include "a pue.h" static void sig _ pi pe(int);
/* our signal handler */
int main(void) { int n, f d1[2], f d2[2]; pid _ t pid; char line[MAXLI NE]; if (signal(SIGPIPE, sig _ pi pe) == SIG _ ER R ) er r _ sys("signal er r or "); if ( pi pe(f d1) < 0 || pi pe(f d2) < 0) er r _ sys(" pi pe er r or ");
Dept.of CS&E,SJBIT
Page 118
UNIX SYSTEM PROGRAMMING
10CS62
if (( pid = f or k ()) < 0) { er r _ sys("f or k er r or "); } else if ( pid > 0) { /* par ent */ close(f d1[0]); close(f d2[1]); while (f gets(line, MAXLI NE, stdin) != NULL) { n = str len(line); if (wr ite(f d1[1], line, n) != n) er r _ sys("wr ite er r or to pi pe"); if ((n = r ead(f d2[0], line, MAXLI NE)) < 0) er r _ sys("r ead er r or f r om pi pe"); if (n == 0) { er r _ msg("child closed pi pe"); br eak ; } line[n] = 0; /* null ter minate */ if (f puts(line, stdout) == EOF) er r _ sys("f puts er r or "); } if (f er r or (stdin)) er r _ sys("f gets er r or on stdin"); exit(0); } else { /* child */ close(f d1[1]); close(f d2[0]); if (f d1[0] != STDI N _ FILE NO) { if (du p2(f d1[0], STDI N _ FILE NO) != STDI N _ FILE NO) er r _ sys("du p2 er r or to stdin"); close(f d1[0]); } if (f d2[1] != STDOUT _ FILE NO) { if (du p2(f d2[1], STDOUT _ FILE NO) != STDOUT _ FILE NO) er r _ sys("du p2 er r or to stdout"); close(f d2[1]); } if (execl("./add2", "add2", (char *)0) < 0) er r _ sys("execl er r or "); } exit(0); } static void sig _ pi pe(int signo) { pr intf ("SIGPIPE caught\n"); exit(1); } Program 7.5. Program to drive the add2 f ilter
Dept.of CS&E,SJBIT
Page 119
UNIX SYSTEM PROGRAMMING
10CS62
10. Her e, we cr eate two pi pes, with the par ent and the child closing the ends they don't need. 11. We have to use two pi pes: one f or the standar d in put of the co- pr ocess and one f or its standar d out put. 12. The child then calls du p2 to move the pi pe descr i ptor s onto its standar d in put and standar d out put, bef or e calling execl. 13. If we com pile and r un the pr ogr am 7.5, it wor k s as ex pected. Fur ther mor e, if we k ill the add2 co- pr ocess while the pr ogr am 7.5 is waiting f or our in put and then enter two num ber s, the signal handler is invok ed when the pr ogr am wr ites to the pi pe that has no r eader .
7.5. FIFOs 1. FIFOs ar e sometimes called named pi pes. 2. Pi pes can be used only between r elated pr ocesses when a common ancestor has cr eated the pi pe. 3. With FIFOs, however , unr elated pr ocesses can exchange data. 4. We saw ear lier that a FIFO is a ty pe of f ile. One of the encodings of the st _ mode mem ber of the stat str uctur e indicates that a f ile is a FIFO. 5. We can test f or this with the S _ ISFIFO macr o. 6. Cr eating a FIFO is similar to cr eating a f ile. Indeed, the pathname f or a FIFO exists in the f ile system. #include int mk f if o(const char * pathname, mode _ t mode); Retur ns: 0 if OK , 1 on er r or 7. The s pecif ication of the mode ar gument f or the mk f if o f unction is the same as f or the o pen f unction. 8. The r ules f or the user and gr ou p owner shi p of the new FIFO ar e the same as we descr i bed ear lier . 9. Once we have used mk f if o to cr eate a FIFO, we o pen it using o pen. 10. Indeed, the nor mal f ile I/O f unctions (close, r ead, wr ite, unlink , etc.) all wor k with FIFOs.
Dept.of CS&E,SJBIT
Page 120
UNIX SYSTEM PROGRAMMING
10CS62
11. When we o pen a FIFO, the non block ing flag (O _ NO NBLOCK ) af f ects what ha p pens.
In the nor mal case (O _ NO NBLOCK not s pecif ied), an o pen f or r ead-only block s until some other pr ocess o pens the FIFO f or wr iting. Similar ly, an o pen f or wr iteonly block s until some other pr ocess o pens the FIFO f or r eading. If O _ NO NBLOCK is s pecif ied, an o pen f or r ead-only r etur ns immediately. But an o pen f or wr ite-only r etur ns -1 with er r no set to E NXIO if no pr ocess has the FIFO o pen f or r eading.
12. As with a pi pe, if we wr ite to a FIFO that no pr ocess has o pen f or r eading, the signal SIGPIPE is gener ated. 13. When the last wr iter f or a FIFO closes the FIFO, an end of f ile is gener ated f or the reader of the FIFO. 14. It is common to have multi ple wr iter s f or a given FIFO. This means that we have to wor r y a bout atomic wr ites if we do not want the wr ites f r om multi ple pr ocesses to be inter leaved. 15. As with pi pes, the constant PIPE _ BUF s pecif ies the maximum amount of data that can be wr itten atomically to a FIFO. 16. Ther e ar e two uses f or FIFOs. 1. FIFOs ar e used by shell commands to pass data f r om one shell pi peline to another without cr eating inter mediate tem por ar y f iles. 2. FIFOs ar e used as r endezvous points in client-ser ver a p plications to pass data between the clients and the ser ver s. E x am pl e: U si n g F I F Os t o Du pl i cat e Out put S t reams
17. FIFOs can be used to du plicate an out put str eam in a ser ies of shell commands. 18. This pr events wr iting the data to an inter mediate disk f ile (similar to using pi pes to avoid inter mediate disk f iles). 19. But wher eas pi pes can be used only f or linear connections between pr ocesses, a FIFO has a name, so it can be used f or nonlinear connections. 20. Consider a pr ocedur e that needs to pr ocess a f ilter ed in put str eam twice. Figur e 7.8 shows this ar r angement.
Dept.of CS&E,SJBIT
Page 121
UNIX SYSTEM PROGRAMMING
10CS62
Figure 7.8. Procedure that processes a filtered input stream twice
21. With a FIFO and the U NIX pr ogr am tee(1), we can accom plish this pr ocedur e without using a tem por ar y f ile. (The tee pr ogr am co pies its standar d in put to both its standar d out put and to the f ile named on its command line.) mk fif o f if o1 pr og3 < f if o1 & pr og1 < inf ile | tee f if o1 | pr og2 22. We cr eate the FIFO and then star t pr og3 in the back gr ound, r eading f r om the FIFO. We then star t pr og1 and use tee to send its in put to both the FIFO and pr og2.
Figur e 7.9 shows the pr ocess ar r angement. Figure 7.9. Using a FIFO and tee to send a stream to two dif f erent processes E x am pl eC l i ent S er ver C ommuni cat i on U si n g a F I F O
23. Another use f or FIFOs is to send data between a client and a ser ver . 24. If we have a ser ver that is contacted by numer ous clients, each client can wr ite its request to a well-known FIFO that the ser ver cr eates. (By "well-k nown" we mean that the pathname of the FIFO is k nown to all the clients that need to contact the ser ver .) 25. Figur e 7.10 shows this ar r angement.
Dept.of CS&E,SJBIT
Page 122
UNIX SYSTEM PROGRAMMING
10CS62
Figure 7.10. Clients sending requests to a server using a FIFO
26. Since ther e ar e multi ple wr iter s f or the FIFO, the r equests sent by the clients to the ser ver need to be less than PIPE _ BUF bytes in size. This pr events any inter leaving of the client wr ites. 27. The pr o blem in using FIFOs f or this ty pe of client-ser ver communication is how to send r e plies back f r om the ser ver to each client. 28. A single FIFO can't be used, as the clients would never k now when to r ead their res ponse ver sus r es ponses for other clients. 29. One solution is f or each client to send its pr ocess ID with the r equest. The ser ver then cr eates a unique FIFO f or each client, using a pathname based on the client's pr ocess ID. 30. For exam ple, the ser ver can cr eate a FIFO with the name /tm p/ser v1.XXXXX, wher e XXXXX is r e placed with the client's pr ocess ID. 31. Figur e 7.11 shows this ar r angement.
Dept.of CS&E,SJBIT
Page 123
UNIX SYSTEM PROGRAMMING
10CS62
Figure 7.11. Clientserver communication using FIFOs
32. This ar r angement wor k s, although it is im possi ble f or the ser ver to tell whether a client cr ashes. 33. This causes the client-s pecif ic FIFOs to be lef t in the f ile system. The ser ver also must catch SIGPIPE, since it's possi ble f or a client to send a r equest and ter minate bef or e r eading the r es ponse, leaving the client-s pecif ic FIFO with one wr iter (the ser ver ) and no r eader . 34. With the ar r angement shown in Figur e 7.11, if the ser ver o pens its well-k nown FIFO read-only (since it only reads f r om it) each time the num ber of clients goes f r om 1 to 0, the ser ver will read an end of f ile on the FIFO. To pr event the ser ver f r om having to handle this case, a common tr ick is just to have the ser ver o pen its well-k nown FIFO f or r eadwr ite. 7.6. XSI- X/Open System Interf ace: IPC
1. The thr ee ty pes of IPC that ar e called XSI IPC ar e message queues, sema phor es, and shar ed memor y.
Identif iers and K eys
2. Each IPC str uctur e (message queue, sema phor e, or shar ed memor y segment) in the ker nel is r ef er r ed to by a non-negative integer identif ier .
Dept.of CS&E,SJBIT
Page 124
UNIX SYSTEM PROGRAMMING
10CS62
3. To send or f etch a message to or f r om a message queue, f or exam ple, all we need know is the identif ier f or the queue. 4. Unlik e f ile descr i ptor s, IPC identif ier s ar e not small integer s. Indeed, when a given IPC str uctur e is cr eated and then r emoved, the identif ier associated with that str uctur e continually incr eases until it r eaches the maximum positive value f or an integer , and then wr a ps ar ound to 0. 5. The identif ier is an inter nal name f or an IPC o b ject. 6. Coo per ating pr ocesses need an exter nal naming scheme to be a ble to r endezvous using the same IPC o b ject. 7. For this pur pose, an IPC o b ject is associated with a key that acts as an exter nal name. 8. Whenever an IPC str uctur e is being cr eated ( by calling msgget, semget, or shmget), a key must be s pecif ied. 9. The data ty pe of this k ey is the pr imitive system data ty pe key _ t, which is of ten def ined as a long integer in the header . 10. This k ey is conver ted into an identif ier by the k er nel. 11. Ther e ar e var ious ways f or a client and a ser ver to r endezvous at the same IPC str uctur e. a. The ser ver can cr eate a new IPC str uctur e by s pecif ying a k ey of IPC _ PR IVATE and stor e the r etur ned identif ier somewher e (such as a f ile) f or the client to o btain. The disadvantage to this technique is that f ile system o per ations ar e r equir ed f or the ser ver to wr ite the integer identif ier to a f ile, and then f or the clients to r etr ieve this identif ier later . b. The client and the ser ver can agr ee on a k ey by def ining the k ey in a common header , f or exam ple. The ser ver then cr eates a new IPC str uctur e s pecif ying this key. The pr o blem with this a p pr oach is that it's possi ble f or the k ey to alr eady be associated with an IPC str uctur e, in which case the get function (msgget, semget, or shmget) r etur ns an er r or . The ser ver must handle this er r or , deleting the existing IPC str uctur e, and tr y to cr eate it again. c. The client and the ser ver can agr ee on a pathname and pr o ject ID (the pr o ject ID is a char acter value between 0 and 255) and call the f unction ftok to conver t these two values into a k ey. This k ey is then used in ste p 2. The only ser vice pr ovided by ftok is a way of gener ating a k ey f r om a pathname and pr o ject ID #include
Dept.of CS&E,SJBIT
Page 125
UNIX SYSTEM PROGRAMMING
10CS62
key _ t f tok (const char * path, int id); Retur ns: k ey if OK , (k ey _ t)-1 on er r or 12. The path ar gument must r ef er to an existing f ile. Only the lower 8 bits of id ar e used when gener ating the k ey. 13. The k ey cr eated by f tok is usually f or med by tak ing par ts of the st _ dev and st _ ino f ields in the stat str uctur e cor r es ponding to the given pathname and com bining them with the pr o ject ID. 14. If two pathnames r ef er to two dif f er ent f iles, then f tok usually r etur ns two dif f er ent keys f or the two pathnames. 15. However , because both i-node num ber s and k eys ar e of ten stor ed in long integer s, ther e can be inf or mation loss cr eating a k ey. This means that two dif f er ent pathnames to dif f er ent f iles can gener ate the same k ey if the same pr o ject ID is used. 16. The thr ee get f unctions (msgget, semget, and shmget) all have two similar ar guments: a k ey and an integer f lag. 17. A new IPC str uctur e is cr eated (nor mally, by a ser ver ) if either k ey is IPC _ PR IVATE or k ey is not cur r ently associated with an IPC str uctur e of the par ticular ty pe and the IPC _ CR EAT bit of f lag is s pecif ied. 18. To r ef er ence an existing queue (nor mally done by a client), k ey must equal the k ey that was s pecif ied when the queue was cr eated, and IPC _ CR EAT must not be s pecif ied. 19. Note that it's never possi ble to s pecif y IPC _ PR IVATE to r ef er ence an existing queue, since this s pecial k ey value always cr eates a new queue. 20. To r ef er ence an existing queue that was cr eated with a k ey of IPC _ PR IVATE, we must k now the associated identif ier and then use that identif ier in the other IPC calls (such as msgsnd and msgr cv), by passing the get f unction. 21. If we want to cr eate a new IPC str uctur e, mak ing sur e that we don't r ef er ence an existing one with the same identif ier , we must s pecif y a f lag with both the IPC _ CR EAT and IPC _ EXCL bits set. Doing this causes an er r or r etur n of EEXIST if the IPC str uctur e alr eady exists. Permission Structure
22. XSI IPC associates an i pc _ per m str uctur e with each IPC str uctur e.
Dept.of CS&E,SJBIT
Page 126
UNIX SYSTEM PROGRAMMING
10CS62
23. This str uctur e def ines the per missions and owner and includes at least the f ollowing mem ber s: str uct i pc _ per m { uid _ t uid; /* owner 's ef f ective user id */ gid _ t gid; /* owner 's ef f ective gr ou p id */ uid _ t cuid; /* cr eator 's ef f ective user id */ gid _ t cgid; /* cr eator 's ef f ective gr ou p id */ mode _ t mode; /* access modes */ . . . }; 24. Each im plementation includes additional mem ber s. See on your system for the com plete def inition. 25. All the f ields ar e initialized when the IPC str uctur e is cr eated. 26. Later , we can modif y the uid, gid, and mode fields by calling msgctl, semctl, or shmctl. 27. To change these values, the calling pr ocess must be either the cr eator of the IPC str uctur e or the su per user . 28. Changing these f ields is similar to calling chown or chmod for a f ile. 29. The values in the mode f ield ar e as shown below. st _ mode mask
Dept.of CS&E,SJBIT
Meaning
S _ IR USR
user -read
S _ IWUSR
user -wr ite
S _ IXUSR
user -execute
S _ IR GR P
gr oup-read
S _ IWGR P
gr oup-wr ite
S _ IXGR P
gr oup-execute
S _ IR OTH
other -read
S _ IWOTH
other -wr ite
S _ IXOTH
other -execute
Page 127
UNIX SYSTEM PROGRAMMING
10CS62
30. Below ar e the six per missions f or each f or m of IPC. Permission
Bit
user -read
0400
user -wr ite (alter )
0200
gr ou p-read
0040
gr ou p-wr ite (alter ) 0020 other -read
0004
other -wr ite (alter )
0002
Conf iguration Limits
31. All thr ee f or ms of XSI IPC have built-in limits that we may encounter . 32. Most of these limits can be changed by r econf igur ing the k er nel. Advantages and Disadvantages
33. A f undamental pr o blem with XSI IPC is that the IPC str uctur es ar e system wide and do not have a r ef er ence count. 34. For exam ple, if we cr eate a message queue, place some messages on the queue, and then ter minate, the message queue and its contents ar e not deleted. 35. They r emain in the system until s pecif ically r ead or deleted by some pr ocess calling msgr cv or msgctl, by someone executing the i pcr m command, or by the system being re booted. 36. Another pr o blem with XSI IPC is that these IPC str uctur es ar e not k nown by names in the f ile system. 37. We can't access them and modif y their pr o per ties. 38. Almost a dozen new system calls (msgget, semop, shmat, and so on) wer e added to the k er nel to su p por t these IPC o b jects. 39. We can't see the IPC o b jects with an ls command, we can't r emove them with the rm command, and we can't change their per missions with the chmod command. 40. Instead, two new commands i pcs(1) and i pcr m(1)wer e added.
Dept.of CS&E,SJBIT
Page 128
UNIX SYSTEM PROGRAMMING
10CS62
41. Since these f or ms of IPC don't use f ile descr i ptor s, we can't use the multi plexed I/O functions (select and poll) with them. 42. This mak es it har der to use mor e than one of these IPC str uctur es at a time or to use any of these IPC str uctur es with f ile or device I/O. 43. Other advantages f or message queues ar e that they'r e r elia ble, f low contr olled; r ecor d or iented, and can be pr ocessed in other than f ir st-in, f ir st-out or der . 44. The f ollowing com par es some of the f eatur es of these var ious f or ms of IPC.
IPC type
Connectionless? Reliable?
Flow control?
Records?
Message types or priorities?
message queues
no
yes
yes
yes
yes
STR EAMS
no
yes
yes
yes
yes
U NIX domain str eam sock et
no
yes
yes
no
no
U NIX domain datagr am sock et
yes
yes
no
yes
no
FIFOs (nonSTR EAMS)
no
yes
yes
no
no
7.7. Message Queues
1. A message queue is a link ed list of messages stor ed within the k er nel and identif ied by a message queue identif ier . 2. We will call the message queue just a queue and its identif ier a queue ID. 3. A new queue is cr eated or an existing queue o pened by msgget. 4. New messages ar e added to the end of a queue by msgsnd. 5. Every message has a positive long integer ty pe f ield, a non-negative length, and the actual data bytes (cor r es ponding to the length), all of which ar e s pecif ied to msgsnd when the message is added to a queue. 6. Messages ar e f etched f r om a queue by msgr cv. 7. We do not have to f etch the messages in a f ir st-in, f ir st-out or der . Instead, we can fetch messages based on their ty pe f ield. 8. Each queue has the f ollowing msqid _ ds str uctur e associated with it:
Dept.of CS&E,SJBIT
Page 129
UNIX SYSTEM PROGRAMMING
10CS62
str uct msqid _ ds { str uct i pc _ per m msg _ per m; msgqnum _ t msg _ qnum; /* # of messages on queue */ msglen _ t msg _ q bytes; /* max # of bytes on queue */ pid _ t msg _ ls pid; /* pid of last msgsnd() */ pid _ t msg _ lr pid; /* pid of last msgr cv() */ time _ t msg _ stime; /* last-msgsnd() time */ time _ t msg _ r time; /* last-msgr cv() time */ time _ t msg _ ctime; /* last-change time */ . . . }; 9. This str uctur e def ines the cur r ent status of the queue. The mem ber s shown ar e the ones def ined by the Single U NIX S pecif ication. Im plementations include additional fields not cover ed by the standar d. 10. The f ir st f unction nor mally called is msgget to either o pen an existing queue or cr eate a new queue. #include int msgget(k ey _ t k ey, int f lag); Retur ns: message queue ID if OK , 1 on er r or 11. We have alr eady seen the r ules f or conver ting the k ey into an identif ier and discussed whether a new queue is cr eated or an existing queue is r ef er enced. 12. When a new queue is cr eated, the f ollowing mem ber s of the msqid _ ds str uctur e ar e initialized.
The i pc _ per m str uctur e is initialized. The mode mem ber of this str uctur e is set to the cor r es ponding per mission bits of f lag.
msg _ qnum, msg _ ls pid, msg _ lr pid, msg _ stime, and msg _ r time ar e all set to 0.
msg _ ctime is set to the cur r ent time.
msg _ q bytes is set to the system limit.
13. On success, msgget r etur ns the non-negative queue ID. This value is then used with the other thr ee message queue f unctions. 14. The msgctl f unction per f or ms var ious o per ations on a queue.
Dept.of CS&E,SJBIT
Page 130
UNIX SYSTEM PROGRAMMING
10CS62
#include int msgctl(int msqid, int cmd, str uct msqid _ ds * buf ); Retur ns: 0 if OK , -1 on er r or 15. The cmd ar gument s pecif ies the command to be per f or med on the queue s pecif ied by msqid.
IPC _ STAT
Fetch the msqid _ ds str uctur e f or this queue, stor ing it in the str uctur e pointed to by buf .
IPC _ SET
Co py the f ollowing f ields f r om the str uctur e pointed to by buf to the msqid _ ds str uctur e associated with this queue: msg _ per m.uid, msg _ per m.gid, msg _ per m.mode, and msg _ q bytes. This command can be executed only by a pr ocess whose ef f ective user ID equals msg _ per m.cuid or msg _ per m.uid or by a pr ocess with su per user pr ivileges. Only the su per user can incr ease the value of msg _ q bytes.
Remove the message queue f r om the system and any data still on the queue. This r emoval is immediate. Any other pr ocess still using the message queue will get an er r or of EIDR M on its next attem pted o per ation on the queue. IPC _ R MID This command can be executed only by a pr ocess whose ef f ective user ID equals msg _ per m.cuid or msg _ per m.uid or by a pr ocess with su per user pr ivileges. 16. Data is placed onto a message queue by calling msgsnd. #include int msgsnd(int msqid, const void * ptr , size _ t n bytes, int flag); Retur ns: 0 if OK , -1 on er r or 17. As we mentioned ear lier , each message is com posed of a positive long integer ty pe field, a non-negative length (n bytes), and the actual data bytes (cor r es ponding to the length). 18. Messages ar e always placed at the end of the queue. 19. The ptr ar gument points to a long integer that contains the positive integer message ty pe, and it is immediately f ollowed by the message data. (Ther e is no message data if n bytes is 0.) 20. If the lar gest message we send is 512 bytes, we can def ine the f ollowing str uctur e:
Dept.of CS&E,SJBIT
Page 131
UNIX SYSTEM PROGRAMMING str uct mymesg { long mty pe; char mtext[512]; };
10CS62
/* positive message ty pe */ /* message data, of length n bytes */
21. The ptr ar gument is then a pointer to a mymesg str uctur e. The message ty pe can be used by the r eceiver to f etch messages in an or der other than f ir st in, f ir st out. 22. A f lag value of IPC _ NOWAIT can be s pecif ied. This is similar to the non block ing I/O f lag f or f ile I/O. 23. If the message queue is f ull, s pecif ying IPC _ NOWAIT causes msgsnd to r etur n immediately with an er r or of EAGAI N. 24. If IPC _ NOWAIT is not s pecif ied, we ar e block ed until ther e is r oom f or the message, the queue is r emoved f r om the system, or a signal is caught and the signal handler retur ns. 25. In the second case, an er r or of EIDR M is r etur ned ("identif ier r emoved"); in the last case, the er r or r etur ned is EI NTR . 26. Note how ungr acef ully the r emoval of a message queue is handled. Since a r ef er ence count is not maintained with each message queue, the r emoval of a queue sim ply gener ates err or s on the next queue o per ation by pr ocesses still using the queue. 27. When msgsnd r etur ns successf ully, the msqid _ ds str uctur e associated with the message queue is u pdated to indicate the pr ocess ID that made the call (msg _ ls pid), the time that the call was made (msg _ stime), and that one mor e message is on the queue (msg _ qnum). 28. Messages ar e r etr ieved f r om a queue by msgr cv. #include ssize _ t msgr cv(int msqid, void * ptr , size _ t n bytes , long ty pe, int f lag); Retur ns: size of data por tion of message if OK , -1 on er r or 29. The ptr ar gument points to a long integer (wher e the message ty pe of the r etur ned message is stor ed) f ollowed by a data buf f er f or the actual message data. 30. n bytes s pecif ies the size of the data buf f er . If the r etur ned message is lar ger than n bytes and the MSG _ NOER R OR bit in f lag is set, the message is tr uncated. 31. The ty pe ar gument lets us s pecif y which message we want.
Dept.of CS&E,SJBIT
Page 132
UNIX SYSTEM PROGRAMMING
10CS62
ty pe == 0
The f ir st message on the queue is r etur ned.
ty pe > 0
The f ir st message on the queue whose message ty pe equals ty pe is r etur ned.
ty pe < 0
The f ir st message on the queue whose message ty pe is the lowest value less than or equal to the a bsolute value of ty pe is r etur ned.
32. A nonzer o ty pe is used to r ead the messages in an or der other than f ir st in, f ir st out. 33. For exam ple, the ty pe could be a pr ior ity value if the a p plication assigns pr ior ities to the messages. Another use of this f ield is to contain the pr ocess ID of the client if a single message queue is being used by multi ple clients and a single ser ver (as long as a pr ocess ID f its in a long integer ). 34. We can s pecif y a f lag value of IPC _ NOWAIT to mak e the o per ation non block ing, causing msgr cv to r etur n -1 with er r no set to E NOMSG if a message of the s pecif ied ty pe is not availa ble. 35. If IPC _ NOWAIT is not s pecif ied, the o per ation block s until a message of the s pecif ied ty pe is availa ble, the queue is r emoved f r om the system (-1 is r etur ned with er r no set to EIDR M), or a signal is caught and the signal handler r etur ns (causing msgr cv to r etur n 1 with err no set to EI NTR ). 36. When msgr cv succeeds, the k er nel u pdates the msqid _ ds str uctur e associated with the message queue to indicate the caller 's pr ocess ID (msg _ lr pid), the time of the call (msg _ r time), and that one less message is on the queue (msg _ qnum).
7.8. Semaphores 1. A sema phor e is not a f or m of IPC. A sema phor e is a counter used to pr ovide access to a shar ed data o b ject f or multi ple pr ocesses. 2. To o btain a shar ed r esour ce, a pr ocess needs to do the f ollowing: a.) Test the sema phor e that contr ols the r esour ce. b.) If the value of the sema phor e is positive, the pr ocess can use the r esour ce. In this case, the pr ocess decr ements the sema phor e value by 1, indicating that it has used one unit of the r esour ce. c.) Other wise, if the value of the sema phor e is 0, the pr ocess goes to slee p until the sema phor e value is gr eater than 0. When the pr ocess wak es u p, it r etur ns to ste p 1. 3. When a pr ocess is done with a shar ed r esour ce that is contr olled by a sema phor e, the sema phor e value is incr emented by 1. If any other pr ocesses ar e aslee p, waiting f or the sema phor e, they ar e awak ened.
Dept.of CS&E,SJBIT
Page 133
UNIX SYSTEM PROGRAMMING
10CS62
4. To im plement sema phor es cor r ectly, the test of a sema phor e's value and the decr ementing of this value must be an atomic o per ation. For this r eason, sema phor es ar e nor mally im plemented inside the ker nel. 5. A common f or m of sema phor e is called a binar y sema phor e. It contr ols a single resour ce, and its value is initialized to 1. 6. In gener al, however , a sema phor e can be initialized to any positive value, with the value indicating how many units of the shar ed r esour ce ar e availa ble f or shar ing. 7. XSI sema phor es ar e, unf or tunately, mor e com plicated than this. Thr ee f eatur es contr i bute to this unnecessar y com plication. a.) A sema phor e is not sim ply a single non-negative value. Instead, we have to def ine a sema phor e as a set of one or mor e sema phor e values. When we cr eate a sema phor e, we s pecif y the num ber of values in the set. b.) The cr eation of a sema phor e (semget) is inde pendent of its initialization (semctl). This is a f atal f law, since we cannot atomically cr eate a new sema phor e set and initialize all the values in the set. c.) Since all f or ms of XSI IPC r emain in existence even when no pr ocess is using them, we have to wor r y a bout a pr ogr am that ter minates without r eleasing the sema phor es it has been allocated. The undo f eatur e that we descr i be later is su p posed to handle this. 8. The k er nel maintains a semid _ ds str uctur e f or each sema phor e set: str uct semid _ ds { str uct i pc _ per m unsigned shor t time _ t time _ t . . . };
sem _ per m; sem _ nsems; sem _ otime; sem _ ctime;
/* # of sema phor es in set */ /* last-semo p() time */ /* last-change time */
9. The Single U NIX S pecif ication def ines the f ields shown, but im plementations can def ine additional mem ber s in the semid _ ds str uctur e. 10. Each sema phor e is r e pr esented by an anonymous str uctur e containing at least the following mem ber s: str uct {
Dept.of CS&E,SJBIT
Page 134
UNIX SYSTEM PROGRAMMING unsigned shor t 0 */ pid _ t unsigned shor t semval>cur val */ unsigned shor t . . . };
10CS62
semval;
/* sema phor e value, always >=
sem pid; semncnt;
/* pid f or last o per ation */ /* # pr ocesses awaiting
semzcnt;
/* # pr ocesses awaiting semval==0 */
11. The ta ble below lists the system limits that af f ect sema phor e sets. Typical values Description
FreeBSD 5.2.1
Linux 2.4.22
Mac OS X 10.3
Solaris 9
The maximum value of any sema phor e
32,767
32,767
32,767
32,767
The maximum value of any sema phor e's ad just-on-exit value
16,384
32,767
16,384
16,384
The maximum num ber of sema phor e sets, systemwide
10
128
87,381
10
The maximum num ber of sema phor es, systemwide
60
32,000
87,381
60
The maximum num ber of sema phor es per sema phor e set
60
250
87,381
25
The maximum num ber of undo str uctur es, systemwide
30
32,000
87,381
30
The maximum num ber of undo entr ies per undo str uctur es
10
32
10
10
100
32
100
10
The maximum num ber of o per ations per semo p call
12. The f ir st f unction to call is semget to o btain a sema phor e ID. #include int semget(k ey _ t k ey, int nsems, int f lag); Retur ns: sema phor e ID if OK , -1 on er r or
Dept.of CS&E,SJBIT
Page 135
UNIX SYSTEM PROGRAMMING
10CS62
13. We have alr eady seen the r ules f or conver ting the k ey into an identif ier and discussed whether a new set is cr eated or an existing set is r ef er enced. 14. When a new set is cr eated, the f ollowing mem ber s of the semid _ ds str uctur e ar e initialized.
The i pc _ per m str uctur e is initialized. The mode mem ber of this str uctur e is set to the cor r es ponding per mission bits of f lag.
sem _ otime is set to 0.
sem _ ctime is set to the cur r ent time.
sem _ nsems is set to nsems.
15. The num ber of sema phor es in the set is nsems. 16. If a new set is being cr eated (ty pically in the ser ver ), we must s pecif y nsems. If we ar e r ef er encing an existing set (a client), we can s pecif y nsems as 0. 17. The semctl f unction is the catchall f or var ious sema phor e o per ations. #include int semctl(int semid, int semnum, int cmd, ... /* union semun ar g */); 18. The f our th ar gument is o ptional, de pending on the command r equested, and if pr esent, is of ty pe semun, a union of var ious command-s pecif ic ar guments: union semun { int str uct semid _ ds unsigned shor t };
val; * buf ; *ar r ay;
/* f or SETVAL */ /* f or IPC _ STAT and IPC _ SET */ /* f or GETALL and SETALL */
19. Note that the o ptional ar gument is the actual union, not a pointer to the union. 20. The cmd ar gument s pecif ies one of the f ollowing ten commands to be per f or med on the set s pecif ied by semid. 21. The f ive commands that r ef er to one par ticular sema phor e value use semnum to s pecif y one mem ber of the set. The value of semnum is between 0 and nsems-1, inclusive.
IPC _ STAT
Fetch the semid _ ds str uctur e f or this set, stor ing it in the str uctur e pointed to by ar g b . uf .
Dept.of CS&E,SJBIT
Page 136
UNIX SYSTEM PROGRAMMING
10CS62
IPC _ STAT
Fetch the semid _ ds str uctur e f or this set, stor ing it in the str uctur e pointed to by ar g b . uf .
IPC _ SET
Set the sem _ per m.uid, sem _ per m.gid, and sem _ per m.mode f ields f r om the str uctur e pointed to by ar g b . uf in the semid _ ds str uctur e associated with this set. This command can be executed only by a pr ocess whose ef f ective user ID equals sem _ per m.cuid or sem _ per m.uid or by a pr ocess with su per user pr ivileges.
Remove the sema phor e set f r om the system. This r emoval is immediate. Any other pr ocess still using the sema phor e will get an er r or of EIDR M on IPC _ R MID its next attem pted o per ation on the sema phor e. This command can be executed only by a pr ocess whose ef f ective user ID equals sem _ per m.cuid or sem _ per m.uid or by a pr ocess with su per user pr ivileges. GETVAL
Retur n the value of semval f or the mem ber semnum.
SETVAL
Set the value of semval f or the mem ber semnum. The value is s pecif ied by ar g.val.
GETPID
Retur n the value of sem pid f or the mem ber semnum.
GET NC NT Retur n the value of semncnt f or the mem ber semnum. GETZC NT Retur n the value of semzcnt f or the mem ber semnum. GETALL
Fetch all the sema phor e values in the set. These values ar e stor ed in the ar r ay pointed to by ar g.ar r ay.
SETALL
Set all the sema phor e values in the set to the values pointed to by ar g.ar r ay.
22. For all the GET commands other than GETALL, the f unction r etur ns the cor r es ponding value. For the r emaining commands, the r etur n value is 0. 23. The f unction semo p atomically per f or ms an ar r ay of o per ations on a sema phor e set. #include int semo p(int semid, str uct sem buf semo par r ay[],size _ t no ps); Retur ns: 0 if OK , -1 on er r or 24. The semo par r ay ar gument is a pointer to an ar r ay of sema phor e o per ations, re pr esented by sem buf str uctur es: str uct sem buf { unsigned shor t shor t
Dept.of CS&E,SJBIT
sem _ num; sem _ o p;
/* mem ber # in set (0, 1, ..., nsems-1) */ /* o per ation (negative, 0, or positive) */
Page 137
UNIX SYSTEM PROGRAMMING shor t */ };
sem _ f lg;
10CS62 /* IPC _ NOWAIT, SEM _ U NDO
25. The no ps ar gument s pecif ies the num ber of o per ations (elements) in the ar r ay. 26. The o per ation on each mem ber of the set is s pecif ied by the cor r es ponding sem _ op value. 27. This value can be negative, 0, or positive. a.) The easiest case is when sem _ op is positive. This case cor r es ponds to the retur ning of r esour ces by the pr ocess. The value of sem _ op is added to the sema phor e's value. If the undo f lag is s pecif ied, sem _ op is also su btr acted f r om the sema phor e's ad justment value f or this pr ocess. b.) If sem _ op is negative, we want to o btain r esour ces that the sema phor e contr ols. If the sema phor e's value is gr eater than or equal to the a bsolute value of sem _ op (the r esour ces ar e availa ble), the a bsolute value of sem _ op is su btr acted f r om the sema phor e's value. This guar antees that the r esulting value f or the sema phor e is gr eater than or equal to 0. If the undo f lag is s pecif ied, the a bsolute value of sem _ o p is also added to the sema phor e's ad justment value f or this pr ocess. If the sema phor e's value is less than the a bsolute value of sem _ op (the r esour ces ar e not availa ble), the f ollowing conditions a p ply. a. If IPC _ NOWAIT is s pecif ied, semop retur ns with an er r or of EAGAIN. b. If IPC _ NOWAIT is not s pecif ied, the semncnt value f or this sema phor e is incr emented (since the caller is a bout to go to slee p), and the calling pr ocess is sus pended until one of the f ollowing occur s. i. The sema phor e's value becomes gr eater than or equal to the a bsolute value of sem _ op (i.e., some other pr ocess has r eleased some r esour ces). The value of semncnt f or this sema phor e is decr emented (since the calling pr ocess is done waiting), and the a bsolute value of sem _ op is su btr acted f r om the sema phor e's value. If the undo f lag is s pecif ied, the a bsolute value of sem _ op is also added to the sema phor e's ad justment value f or this pr ocess. ii. The sema phor e is r emoved f r om the system. In this case, the function r etur ns an er r or of EIDR M. iii. A signal is caught by the pr ocess, and the signal handler r etur ns. In this case, the value of semncnt f or this sema phor e is decr emented (since the calling pr ocess is no longer waiting), and the f unction retur ns an er r or of EI NTR . c.) If sem _ op is 0, this means that the calling pr ocess wants to wait until the sema phor e's value becomes 0.
Dept.of CS&E,SJBIT
Page 138
UNIX SYSTEM PROGRAMMING
10CS62
If the sema phor e's value is cur r ently 0, the f unction r etur ns immediately. If the sema phor e's value is nonzer o, the f ollowing conditions a p ply. a. If IPC _ NOWAIT is s pecif ied, r etur n is made with an er r or of EAGAIN. b. If IPC _ NOWAIT is not s pecif ied, the semzcnt value f or this sema phor e is incr emented (since the caller is a bout to go to slee p), and the calling pr ocess is sus pended until one of the f ollowing occur s. i. The sema phor e's value becomes 0. The value of semzcnt f or this sema phor e is decr emented (since the calling pr ocess is done waiting). ii. The sema phor e is r emoved f r om the system. In this case, the function r etur ns an er r or of EIDR M. iii. A signal is caught by the pr ocess, and the signal handler r etur ns. In this case, the value of semzcnt f or this sema phor e is decr emented (since the calling pr ocess is no longer waiting), and the f unction retur ns an er r or of EI NTR . 28. The semop f unction o per ates atomically; it does either all the o per ations in the ar r ay or none of them. Semaphore Ad justment on exit
29. It is a pr o blem if a pr ocess ter minates while it has r esour ces allocated thr ough a sema phor e. 30. Whenever we s pecif y the SEM _ U NDO f lag f or a sema phor e o per ation and we allocate r esour ces (a sem _ op value less than 0), the k er nel r emem ber s how many resour ces we allocated f r om that par ticular sema phor e (the a bsolute value of sem _ op). 31. When the pr ocess ter minates, either voluntar ily or involuntar ily, the k er nel check s whether the pr ocess has any outstanding sema phor e ad justments and, if so, a p plies the ad justment to the cor r es ponding sema phor e. 32. If we set the value of a sema phor e using semctl, with either the SETVAL or SETALL commands, the ad justment value f or that sema phor e in all pr ocesses is set to 0.
Dept.of CS&E,SJBIT
Page 139
UNIX SYSTEM PROGRAMMING
10CS62
UNIT - 8 NETWOR K IPC: SOCK ETS Sock et Descriptors
o
A sock et is an abstraction of a communication endpoint. To create a sock et we can mak e a call the f ollowing f unction.
o
The sock et communication domains
o
Dept.of CS&E,SJBIT
Page 140
UNIX SYSTEM PROGRAMMING
10CS62
Addressing Byte Ordering •
•
Byte order f or test platf orms
Functions to convert between network byte order & Processor byte order
Address Formats Dept.of CS&E,SJBIT
Page 141
UNIX SYSTEM PROGRAMMING
10CS62
str uct sock addr
•
{ sa _ f amily _ t sa _ f amily; /* addr ess f amily */ char sa _ data[]; /* var ia ble-length addr ess */ . . . }; •
str uct sock addr
{ sa _ f amily _ t sa _ f amily; /* addr ess f amily */ char sa _ data[14]; /* var ia ble-length addr ess */ }; str uct in _ addr { in _ addr _ t s _ addr ; };
/* IPv4 addr ess */
str uct sock addr _ in { sa _ f amily _ t sin _ f amily; /* addr ess f amily */ in _ por t _ t sin _ por t; /* por t num ber */ str uct in _ addr sin _ addr ; /* IPv4 addr ess */ }; str uct in6 _ addr { uint8 _ t s6 _ addr [16]; };
/* IPv6 addr ess */
str uct sock addr _ in6 { sa _ f amily _ t sin6 _ f amily; /* addr ess f amily */ in _ por t _ t sin6 _ por t; /* por t num ber */ uint32 _ t sin6 _ f lowinf o; /* tr af f ic class and f low inf o */ str uct in6 _ addr sin6 _ addr ; /* IPv6 addr ess */ uint32 _ t sin6 _ sco pe _ id; /* set of inter f aces f or sco pe */ };
To pr int the addr ess in Human under standa ble f or m Dept.of CS&E,SJBIT
Page 142
UNIX SYSTEM PROGRAMMING
10CS62
Addr ess Look u p •
The hosts k nown by a given com puter system
str uct hostent { char *h _ name; /* name of host */ char **h _ aliases; /* pointer to alter nate host name ar r ay */ int h _ addr ty pe; /* addr ess ty pe */ int h _ length; /* length in bytes of addr ess */ char **h _ addr _ list; /* pointer to ar r ay of networ k addr esses */ . . . };
networ k names and num ber s with a similar set of inter f aces Dept.of CS&E,SJBIT
Page 143
UNIX SYSTEM PROGRAMMING
str uct netent { char *n _ name; char **n _ aliases; int n _ addr ty pe; uint32 _ t n _ net; };
10CS62
/* networ k name */ /* alter nate networ k name ar r ay pointer */ /* addr ess ty pe */ /* networ k num ber */
To ma p between pr otocol names and num ber s
str uct pr otoent { Dept.of CS&E,SJBIT
Page 144
UNIX SYSTEM PROGRAMMING
10CS62
char * p _ name; /* pr otocol name */ char ** p _ aliases; /* pointer to alter nate pr otocol name ar r ay */ int p _ pr oto; /* pr otocol num ber */ . . . };
To Ma p between Ser vice name to por t num ber and vice ver sa
str uct ser vent { char *s _ name; /* ser vice name */ char **s _ aliases; /* pointer to alter nate ser vice name ar r ay */ int s _ por t; /* por t num ber */ char *s _ pr oto; /* name of pr otocol */ . . . };
To ma p f r om a host name and a ser vice name to an addr ess and vice ver sa
Dept.of CS&E,SJBIT
Page 145
UNIX SYSTEM PROGRAMMING
10CS62
str uct addr inf o { int ai _ f lags; /* customize behavior */ int ai _ f amily; /* addr ess f amily */ int ai _ sock ty pe; /* sock et ty pe */ int ai _ pr otocol; /* pr otocol */ sock len _ t ai _ addr len; /* length in bytes of addr ess */ str uct sock addr *ai _ addr ; /* addr ess */ char *ai _ canonname; /* canonical name of host */ str uct addr inf o *ai _ next; /* next in list */ . };
Flags f or addrinf o structure
Dept.of CS&E,SJBIT
Page 146
UNIX SYSTEM PROGRAMMING
10CS62
To Handle the er r or Messages #include const char *gai _ str er r or (int er r or );
To converts an address into a host name and a service name
Associating Addresses with Sock ets
Dept.of CS&E,SJBIT
Page 147