unix. Профессиональное программирование
DESCRIPTION
UNIXПрофессиональноепрограммирование,У. Ричард Стивенс,Стивен А. РагоUNIX. Профессиональное программирование» представляет собой подробнейшее справочное руководство для любого профессионального программиста, работающего с UNIX.TRANSCRIPT
-
Books.Ru ISBN 5932860898, UNIX. , 2 Books.Ru . , . , ([email protected]), .
-
Advanced Programming in the UNIX Environment
W. Richard Stevens,Stephen A. Rago
Second Edition
-
UNIX
. , .
2007
-
High tech
. , .
UNIX. , 2
.
. . . . . . .
., .UNIX. , 2 . .: , 2007. 1040 ., .ISBN 5932860898
UNIX. , UNIX. , . , UNIX. , , (IPC), , POSIX.1. , 4 : FreeBSD 5.2.1, Linux 2.4.22, Slaris 9 Mac OS X 10.3. , UNIX, , , POSIX.1 Single UNIX Specification.
ISBN13: 9785932860892ISBN10: 5932860898ISBN 0201433079 ()
, 2007Authorized translation of the English edition 2005 Pearson Education, Inc. Thistranslation is published and sold by permission of Pearson Education, Inc., the ownerof all rights to publish and sell the same.
, . , , .
. 199034, , 16 , 7,. (812) 3245353, [email protected]. N 000054 25.12.98.
00593, 2; 953000 .
18.04.2007. 70100 1/16 . . 65 . . 2000 . N
199034, , 9 , 12.
-
1.
1.1.1.2.1.3.1.4.1.5.1.6.1.7.1.8.1.9.1.101.111.12
2.
2.1.2.2.
2.3.
2.4.
. . . . . . . . . . . . . . . . . . . . . . . . 15
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
. . . . . . . . . . . . . . . . . . . 19
UNIX. . . . . . . . . . . . . . . . . . . . . . . . . . . 27
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 UNIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48. . . . . . . . . . . . . . . . . 49. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
UNIX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 UNIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.2.1. ISO C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532.2.2. IEEE POSIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552.2.3. Single UNIX Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . 632.2.4. FIPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
UNIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652.3.1. UNIX System V Release 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652.3.2. 4.4BSD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662.3.3. FreeBSD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672.3.4. Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672.3.5. Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672.3.6. Solaris . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682.3.7. UNIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
. . . . . . . . . . . . . . . . . . . 68
-
72.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692.5.1. ISO C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702.5.2. POSIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722.5.3. XSI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
2.6.2.7.2.8.2.9.2.10
3.
3.1.3.2.3.3.3.4.3.5.3.6.3.7.3.8.3.9.3.103.113.123.133.143.153.163.17
4.
4.1.4.2.4.3.4.4.4.5.4.6.4.7.4.8.4.9.4.104.112.5.4. sysconf, pathconf fpathconf . . . . . . . . . . . . . . . . 752.5.5. . . . . . . . . 84
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 . . . 92 . . . . . . . . . . . . . . . . . . . . . . 93 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 open. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 creat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100 close . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101 lseek . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101 read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .105 write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106 . . . . . . . . . . . . . . . . . . . . .107. . . . . . . . . . . . . . . . . . . . . . . . . .109. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113. dup dup2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115. sync, fsync fdatasync . . . . . . . . . . . . . . . . . . . . . . . . . .117. fcntl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .118. ioctl. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124. /dev/fd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129 stat, fstat lstat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .130 setuserID setgroupID. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135 . . . . . . . . . . . . . . . . .138 access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139 umask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .140 chmod fchmod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .143. sticky . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146. chown, fchown lchown . . . . . . . . . . . . . . . . . . . . . . . . .146
-
84.12. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1484.13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1494.14. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1504.15. link, unlink, remove rename. . . . . . . . . . . . . . . . . . . . .1534.164.174.184.194.204.214.224.234.244.25
5.
5.1.5.2.5.3.5.4.5.5.5.6.5.7.5.8.5.9.5.105.115.125.135.145.15
6.
6.1.6.2.6.3.6.4.6.5.6.6.6.7.6.8.6.9.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .157
. symlink readlink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .160
. . . . . . . . . . . . . . . . . . . . . . . .161
. utime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162
. mkdir rmdir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167
. chdir, fchdir getcwd . . . . . . . . . . . . . . . . . . . . . . . . . . .172
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .175
. . . . . . . . . . . . . . . . . . . . .177
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .179
. . . . . . . . . . . . . . . . . . . . .181
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181 FILE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181 , . . .183 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .186 . . . . . . . . . . . . . . . . . . . . . . . . . .189 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .192 . . . . . . . . . .193 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .196. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198. . . . . . . . . . . . . . . . . . . . . . . . . . . . .199. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .207. . . . . . . . .211. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .212
. . . . . . . . . . . . . . . . . . . . .213
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .213 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .213 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219 . . . . . . . . . . . . . . . . . . . .220 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .223 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .224 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .225
-
96.10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2276.11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .232
7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .234
7.1.7.2.7.3.7.4.7.5.7.6.7.7.7.8.7.9.7.107.117.12
8.
8.1.8.2.8.3.8.4.8.5.8.6.8.7.8.8.8.9.8.108.118.128.138.148.158.168.17
9.
9.1.9.2.9.3.9.4.9.5.9.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .234 main . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .234 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .240 . . . . . . . . . . . . . . . . . . . . . . . . . . . .240 C . . . . . . . . . . . . . . . . . . .241 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .243 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .244 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .248. setjump longjump . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252. getrlimit setrlimit . . . . . . . . . . . . . . . . . . . . . . . . . . . . .259. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .266
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .266 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .266 fork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .268 vfork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .274 exit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .276 wait waitpid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .279 waitid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .285 wait3 wait4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .286 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287. exec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .291. . . . . . . . .298. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304. system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .308. . . . . . . . . . . . . . . . . . .313. . . . . . . . . . . . . . . . . . . . . . . . . . . . .320. . . . . . . . . . . . . . . . . . . . . . .320. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .323
. . . . . . . . . . . . . . . . . . . . . . .325
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325 . . . . . . . . . . . . . . . . . . . .331 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .333 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .335 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337
-
10
9.7. tcgetpgrp, tcsetpgrp tcgetsid . . . . . . . . . . . . . . . . . . . . .3399.8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3409.9. . . . . . . . . . . . . . . . .3439.10. . . . . . . . . . . . . . . . . . . . . . . . . . . .3499.119.12
10.
10.110.210.310.410.510.610.710.810.910.110.110.110.110.110.110.110.110.110.110.210.210.2
11.
11.111.211.311.411.511.611.7
12.
12.112.2. FreeBSD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .352
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .355
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .356
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .356
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .356
. signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .371
. . . . . . . . . . . . . . . . . . . . . . . . . . . .373
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .376
. SIGCLD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .379
. . . . . . . . . . . . . .382
. kill raise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3830. alarm pause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3851. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3912. sigprocmask. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3933. sigpending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3944. sigaction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3975. sigsetjmp siglongjmp . . . . . . . . . . . . . . . . . . . . . . . . .4036. sigsuspend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4077. abort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4148. system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4179. sleep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4220. . . . . . . . . . . . . . . . . . . . . . . . . .4241. . . . . . . . . . . . . . . . . . . . . . . . . . . .4272. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .429
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .431
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .431
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .431
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .433
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .434
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .437
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .445
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .465
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .465
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .465
-
11
12.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46612.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47212.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48012.6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48512.712.812.912.112.1
13.
13.113.213.313.413.513.613.713.8
14.
14.114.214.314.414.5
14.6
14.714.814.914.1
15.
15.115.215.315.415.515.6. . . . . . . . . . . . . . . . . . . . . . .490
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .494
. fork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4980. . . . . . . . . . . . . . . . . . . . . . . . . .5021. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .503
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .504
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .504
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .504
. . . . . . . . . . . . . . . . . . . . . . .506
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .510
. . . . . . . . . . . . . . . . . . . . . . . .515
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .517
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .522
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .522
. . . . . . . . . . . . . . . . . . . . . .523
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .523
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .523
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .527
. STREAMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .544
. . . . . . . . . . . . . . . . . . . . . . . .55814.5.1. select pselect . . . . . . . . . . . . . . . . . . . . . . . . . . 56114.5.2. poll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56914.6.1. System V . . . . . . . . . . . . . . . 57014.6.2. BSD. . . . . . . . . . . . . . . . . . . . 571
. readv writev . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .571
. readn writen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .574
. . . . . . . . . . . .5760. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .583
. . . . . . . . . . . . . . . . . . . . . . . . . . . .585
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .585
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .586
. popen pclose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .594
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .601
. FIFO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .605
. XSI IPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .609
-
12
15.6.1. . . . . . . . . . . . . . . . . . . . . . . . . . 61015.6.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . 61115.6.3. . . . . . . . . . . . . . . . . . . . . . . . 61215.6.4. . . . . . . . . . . . . . . . . . . . . . . . 613
15.715.815.915.115.1
16.
16.116.216.3
16.416.516.616.716.816.9
17.
17.117.2
17.3
17.4
17.517.617.7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .615
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .621
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6280.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6361. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .639
: . . . . . . . . . . . . . .642
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .642
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .643
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64716.3.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64716.3.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64916.3.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65116.3.4. . . . . . . . . . . . . . . . . . . . . . . . 659
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .660
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .664
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .679
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .682
. . . . . . . . . . . . . . . .683
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .684
IPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .686
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .686
. STREAMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68617.2.1. STREAMS . . . . . . . . . . . . . . . . . . . 69017.2.2. . . . . . . . . . . . . . . . . . . . . . . . . . . 691
. UNIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69517.3.1. UNIX . . . . . . . . . . . . . . . . . 69617.3.2. . . . . . . . . . . . . . . . . . . . . . . . . . . 698
. . . . . . . . . . . . . . . . . . . . . . . . . . . .70317.4.1.
STREAMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70517.4.2.
UNIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708. , 1 . . . . . . . . . . . . . . . . . . . . . . . .717. , 2 . . . . . . . . . . . . . . . . . . . . . . . .723. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731
-
13
18. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .733
18.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73318.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73318.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74218.418.518.618.718.818.918.118.118.118.118.1
19.
19.119.219.3
19.419.519.619.719.8
20.
20.120.220.320.420.520.620.720.820.920.1. . . . . . . . . . .748
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .749
. stty. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .757
. . . . . . . . . . . . . . . . .758
. . . . . . . . . . . . . . . . . . . . . . . .759
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7600. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7661. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7692. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7763. termcap, terminfo curses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7784. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .779
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .781
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .781
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .781
. . . . . . . . . . . . . . . . . . . . .78819.3.1. STREAMS . . . . . . . . . . . . . 79019.3.2. BSD . . . . . . . . . . . . . . . . . . . . . . . . . . 79319.3.3. Linux . . . . . . . . . . . . . . . . . . . . . . . . . 797
. pty_fork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .799
. pty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .801
. pty . . . . . . . . . . . . . . . . . . . . . . . . . . . .806
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .814
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .815
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .818
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .818
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .818
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .820
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .822
. ? . . . . . . . . . . . . . . . . . . . .826
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .828
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .829
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .830
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8580. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .864
-
14
21. . . . . . . . . . . . . . . . . . . . . . . .866
21.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86621.2. . . . . . . . . . . . . . . . . . . . . . . . . . .86621.3. . . . . . . . . . . . . . . . . . . . . . . . . . . .86921.421.521.6
A.
B.
C.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .870
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 872
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .919
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .921
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .956
. . . . . . . . . . . . . . . . . .965
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1000
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1008
-
,
, , (
, . le OS X, , (Stephen Rago), , , UNIX UNIX . . ,
UNIX UNIX . .
(Mukesh Kacker), Pronto Networks, Inc.
, UNIX.
. (Eric S. Raymond), The Art of UNIX Programming
, UNIX.
. . ,
UNIX, , , POSIX 1003.1 2004 ) Single UNIX Specification, Version 3.
(Andrew Josey), The Open Group POSIX 1003.1
, UNIX. , GNU/Linux App , . .
(Dr. Benjamin Kuperman), . (Swarthmore)
-
Ad C.
ming in the Ad ,
vanced Programming in the UNIX Environment , UNIX , .
UniForum Monthly
Advanced Program UNIX Environment, (disonWesley), . , 1992 . .
Open Systems Today
UNIX Advanced Programming in the UNIX Environment, (AddisonWesley). , .
RS/Magazine
-
,
, nition, POSIX ,
(Maurice Ba : , UNIX ?.
: , . , .
. 70 , , , . , Microsoft Intel, , .
UNIX, , , , . 70 80 XX AT&T UNIX,
, . , AT&T SVID (System V Interface Defi System V),
. , UNIX , , . , UNIX , .
, , UNIX, , , . ch) The Design of the Unix Operating System. ,, , . (Brian Kernighan) , , (Rich Stevens). , ,
-
18
, .
, Linux UNIX ,
, . (Steve Rago) , ,
ISO IEEE . .
.
(Dennis Ritchie) ,
2005
-
. ,
. Illustrated, .
S Solaris,
,
UNIX Network Programming., , . 1999 . , , USENIX .
. 1993 UNIX System V Network Programming, , UNIX Network Programming,
System V. , , TCP/IP
, STREAMS. , , ,
AddisonWesley , , , . 13 . UNIX , .
ystem V Linux. , UNIX, Linux, . , System V Release 4, .
4.4BSD (CSRG Computing Science Research Group)
UNIX, , .
-
20
Linux, , , UNIX , . Linux , ,
Compute
199UNIX Envir UNvanced Prog
13
, IS
ThePOSIX.1
munication) BSD.
, Appler Mac , Mach FreeBSD.
, , .
2 Advanced Programming in theonment, IX. : Adramming in the UNIX Environment. ,
.
. ,
. , UNIX.
, , . 2, .
POSIX.1 2001 1990 , .O C 1990 1999 , , POSIX.1.
POSIX.1 . Single UNIX Specification ( Open Group, X/Open) POSIX.1. 1003.1 , .
, . , .
POSIX.1. (IPC Interprocess Com, , , IPC.
-
21
, POSIX.1. , . .
Post.
, . book.com/losmodem.pdf).
book.com. :
1. FreeBSD I
2. Linux 2.UNIXPentium.
3. Solaris 9tems,
4. Darwin 7
, ( ,
(Kward), , . , Script , , , .
, , , : PostScript (http://www.apue;tchapter/modem.ps) PDF (http://www.apuebook.com/lostchapter/
www.apue;
5.2.1 , 4.4BSD, ntel Pentium.
4.22 ( Mandrake 9.2) , Intel
System V Release 4 Sun Microsys 64 UltraSPARCIIi.
.4.0 , FreeBSD Mach, Apple Mac OS X, 10.3, PowerPC.
, , .
. , ), ,
. (Jeanne) Linux .
. (David Bausum), (David Boreham), eith Bostic), (Mark Ellis), (Phil Ho (Andrew Josey), (Mukesh Kacker),
-
22
(Brian Kernighan), (Bengt Kleberg), (Ben Kuperman), (Eric Raimond) (Andy Rudoff).
, , . Franz),
, 2005
,
call interfac
Solaris, , , , UNIX. AddisonWesley, (Tyrrell Albaugh), (Mary (John Fuller), (Karen Gettman), (Jessica Goldstein), (Noreen Regina) (John Wait). (Evelyn Pyle)
.
, , .
UNIX: , C. , UNIX.
, UNIX : , , ,
. . (systeme). C
, , C ( , ).
Unix Programmers Manual (
UNIX). . , .
-
23
UNIX UNIX, 80 , , 80. ANSI C,
1. UNIX,
2. 5)
3. (
4. ( 11 (
5. IPC
6. Script
, UN
IEEE POSIX ( ) X/Open.
. , System V Release 4 4.4BSD. , , .
:
, ( 1). UNIX UNIX ( 2).
: ( 3), ( 4), ( ( 6).
: UNIX ( 7), 8), ( 9) ( 10).
: ), ( 12)
13).
( 14 15).
: ( 16), Post ( 17), ( 18) ( 19).
C, UNIX. UNIX.
UNIX , IX.
10 000 . C. , ANSI C. ,
-
24
Unix Programmers Manual( UNIX) , .
vprog.tar.Z.
. UNIX
Com BSD Net 2 4.xBSDAT&T. XPG UNIX
198
4.3BSD
SVR3 . ,
, .
, ( 16, 17, 18 19). , .
. FTP ftp.uu.net, published/books/stevens.ad; .
, , .
System V 4.xBSD.
4.xBSD puter Systems Research Group . BSD Net 1 . SVRx System V Release x 3 X/Open Portability Guide, issue 3 ( X/Open , 3). ANSI C ANSI C. POSIX.1 ISO IEEE .
UNIX 2.2 2.3.
6 1987 1988 1989 1990 1991 1992
4.3BSD Tahoe
BSD Net 1
4.3BSD Reno
BSD Net 2
4.4BSD ?
4.3 + BSD
.0 SVR3.1 SVR3.2 SVR4
XPG3 ANSI C POSIX.1
-
25
4.3+BSD UNIX, BSD Net 2 4.4BSD. 4.4BSD , 4.4BSD. ,
UN
1. UNIX Sy(UHC),
2. 4.3+BSD Hewle
3. BSD/386
4. SunOS, S
,
. System Ellis), (Evi (Dave Preso
keley CSRG (PetSacksen) 4.3+BSD.
IX:
stem V/386 Release 4.0 Version 2.0 ( SVR4) U.H. Corp. Intel 80386.
Computer Systems Research Group, , , tt Packard.
( BSD Net 2) Berkeley Software Design Inc., Intel 80386. , 4.3+BSD.
4.1.1 4.1.2 Sun Microsystems (, , ,
ystem V), SPARC SLC.
.
, .
. , , , .
, , . V. AddisonWesley,
: (Maury Bach), (Mark (Jeff Gitlin), (Piter Honeyman),
(John Linderman), (Doug McIlroy), Nemeth), (Craig Partridge), tto), (Gary Wilson) (Gary Wright).
(Keith Bostic) c (Kirk McKusick) U.C. Ber , BSD ( er Salus)). (Sam Nataros) (Joachim UHC SVR4
-
26
. (Trent Hein) BSD/386.
: (Paul Lucchina), (Joe Godsil), (
,
, 1992Jim Hogue), (Ed Tankus) (Gary Wright). AddisonWesley, (John Wait), . . (NOAO) (Sidney Wolff), (Richard Wolff)
(Steve Grandi) .
UNIX troff, . ,
, groff, (James Clark). . troff.
, , .
http://ww.kohala.com/~rstevens
-
1.1. , ,
, (
1.2. , ,
( 1 UNIX
.
, , ,
. UNIX.
UNIX , , , ). UNIX.
, . . , UNIX, , .
UNIX, . (kernel), . . 1.1
UNIX.
, ; ( . 1.1). , ,
-
28 1. UNIX
,
1.3.
/etc/passwd. , ,
sar:x:205:
. 1.1. 1.11). , .
, . , , , .
Linux GNU. GNU/Linux, Linux. , ,
, , . ( , ,.)
UNIX .
; , : ,
(205), (105), , (/home/sar) (/bin/ksh).
105:Stephen Rago:/home/sar:/bin/ksh
. 6 .
UNIX
-
1.3. 29
, , . ( , . (
1.1.
(Ste UNIshell
(Bill Joy) System( Bourne shel
shell.
Bourne shell
Bourneagainshell
C shell
Korn shell
TENEX C sh , , ,
.) , ) ( ).
. 1.1.
, UNIX
, , .
Bourne shell Bell Labs ve Bourne). X, Version 7. Bourne Algol 68.
C shell BSD. , C shell
V/386 Release 3.2 AT&T, System V Release 4 (SVR4). UNIX .) C shell 6 UNIX, l. C, , , Bourne shell: , .
Korn shell Bourne SVR4. Bell Labs (David Korn), UNIX, SVR4
,
FreeBSD 5.2.1 Linux 2.4.22 Mac OS X 10.3 Solaris 9
/bin/sh bash bash
/bin/bash
/bin/csh tcsh tcsh tcsh
/bin/ksh
ell /bin/tcsh
-
30 1. UNIX
. Bourneshell , C shell : , .
Bourneagai POSIX
C sh Beranek and
Linu Mac Oshell,
Bourne shel
1.4.
,
. ( n shell GNU, Linux. Bourne shell, , C shell Korn shell.
TENEX C shell ell. , , TENEX, 1972 Bolt Newman. TENEX C shell C shell .
x Bourneagain shell . /bin/sh /bin/bash. FreeBSDS X TENEX C Bourne shell, C shell .
Solaris BSD System V , . 1.1. .
UNIX. .
, . ,
l, Korn shell Bourneagain shell.
UNIX . , (root),
/.
, , , . ,
), , , ( ), . stat fstat ,
-
1.4. 31
. 4 .
, . UNIX
(\0). ( . ( ,
: . (
14 . UNIX
, (/)
1.1
#include "#include
-
32 1. UNIX
intmain(int argc, char *argv[]){ DIR *dp; struct dirent *dirp;
if (ar er
if ((d er while pr
closed exit(0}
ls UN ls
C
man 1 ls
man sl ls
cc myls.cgc != 2)r_quit(": ls _");
p = opendir(argv[1])) == NULL)r_sys(" %s", argv[1]);((dirp = readdir(dp)) != NULL)intf("%s\n", dirp>d_name);
ir(dp););
(1) IX. , . 1 8, . ,
UNIX.
, , UNIX Programmers Manual ( UNIX). , : , .
UNIX , . , AT&T [1990e] 3S, fopen(3S). , .
. ,
ls :
, . myls.c a.out:
-
1.4. 33
cc(1) C. , GNU C, gcc(1). cc gcc.
:
$ ./a.out ...consolettymemkmemnullmousestdinstdoutstderrzero cdrom$ ./a.out $ ./a.out
apue.h. .
/dev
,
/var/spool/cron
/var/spool/cron: Permission denied/dev/tty
/dev/tty: Not a directory
: , , , . ,
, . ;
. .
, . ls .
20 .
, . B.
main ISO C. ( .)
, argv[1], , . 7, main
-
34 1. UNIX
.
,
DIRdirent ,
: err_err_sys sion deni)). B 1.7.
,. ,
, joe ,
opendir, readdir closedir.
opendir DIR, readdir. ( .) readdir, . readdir , . , dirent, (d_name). stat ( 4.2), .
sys err_quit. ,
, (Permised ( ) Not a directory ( . ,
exit 0. exit .
0 1 255 8.5 , ,
.
,
. , , . chdir.
doc/memo/joe , memo, doc, .
, doc memo , , joe . /usr/lib/lint lint lib, usr, .
, ;
. ( 1.3).
-
1.5. 35
1.5.
, , ,
, .
ls
ls > file.
file.list.
lseek close
1.2
#include "
#define BU
intmain(void){ int n; char b
while , . , , , .
, , : ,
,
.
list
ls
open, read, write,. .
, . .
.
apue.h"
FFSIZE 4096
uf[BUFFSIZE];
((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0)
-
36 1. UNIX
if (write(STDOUT_FILENO, buf, n) != n)
err_sys(" ");
if (n < 0)
err_sys(" ");
exit(0
}
STDIN_FILENO UNIX,
S .
B
re 1.
(a.out)
./a.out >
,
./a.out <
3 .);
, apue.h, STDOUT_FILENO POSIX ( ). , , read write, .
TDIN_FILENO STDOUT_FILENO, , . 0 1,
UFFSIZE 3.9, , .
.
ad . write , . read 0 . , read 1.
:
data
data, . , . , , ( ControlD).
:
infile > outfile
infile outfile.
-
1.5. 37
. BUFF UN ,
,
1.3
#include "
intmain(void){ int c;
while if
if (fe er
exit(0}
ge stdout , SIZE . , ( IX). , fgets , read . 5.4, .
printf. , , ( apue.h), .
1.3, ( 5.8), ,
read write. , , .
. ;
apue.h"
((c = getc(stdin)) != EOF) (putc(c, stdout) == EOF) err_sys(" ");
rror(stdin))r_sys(" ");
);
tc , putc. , getc EOF ( ). stdin
.
-
38 1. UNIX
1.6. , .
,
UNIX
1.4
#include "
intmain(void){ printf exit(0}
$ ./a.out $ ./a.out
getpid.
itpid. ( exec. 8.10.
, . .
.
, .
.
1.4 , .
.
apue.h"
(" %d\n", getpid()););
a.out , :
851
854
: fork, exec wa exec , exec.)
-
1.6. 39
UNIX ( 1.5), .
fgets ,
,
1.5
#include "#include ../var/spool
. getcwd
apue.h"
*ptr;size;
dir("/usr/spool/uucppublic") < 0)r_sys(" chdir");
path_alloc(&size); /* */tcwd(ptr, size) == NULL)r_sys(" ");
("cwd = %s\n", ptr););
: getcwd , . 4.9, ,
/var/spool /usr/spool. .
-
4.23. 175
getcwd , . getcwd . chdir .
fc.
4.23.
dev_t.
328 ,
nux hdir getcwd , . ,
fchdir.
st_dev st_rdev. 18.9 ttyname. .
, , . . . 4.1 , . , , , .
, : major minor., , dev_t.
UNIX 16 , 8 8 . FreeBSD 5.2.1 Mac OS X 10.3 , 24 . 32 Solaris 9 32 , 14 , 18 . 64 Solaris 9 64 , 32 . Linux 2.4.22,
, dev_t 64 , 8 .
POSIX.1 dev_t, . major minor, , , . BSD . Solaris , Li , .
-
176 4.
st_dev , .
st_rdev
,
4.1
#include "#ifdef SOL#include file");
system ISO C, system,
system
pid,
1. ,
2.
3.
8.1
#include
(const char *cmdstring);
: .
-
310 8.
int status;
if (cmdstring == NULL) return(1); /* UNIX */
if ((pid = fork()) < 0) { sta } else ex _e } else wh } }
return}
c ,
fo
,
8.1
#include "#include .
, , .
execlp execl, PATH .
, execlp. , .
, exit _exit. , rk.
system 8.13. ( pr_exit 8.3.)
3. system
apue.h"sys/wait.h>
atus;
-
8.13. system 311
if ((status = system("date")) < 0) err_sys(" system()"); pr_exit(status);
if ((status = system("nosuchcommand")) < 0) err_sys(" system()"); pr_exi
if ((s er pr_exi
exit(0}
$ ./a.outSun Mar 21sh: nosuchsar :0 Marsar pts/0 sar pts/1 sar pts/2 sar pts/3
while ((la ;
, pi while. t(status);
tatus = system("who; exit 44")) < 0)r_sys(" system()");t(status);
);
8.13 :
18:41:32 EST 2004 , = 0 datecommand: command not found , = 127 nosuchcommand 18 19:45Mar 18 19:45 (:0)Mar 18 19:45 (:0)Mar 18 19:45 (:0)Mar 18 19:45 (:0) , = 44 exit
system fork exec , , (
, 10.18) .
UNIX, SVR3.2 4.3BSD, waitpid. :
stpid = wait(&status)) != pid && lastpid != 1)
, system. while ,
, system, , , , d,
wait , POSIX.1 Rationale waitpid. 15.3 , popen pclose, waitpid.
-
312 8.
setuserID , system setuserID? , . 8.14 ,
8.1
#include "
intmain(int a{ int st
if (ar er
if ((s er pr_exi
exit(0}
8.1
#include "
intmain(void){ printf exit(0}
$ tsys pri u$ su Password: # chown ro# chmod u+# ls l tsrwsrwxrx# exit system .
4. system
apue.h"
rgc, char *argv[])
atus;
gc < 2)r_quit(" ");
tatus = system(argv[1])) < 0)r_sys(" system()");t(status);
);
tsys.
8.15 , .
5.
apue.h"
(" uid = %d, uid = %d\n", getuid(), geteuid()););
printuids. :
ntuids id = 205, uid = 205 , = 0 ot tsys s tsys set;user;IDys 1 root 16361 Mar 16 16:59 tsys
-
8.14. 313
$ tsys printuids uid = 205, uid = 0 , , = 0
, tsys,
userID
8.14. fr
. FreeB
fork exec, system.
/bin/sh bash 2, , bash , .
, setuserID setgroupID , fork exec, fork exec. system set setgroupID.
, system , IFS . . IFS system .
UNIX
. , , .
, , , , .
, , ead 5.9.
. , , Solaris 9 , SD 5.2.1 Mac OS X 10.3 , , . , Linux 2.4.22 .
, . , Solaris , runacct(1m) acctcom(1), FreeBSD sa(8).
acct.
-
314 8.
accton(8) (, , ). , accton , . . /var/account/acct FreeBSD MacOS X, /var/a,
typedef u_struct acc{ char a char a uid_t gid_t dev_t time_t comp_t comp_t comp_t comp_t comp_t comp_t char a };
ac_fl
8.8.
ac_flag
AFORK
ASU
ACOMPAT
ACORE
AXSIG
AEXPND ccount/pacct Linux /var/adm/pacct Solaris. accton .
:
short comp_t; /* 3, 8, ; 13 */t
c_flag; /* (. 8.8) */c_stat; /* ( */ /* core) ( Solaris) */ac_uid; /* */ac_gid; /* */ac_tty; /* */ ac_btime; /* */ ac_utime; /* ( ) */ ac_stime; /* ( ) */ ac_etime; /* ( ) */ ac_mem; /* */ ac_io; /* ( read write) */ /* "" BSD */ ac_rw; /* */ /* ( BSD) */c_comm[8]; /* : [8] Solaris, */ /* [10] Mac OS X, [16] FreeBSD [17] Linux */
ag , . . 8.8.
ac_flag acct
FreeBSD5.2.1
Linux2.4.22
Mac OS X10.3
Solaris 9
fork, exec
(core)
-
8.14. 315
, , fork. . , , ( 1.10 ,
. . ,
sleep(2)
exit(2)
for
. 8.4. , .
. , ), . ,
( 60 128 ). ; , , . , , , ,
.
,
fork, , exec , AFORK. A B, B C C
, . C, , .
, , .
/bin/dd
execl
sleep(6)
kill()
sleep(8)
exit(0)
sleep(4)
abort()
kfork
forkfork
,
-
316 8.
8.16. fork . .
8.16.
#include "
intmain(void){ pid_t
if ((p er else i sl ex } if ((p er else i sl ab } if ((p er else i ex ex }
/* thi if ((p er else i sl ex }
sleep( kill(g
exit(6}
8.1
#include "#include
-
8.14. 317
#ifdef HAS_SA_STAT#define FMT "%*.*s e = %6ld, chars = %7ld, stat = %3u: %c %c %c %c\n"#else#define FMT "%*.*s e = %6ld, chars = %7ld, %c %c %c %c\n"#endif#ifndef HA#define AC#endif#ifndef HA#define AX#endif
static unscompt2ulon{ unsign int ex
val = exp =
while va return}
intmain(int a{ struct FILE
if (ar er if ((f er while pr co#ifdef HAS (u#endif ac ac ac ac } if (fe er exit(0}S_ACOREORE 0
S_AXSIGSIG 0
igned longg(comp_t comptime) /* comp_t unsigned long */
ed long val;p;
comptime & 0x1fff; /* 13 */(comptime >> 13) & 7; /* 3 (07) */
(exp > 0)l *= 8;(val);
rgc, char *argv[])
acct acdata; *fp;
gc != 2)r_quit(": pracct _");p = fopen(argv[1], "r")) == NULL)r_sys(" %s", argv[1]);(fread(&acdata, sizeof(acdata), 1, fp) == 1) {intf(FMT, (int)sizeof(acdata.ac_comm), (int)sizeof(acdata.ac_comm), acdata.ac_comm,mpt2ulong(acdata.ac_etime), compt2ulong(acdata.ac_io),_SA_STATnsigned char) acdata.ac_stat,
data.ac_flag & ACORE ? D : ,data.ac_flag & AXSIG ? X : ,data.ac_flag & AFORK ? F : ,data.ac_flag & ASU ? S : );
rror(fp))r_sys(" read"););
-
318 8.
, BSD, ac_flag acct , HAS_SA_STAT , . , ,
#if define
,
1. ,
2.
execl.
3.
4.
).
accton esh edd e
a.out ea.out e .
d(BSD) || defined(MACOS)
.
, , ACORE AXSIG. Linux enum, #ifdef.
:
, accton. ,
accton , ; .
8.16.
: , , , .
.
. accton , , .
8.17, , .
, 4. (
= 6, chars = 0, stat = 0: S = 2106, chars = 15632, stat = 0: S = 8, chars = 273344, stat = 0: = 202, chars = 921, stat = 0: = 407, chars = 0, stat = 134: F
-
8.14. 319
a.out e = 600, chars = 0, stat = 9: F a.out e = 801, chars = 0, stat = 0: F
. (. 2.12) 100 ,
8 core ( 128+6, 1 ( ex
, 136 66
exec , c
. , sleep(2) 202 . sleep(4) 407 . , sleep, . ( 10.) fork exit .
, ac_stat , ,
.6. , ( ) ), . ,
. 28 core, 6 SIGABRT, abort. 9 SIGKILL. , exit) 2, it 0.
/etc/termcap, dd 136 663 . , 136 663 3 . , , .
ac_flag . F , , execl. F , , , fork, a.out. abort,
SIGABRT, core. , D X, Solaris; , ac_stat. , SIGKILL ore, .
: , core. , , core, .
-
320 8.
8.15. . , . getpwu
Syst pwuid( 1990
LOGNAM,
8.16. 1
#include
ogin(void);
, NULL
-
8.16. 321
. times, .
buf:
struct tms clock_ clock_ clock_ clock_};
times times. (
( 2.5.
,
,
#include
clock_t ti tms,
{t tms_utime; /* */t tms_stime; /* */t tms_cutime; /* */ /* */t tms_cstime; /* */
: . . , , . , times . . , , , , . 1.6.)
, , wait, waitid waitpid.
clock_t, , _SC_CLK_TCK, sysconf4).
getrusage(2). 14 ,
. BSD, , BSD , ,
.
8.18, , , tms.
mes(struct tms *buf);
, 1
-
322 8.
8.18.
#include "apue.h"#include
static void pr_times(clock_t, struct tms *, struct tms *);static voi
intmain(int a{ int i;
setbuf for (i do exit(0}
static voido_cmd(cha{ struct clock_ int st
printf if ((s er if ((s er if ((e er pr_tim pr_exi}
static voipr_times(c{ static
if (cl if printf printf printf printf printf }d do_cmd(char *);
rgc, char *argv[])
(stdout, NULL); = 1; i < argc; i++)_cmd(argv[i]); /* */);
dr *cmd) /* "cmd" */
tms tmsstart, tmsend;t start, end;atus;
("\n: %s\n", cmd);tart = times(&tmsstart)) == 1) /* */r_sys(" times");tatus = system(cmd)) < 0) /* */r_sys(" system()");nd = times(&tmsend)) == 1) /* */r_sys(" times");es(endstart, &tmsstart, &tmsend);t(status);
dlock_t real, struct tms *tmsstart, struct tms *tmsend)
long clktck = 0;
ktck == 0) /* . */ ((clktck = sysconf(_SC_CLK_TCK)) < 0) err_sys(" sysconf");(" real: %7.2f\n", real / (double) clktck);(" user: %7.2f\n", (tmsend>tms_utime tmsstart>tms_utime) / (double) clktck);(" sys: %7.2f\n", (tmsend>tms_stime tmsstart>tms_stime) / (double) clktck);(" child user: %7.2f\n", (tmsend>tms_cutime tmsstart>tms_cutime) / (double) clktck);(" child sys: %7.2f\n", (tmsend>tms_cstime tmsstart>tms_cstime) / (double) clktck);
-
8.17. 323
, :
$ ./a.out "sleep 5" "date"
: sleep 5 real: 5.02 user: 0. sys: 0.0 child us child sy
: dMon Mar 22 real: 0. user: 0. sys: 0.0 child us child sy
8.17. exec, _e
,
8.1.
000er: 0.01s: 0.00 , = 0
ate 00:43:58 EST 200401000er: 0.01s: 0.00 , = 0
, , .
UNIX . : fork, xit, wait waitpid. . , fork .
system . exec , . ,
setuserID.
. 10
.
8.2 , _exit exit,
-
324 8.
printf 1. , , . , ?
8.2. , . 7.3, mai
8.3.
$ ./a
$ ./a
8.4.
8.5.
8.6.
8.7. close. vfork
, , vfork n, , vfork?
.
8.7 :
.out
. :
.out ; ./a.out ; ./a.out
. ? ? , ?
8.10 execl, .
execlp, testinterp, /home/sar/bin PATH, argv[2]?
?
, system ps(1), , .
8.10, POSIX.1 , exec. : opendir, DIR closeonexec.
open onexec.
-
9.1. ).
.
,
9.2. Version 7, ( , 9
, . , (
, .
, waitpid ( 8.6), .
, , POSIX.1. , , ,
, , . UNIX, , ,
10.
, UNIX. UNIX, , . ( ),
). . PDP11
-
326 9.
DH11 DZ11. , , .
,
, UNIX ,
ty /etc/ttys for
init
init
getty
fork
exec
. 9.1. , , . , , ( ).
, . , ,
( ).
, . ,
, .
BSD 30 .
, /etc/ttys, . , get, (baud rate). 1, init,
. init getty k exec. , . 9.1.
getty
1
, init,
-
9.2. 327
, . 9.1, 0 ( ). , init getty .
open , getty , . (gettytab),
execle("/b
( getty getty envpfoo logi
init
init
getty
login
fork
exec
exec
. 9.2. getty open . , , . 0, 1 2. , login:, .
getty , getty , .
getty login, :
in/login", "login", "p", username, (char *)0, envp);
tab , login.) init , getty login (
) ( TERM=foo, , gettytab) , gettytab. p
n, , . . 9.2 login.
/etc/ttys, ,
( 0, 1, 2), ,
1
login
-
328 9.
, . 9.2, . , exec . , , init, 1.
). , g
les ,
execl("login . , getpwnam, . login getpass(3), Password: ( , , crypt(3), pw_passwd . ( ), login exit
1. login (init), fork exec etty .
, UNIX. UNIX . , FreeBSD, Linux, MacOS X Solaris , PAM (Pluggable Authentication Modu ).
PAM.
, , , PAM. PAM ,
, .
, login:
(chdir)
(chown)
,
setgid initgroups
, login: (HOME), (SHELL), (USER LOGNAME) (PATH)
(setuid)
/bin/sh", "sh", (char *)0);
-
9.2. 329
argv[0] , . , , , .
login , , .
8. .
, SIGCHLD)
Bourne neagain she , (PATH)
init
fd 0, 1
. 9.3. . , , , .
11, setuid, , , , , setgid, login, .
. init ( 1), , , init (
. 0, 1 2 . . 9.3.
(.profileshell Korn shell; .bash_profile, .bash_login .profile Bourll .cshrc .login C shell).
.
(TERM). , .
C
getty login
1
, 2
-
330 9.
Mac OS X Mac OS X , BSD, Mac OS X FreeBSD, .
BSD Lin
/etc/init mgetty).
Solaris getty,
tt SAF SA, . 9.3, sa init Linux Linux . , login Linux login 4.3BSD.
ux BSD , .
, , getty, Linux tab, , System V. getty, ( getty), /etc/gettydefs (
Solaris : () BSD, () ttymon , SVR4. , getty, ttymon.
ymon , Service Access Facility ( ).
F , . ( 6 [Rago 1993].) , init . init
c (service access controller ), fork exec ttymon, . ttymon , , . exec login, . , login , , . 9.3. ttymon, getty .
-
9.3. 331
9.3. , , ,
superserver.
, ,
. TELN
telnet hos
. login , , FTP SMTP.
, , init , , getty . (, Ethernet) , . , , .
, , , . ,
. ( 19 .)
BSDBSD inetd, Internet
, BSD .
[Stevens, Fenner, and Rudoff 2004].
init /etc/rc. , inetd. inetd init. inetd TCP/IP. , inetd fork exec .
, TELNET TCPET ,
TCP. , ( ) , , TELNET:
tname
TCP hostname, , TELNET. TELNET.
-
332 9.
, , . (, , .) . 9.4 , TELNET telnetd.
fo login. 0, 1 2 login ex
,
POSIX.1,
TELN
. 9.4. telnetd rk . , .
exec .
, 9.2: ,
. login ec. . 9.5 .
. 19, .
, , (. 9.3) (. 9.5),
, , , . ,
.
init
inetd
inetd
telnetd
fork
exec
ET
/bin/sh, /etc/rc
TELNET
1
, TELNET
-
9.4. 333
Mac OS X FreeBSD
BSD,
init.
9.4.
init
fd 0, 1
1
. 9.5. Mac OS X , BSD, Mac OS X
.
Linux Linux , , inetd xinetd (extended Internet services daemon ). xinetd
inetd.
Solaris Solaris BSD Linux. Solaris, BSD, inetd. Solaris SAF,
. inetd , . 9.5.
, . 10.
, 2
inetd, telnetd login
telnetd telnet
-
334 9.
, ( 9.8), . .
Specificatio
getpgid(0)
getpgrp();
,
se
#include
gid(pid_t pid);
, 1
-
9.5. 335
,.
, . ,
9.5.
. 9.6,
proc1 | pr
proc3 | pr
#include
int setpgid(pid_t pid, pid_t pgid);
0 , 1 pid pgid. , pid, pid 0,
. pgid 0, pid.
. , exec.
, setpgid fork, , , , , ,
. , .
, ( ) ( ). waitpid .
. , . 9.6.
.
. , ,
:
oc2 &
oc4 | proc5
setsid.
-
336 9.
1. .
2. .
3.
fo
Si,
#include
pid_t setsid(void);
. 9.6. , . .
. ( , .)
.
. ( .) setsid, .
, . , rk, , . , , , . , , .
ngle UNIX Specification . , , , ,
, 1
proc1 proc2 proc3 proc4
proc5
-
9.6. 337
. SVR4. BSD , . getsid . Single UNIX Specification XSI.
,
9.6.
),
,
DELETE
Control\
#include
id(pid_t pid);
, 1
-
338 9.
, ( ) , .
. 9.7.
(
,
. 9.7. .
POSIX.1 . 19.4.
, System V, , , . , , open, O_NOCTTY ( 3.3).
, BSD, , ioctl, request TIOCSCTTY ). , . ( ioctl setsid , .) O_NOCTTY open BSD, , .
, , . open /dev/tty.
proc1 proc2 proc3 proc4
=
,
( )
proc5
,
-
9.7. tcgetpgrp, tcsetpgrp tcgetsid 339
., , .
getpass(3), , ).
crypt < sa
. . (
crypt. [Garf
9.7.
tc,
tcpgrpid filedes
Si tc
#include
tpgrp(int filedes);
, 1
grp(int filedes, pid_t pgrpid);
0 , 1
-
340 9.
.
, (
9.8. ,
1.
2.
3.
SVR3 .
vi main.c
pr *.c | lmake all &
,
#include
pid_t tcge, tcgetsid, , ).
BSD 1980 . ( ) , ,
. :
.
.
, .
, (shell layers). POSIX.1 , BSD; , POSIX.1 , POSIX
.
, . , . ,
, .
pr &
. , .
, , , .
tsid(int filedes);
, 1
-
9.8. 341
, . , C shell , Bourne shell , Korn shell , . C shell , Bourne shell ,
.
$ make all[1] 14$ pr *.c |[2] 14$ [2] + Don[1] + Don
, ,
( SIGTS
SI
(, System V), SVR4 , jsh sh. Korn shell , . Bourneagain shell
. , , , .
, Korn shell:
> Make.out &
75 lpr &
90 e pr *.c | lpr &e make all > Make.out &
1 make, 1475. 2 1490. , ,
. , ,
. . , , .
, , ,
ControlZ). TP . , , .
.
( DELETE ControlC) GINT.
( Control\) SIGQUIT.
-
342 9.
( ControlZ) SIGTSTP.
18 , .
,
$ cat > te [1] 1681$ [1] + Stop$ fg %1 cat > temp hello, wor
D
$ cat temphello, wor
SIGTTIN. ( fg (tcset
?
$ cat temp , . , , , . . , SIGTTIN . , , , , :
mp.foo & ,
ped (SIGTTIN) cat > temp.foo & 1 .foo , ld EOF ( ).foo , ld
cat, ( ). , ,
( wait waitpid 8.6) , . fg . , bg, .) pgrp) (SIGCONT). , .
, , stty(1). ( 18 , .) :
.foo &
-
9.9. 343
[1] 1719
$ hello, world [1] + Done cat temp.foo &$ stty tostop $ cat temp[1] 1721
$ [1] + Stop
$ fg %1 cat temp.f
hello, wor
, , ,
. 9.8 SIGTTOU,
,
9.9. .
laris. .foo &
, ped(SIGTTOU) cat temp.foo &
oo , ld
cat
, , SIGTTOU. fg ,
.
, . , , , . , , .
? ,
. , .
, ,, . , , . , , POSIX.1.
,
, ps.
, Bourne shell So
-
344 9.
ps o pid,
PID PPID
949 947
1774 949
init inetd
getty telnetd
()
. 9.8. ppid,pgid,sid,comm
:
PGID SID COMMAND
949 949 sh
949 949 ps
login
exec
(SIGINT,SIGQUIT,SITSTP
),
$
SIGTTOU
tcsetpgrp
,
,
setpgidsetpgid
exec setsid,
SIGTTIN
-
9.9. 345
, ps . , ps (949). , 949 , ,
, TPGIUNIX. Mac
ps
Linu
ps
pgrp, TPGID,
ps o pid,
PID PPID 949 9471812 949
ps o pid,
PID PPID 949 947 , .
ps . D. , ps
, Solaris 9 . FreeBSD 5.2.1 OS X 10.3
o pid,ppid,pgid,sess,tpgid,command
x 2.4.22
o pid,ppid,pgrp,session,tpgid,comm
, .
: ( TPGID terminal process group ID). , . , . , . , . tcset . 9.8. , . , ps . , , ps 1.
:
ppid,pgid,sid,comm &
, :
PGID SID COMMAND 949 949 sh 949 949 ps
, .
, Bourne shell .
ppid,pgid,sid,comm | cat1
:
PGID SID COMMAND 949 949 sh
-
346 9.
1823 949 949 949 cat1
1824 1823 949 949 ps
( cat1 cat, . cat cat2,
ps o pid,
.
cat > temp
/dev/null,
,
crypt < sa
( . cat .) :
, . ,
, .
:
ppid,pgid,sid,comm | cat1 &
. , 949,
, ? , , :
.foo &
, , , , SIGTTIN.
. /dev/null ., cat .
, , , /dev/tty ? : , , ,
. ,
laries | lpr &
. , crypt /dev/tty,
), . , Password:, ,
-
9.9. 347
. , , , . , ,
ps o pid,
PID PPI 949 941988 941989 1981990 198
PI 94183183183
, ec,
, . . ,
, .
ps o pid,
PID PPID2837 28185796 2837 . , , .
Bourne shell, , :
ppid,pgid,sid,comm | cat1 | cat2
:
D PGID SID COMMAND7 949 949 sh9 949 949 cat28 949 949 ps8 949 949 cat1
, . :
D PPID PGID SID COMMAND9 947 949 949 sh1 949 949 949 sh2 1831 949 949 ps3 1831 949 949 sh
, ps cat fork exec. , , ex ps .
9.9 .
(cat2) .
, Linux , .
Bourneagain shell; .
ppid,pgrp,session,tpgid,comm
:
PGRP SESS TPGID COMMAND 2837 2837 5796 bash 5796 2837 5796 ps
-
348 9.
( Bournea (579
ps o pid,
PID PPID
2837 2818
5797 2837
.
ps o pid,
sh
(949)fork sh
sh
(1989)exec ps
(1989)
fork
. 9.9. .) Bourne shell. gain shell (ps) 6). ps .
, , . ps . , , 2837 5796, . , .
:
ppid,pgrp,session,tpgid,comm &
PGRP SESS TPGID COMMAND
2837 2837 2837 bash
5797 2837 2837 ps
ps , (5797) . TPGID (2837) , .
:
ppid,pgrp,session,tpgid,comm | cat1
(1988)
sh
(1990)exec cat1
(1990)
fork
cat2
(1988)
exec
ps | cat1 | cat2, Bourne shell
-
9.10. 349
PID PPID PGRP SESS TPGID COMMAND
2837 2818 2837 2837 5799 bash
5799 2837 5799 2837 5799 ps
5800 2837 5799 2837 5799 cat1
(5799), , :
ps o pid,
PID PPID
2837 2818
5801 2837
5802 2837
9.10. ,
. ,
, , ps cat1, .
Bourne shell. Bourne shell
. Bourneagain shell.
ppid,pgrp,session,tpgid,comm | cat1 &
, ps cat1 :
PGRP SESS TPGID COMMAND
2837 2837 2837 bash
5801 2837 2837 ps
5801 2837 2837 cat1
, , , .
, , , init. ,, POSIX.1 .
, ( ), , ( ), ? ,
? . 9.10: , , .
, 9.1. . , , . , (
-
350 9.
(
SIGTSTP. ,
1.
, ,
(PID 2837)
2837
C
. 9.10. 6099), (2837). . ,
fork
5 . () , .
SIGHUP. , SIGHUP . 10.)
kill (ControlZ).
, init
;. POSIX.1 , , ,
. , , , , .
, , , .
(, 6100 1).
(PID 6099)
(PID 6100)
fork/exec
fork
6099
-
9.10. 351
, , ( ) SIGHUP SIGCONT, POSIX.1.
printf pr_ids.
$ ./a.out: : p$ : p
, . 1.
9.1
#include "#include 0
pid == 0
pid < 0
pid == 1 , , . , . , ,
, _POSIX_SAVED_IDS ( ), . : SIGCONT , .
SIX.1 0 . gno 0, kill , . , , . , kill ESRCH errno.
,
, , .
, kill , .
pid.
, , . , . UNIX init ( 1).
, pid, . .
, . .
-
10.10. alarm pause 385
kill , signo, , kill . ( , 12.8.)
10.10. al
se
(
alarm
conds
, alarm
pa
#include
void);
1 EINTR errno
-
386 10.
pause , . 1 EINTR errno.
10.4
10
#include
dnt signo)
, */
ntigned int nsecs)
gnal(SIGALRM, sig_alrm) == SIG_ERR)turn(nsecs);nsecs); /* , */ /* */(alarm(0)); /* , */
/* */
sleep, .
, alarm. . , nsecs, . , nsecs, , nsecs ,
.
SIGALRM. ,
. , signal .
alarm pause .
-
10.10. alarm pause 387
, pause. , pause (, , ).
1 setjmp, 10.19.
SVR2 ,
10
#include
_buf env_alrm;
dnt signo)
p(env_alrm, 1);
ntigned int nsecs)
gnal(SIGALRM, sig_alrm) == SIG_ERR)turn(nsecs);tjmp(env_alrm) == 0) {arm(nsecs); /* */use(); /* */ /* */
(alarm(0)); /* , */ /* */
sleep2 pause , sleep2 SIGALRM.
, . SIGALRM
-
388 10.
, longjmp . 10.6 . SIGINT , , , 5 . , tile,
$ ./a.out? si sl
(sig_in 1
10
#include "
unsigned istatic voi
intmain(void){ unsign
if (si er unslep printf exit(0}
static voisig_int(in{ int i, volati
/* * * */ printf for (i fo , sleep2. k vola
, . 10.6 :
g_int eep2 : 0
, longjmp sleep2 t), . , sleep (0.3).
.6. sleep2 ,
apue.h"
nt sleep2(unsigned int);d sig_int(int);
ed int unslept;
gnal(SIGINT, sig_int) == SIG_ERR)r_sys(" signal(SIGINT)");t = sleep2(5);(" sleep2 : %u\n", unslept););
dt signo)
j;le int k;
, 5 , .
("\n sig_int \n"); = 0; i < 300000; i++)r (j = 0; j < 4000; j++)
-
10.10. alarm pause 389
k += i * j; printf(" sig_int \n");}
sleep1 sleep2 ,
rea
10
#include "
static voiint
main(void){ int n; char l
if (si er alarm( if ((n er alarm( write( exit(0}
static voisig_alrm(i{ /* }
UNIX,
1. . , , .
alarm pause , , . , ( 10.5), , d . 10.7 ,
.
.7. read ;
apue.h"
d sig_alrm(int);
ine[MAXLINE];
gnal(SIGALRM, sig_alrm) == SIG_ERR)r_sys(" signal(SIGALRM)");10); = read(STDIN_FILENO, line, MAXLINE)) < 0)r_sys(" read");0);STDOUT_FILENO, line, n););
dnt signo)
, read */
.
10.7 , 10.4:
-
390 10.
alarm read. , , read. ,
2. ALRM.
longjmp
10
#include "#include errno.h>
lockfile(int); already_running(void);
d)
*/
t signo)
(LOG_INFO, " SIGTERM; "););
signo)
(LOG_INFO, " ");();
rgc, char *argv[])
cmd; sigaction sa;
md = strrchr(argv[0], /)) == NULL)d = argv[0];
-
13.6. 521
cmd++;
/* * . */ daemon
/* * */ if (al sy ex }
/* * */ sa.sa_ sigemp sigadd sa.sa_ if (si sy ex }
sa.sa_ sigemp sigadd sa.sa_ if (si sy ex }
/* * */ /* ... exit(0}
HUP SIGTERM ize(cmd);
, .
ready_running()) {slog(LOG_ERR, " ");it(1);
.
handler = sigterm;tyset(&sa.sa_mask);set(&sa.sa_mask, SIGHUP);flags = 0;gaction(SIGTERM, &sa, NULL) < 0) {slog(LOG_ERR, " SIGTERM: %s", strerror(errno));it(1);
handler = sighup;tyset(&sa.sa_mask);set(&sa.sa_mask, SIGTERM);flags = 0;gaction(SIGHUP, &sa, NULL) < 0) {slog(LOG_ERR, " SIGHUP: %s", strerror(errno));it(1);
.
*/);
SIG. SIGHUP: , , .
-
522 13.
13.7. . . 13.1 syslogd, () UNI
, syslo
13.8.
,
13.1.
(openlo
13.2.
13.3. X.
, . , . 13.1gd .
. 13.1 . ,
. , , .
. , ,
, 9. ,
.
, , . , UNIX, .
. 13.1, , syslog ( openlog, syslog) /dev/log. , ) chroot g?
.
, daemonize 13.1. getlogin ( 8.15), , .
.
-
14.1. writev
14.2. 1:
,,
, (
, , 14
,
;: , , STREAMS, ( select poll), readv , (mmap). , 15 17
.
0.5 , . , . :
, (, ) , .
, . .).
(, , FIFO , , ).
-
524 14.
, .
ioctl.
,
open, re ,
1.
2. .
UNIX PO POSIX.1 err
4.3BS ( 15).
, , , , .
,ad write, , . , , .
, .
open, O_NONBLOCK ( 3.3).
O_NONBLOCK , fcntl ( 3.14). 3.5 ,
System V O_NDELAY. read 0. , read 0 , SIX.1 . System V, read 0, , , , . , read 1 EAGAIN no. UNIX, System V, O_NDELAY, POSIX.1 O_NONBLOCK, , POSIX.1. O_NDELAY
.
D fcntl FNDELAY .
, , , ( 4.3BSD ). , 4.3BSD EWOULDBLOCK, . BSD
-
14.2. - 525
O_NONBLOCK EWOULDBLOCK , EAGAIN. , POSIX.1: , ,
14
#include "#include > Korn shell Bourne
, Korn shell O_CREAT O_APPEND, , REAT .
Bourne shell open O_CREAT, , open , write .)
. , . ,
ed , .
, ., , .) , ,
.
14.6, , .
.6. ,
apue.h"
errno.h>
fcntl.h>
sys/wait.h>
rgc, char *argv[])
;
-
542 14.
pid_t pid; char buf[5]; struct stat statbuf;
if (argc != 2) { fp ex } if ((f er if (wr er
/* if (fs er if (fc er
TELL_W
if ((p er } else /* if
TE
if } else WA
se
/* * * * if pr
/* if if el }rintf(stderr, ": %s filename\n", argv[0]);it(1);
d = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, FILE_MODE)) < 0)r_sys(" open");ite(fd, "abcdef", 6) != 6)r_sys(" write");
setgroupID groupexecute */tat(fd, &statbuf) < 0)r_sys(" fstat");hmod(fd, (statbuf.st_mode & S_IXGRP) | S_ISGID) < 0)r_sys(" fchmod");
AIT();
id = fork()) < 0) {r_sys(" fork"); if (pid > 0) { /* */ */ (write_lock(fd, 0, SEEK_SET, 0) < 0) err_sys(" write_lock");
LL_CHILD(pid);
(waitpid(pid, NULL, 0) < 0) err_sys(" waitpid"); { /* */IT_PARENT(); /* , */
t_fl(fd, O_NONBLOCK);
, , ./ (read_lock(fd, 0, SEEK_SET, 0) != 1) /* */ err_sys(": read_lock ");intf(" read_lock %d\n", errno);
*/ (lseek(fd, 0, SEEK_SET) == 1) err_sys(" lseek"); (read(fd, buf, 2) < 0) err_ret(" (. )");se printf(" (. ), buf = %2.2s\n", buf);
-
14.3. 543
exit(0);}
. . EAGAIN. EACCES E). .
$ ./a.out read
FreeBS
$ ./a.out read
35
?
vi, ,
( , . , . , EACCES
. , read AGAIN ( read ,
Solaris 9 ( ) :
temp.lock
_lock 11 (. ): Resource temporarily unavailable
intro(2), , 11 EAGAIN.D 5.2.1 :
temp.lock
_lock 35 (. ), buf = ab
EAGAIN. .
: , UNIX , : , .
vi .
.
, , ). , vi. , fork, .
-
544 14.
, , vi. , , .
. , ,
20 ,
14.4. STR S STREA 14.5.2) STR
STREAM Sola
(strea STREA wait .
, , . , , ., . .
, . , .
EAMSTREAMS System V
. MS, System V,
poll ( , EAMS ( 17.2 17.2.1).
STREAMS stream (), ( 5.2). streams [Ritchie 1984]
. , , streams SVR3 , . STREAMS ( STREAMS) SVR4. SVR4 [AT&T 1990d]. [Rago 1993] STREAMS , .
STREAMS Single UNIX Specification ( XSIS Option Group). , , ris STREAMS. Linux STREAMS, , .
m) STREAMS . MS
-
14.4. STREAMS 545
STR
. .
,
(
(
. 14.4.
. 14.5. EAMS . . 14.4 , .
. 14.5 , , , , , .
. . (
)
)
-
546 14.
, .) . 14.5 . , , . , , .
STR , Linux
putmsg poll
, STREAMS
[Bach
putpmsg. EAMS , . (, Solaris), STREAMS , , .
STREAMS , 3: open, close, read, write ioctl. , STREAMS SVR3 (getmsg,), SVR4 (getpmsg putpmsg), . .
open pathname, /dev. ls l, , STREAMS. STREAMS .
STREAMS , , ,
. , STREAMS .
STREAMS (clist ). ( SVR2 BSD4.4 1986], 10.3.1, [McKusuck et al. 1996], 10.6, .) , . ,
, read write . STREAMS ,
STREAMS .
STREAMS STREAMS .
read, write, ioctl, getmsg, getpmsg, putmsg , .
-
14.4. STREAMS 547
, , , . . 14.4 , write, putmsg putpmsg. strbuf
struct str int ma int le char *
};
? and Israel 1 Sy
() , write (putm
. ( getpmsg, putm:
bufxlen; /* */n; /* */buf; /* */
putmsg putpmsg len . getmsg getpmsg maxlen ( ), len
. , 1 len .
. 5 [Olander, McGrath,986] stem V. 5 [AT&T 1990d] . ,
System V 4 [Rago 1993] (TLI Transport Layer Interface), .
, , ().
, ( ).
, . , , ioctl . , N , write.
sg getmsg) .
25 , .
, STREAMS, .) read, write, getmsg,sg putpmsg :
-
548 14.
M_DATA ( ) M_PROTO ( ) M_PCPROTO ( )
:
0. 255, ,
,
)
#include
(int filedes, const struct strbuf *ctlptr, const struct strbuf *dataptr, int flag);
g(int filedes, const struct strbuf *ctlptr, const struct strbuf *dataptr, int band, int flag);
0 , 1
-
14.4. STREAMS 549
write, putmsg, 0 flag.
:
14.
. 1 c? ctl
write
putmsg
putmsg
putmsg
putmsg
putmsg
putpmsg
putpmsg
putpmsg
putpmsg
putpmsg
putpmsg
putpmsg
putpmsg
putpmsg , . . 14.4 .
4. STREAMS, write, putmsg putpmsg
, ? ctlptr, tlptr>len. , ctlptr ptr>len , . ? ( ctlptr
dataptr).
?
? band flag
M_DATA ()
0 C , 0
0 M_DATA ()
0 M_PROTO ()