Top Banner
526

Roba4evski OS UNIX

Apr 03, 2015

Download

Documents

Philip Petev
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

www.books-shop.com , , . : UNIX ( /, , ), UNIX( ), ( ) UNI X ( TCP/IP, , TLI). : . . .:. " . . . " " . . . . UNIX. .: 2002. 528 .ISBN 5820600304UNIX The Open Group . , 1997 . , 1997 , "", 02429 24.07.00. . . . . 42,8.. 5000 . 1383"", 198005, , ., 29. , , . . "" .199034, , 9 , 12.www.books-shop.com " UNIX" 1 1 ? 2 2 3 .....3 UNIX 4 UNIX 6System UNIX 6System V Release 4 (SVR4) 7UNIX Berkeley Software Distribution 7 8 UNIX, 8 UNIX 9 9 10/ 10SVID 11ANSI 11 UNIXAIX 13HPUX 13IRIX 13Digital UNIX 13 UNIX 13Solaris UNIX 14 UNIX 15 17 1. UNIX 19 20 21 25 UNIX 26 26www.books-shop.com/bin 27 27/etc 27/lib 27 27 28 /home 28 28/var 28 28 28 30 35 38 38 39 39 40 40 41 Process ID 41 Parent Process ID 41 (Nice Number) 41 (TTY) 41 (RID) (EUID) .41 (RGID) 42 42 44 47 47 47 UNIX 49 50 51 54 55 UNIX 56 shell 56 Bourne shell 59 59 60 64 66, 68, 71www.books-shop.com 73 74 test 75 77 78 79 80 UNIX 82 82 86 UNIX 88 1. 89 2. 89 3. 90 4. 90 5. 91 6. "" 91 7. 91 8. 92 92 2. UNIX 93 UNIX 93 93 95 105 ELF COFF UNIX (2) 122 creat(2) close(2) dup2(2) lseek(2) read(2) readv(2) write(2) writev(2)www.books-shop.com pipe(2) fcntl(2) 129 130 133, 137 140 142 146 150 154 173 184 186 3. 187 191 195 197 199 202 204 207 210 216 219 221 222 226 230 233 234www.books-shop.com 235 236 236 237 237 238 240 242FIFO 243 IPC 245 248 253 258 BSD 264 265 274 277 278 4. 279 SYSTEM V 280 281 282 285 287 BSD UNIX 288 291 292 293 296 303 304 306 307 309 313 314 SVR4 317 317 321 5. 322 323www.books-shop.com 323 325 333 335 338 340 342 343 344 346 348 STREAMS 350 STREAMS 352 356 357 361 362 364 368 369 371 372 375 377 380 6. UNIX 382 TCP/IP 383 TCP/IP 384 TCP/IP 386 OSI 391 IP 393 398 400User Datagram Protocol (UDP) 402Transmisson Control Protocol (TCP) 404 TCP 406 TCP 413 " " 414 416 417 419 420 420www.books-shop.com TLI 426 . 440 442 (binding) 443 (exception) 444 444 445 445 ? 446 447 448 448client.c 449 BSD UNIX 452 453 458 TCP/IP 464 IP 466 UDP 468 TCP 469 UNIX SYSTEM V 470 472 481 DLPI 487 490 LLC 492 IP 493 493 DLPI 497 501 . 503 . UNIX 504 504 INTERNET 505 509 "Books-shop"., , . [email protected] , , . , " Internet. " , . . . , . , . UNIX . . OLLY, , . . " " . . . . . . " " . . . . . . . " , , . . , , , , , . , , , ., , . , , .www.books-shop.com " UNIX. , , , , . , , . . , , . "" , . , UNIX, . UNIX. , UNIX , . , "" UNIX, a: , , , . , , UNIX. : Solaris 2.5 Sun Microsystems, SCO ODT5.0 Santa Cruz Operation, BSDi/386 Berkeley Software Design. UNIX ( ), UNIX . . UNIX . 1 UNIX. . shell. 2 . . . UNIX: , , /. UNIX, . 6 . " ", "www.books-shop.com " " ( ) " , " ( ) , , . UNIX. , ., , . ? , . , , , .. , . , . , . . , , . , . , , ., , , . , , , . ? , , shell , open(2), cat(l) ( ). , , . , d_open sleep :int{exit ()} , :$Enter old password: , /etc/passwd . , + ( ).www.books-shop.com 30 UNIX. PDP7 4 , UNIX , PC . UNIX , , , . UNIX , , . , . "" , UNIX. UNIX, . 1965 Bell Telephone Laboratories ( AT&T) General Electric Company (MIT) , MULTICS(MULTiplexed Information and Computing Service). , . Bell Labs (Ken Tompson) (Dennis Ritchie). MULTICS ( 1969 Bell Labs ), , UNIX. , . www.books-shop.com4 , MULTICS, 1969 , , . PDP7. UNIX, MULTICS , (Brian Kernighan). UNIX , . . 1971 Bell Labs , UNIX. UNIX PDP11, : 16 , 8 , 64 . FORTRAN, . , , , . 1973 , , UNIX. , UNIX , , . Bell Labs 25, UNIX UNIX System Group (USG). UNIX AT&T UNIX , 1974 . , , (Edition). 10 , 1971, 1989 . Bell Labs UNIX 1 1970 . UNIX .www.books-shop.com (Computer Research Group, CRG) PDP11, VAX. , UNIX System Group, . (Programmer's PWB) , SCCS, . USG. : 1971 1973 1973 1975 UNIX, PDP11. , cat(1), mail(1), mount(1M),rmdir(1), wc(1), . (1), . , . , Bell Labs. . , BellLabs, UNIX. , , BSD (Berkeley Software Distribution) UNIX. Bourne Shell . . Microsoft, XENIX. UNIX , 1977 500. 1977 Interactive Systems Corporation VAR (Value Added Reseller) UNIX, . UNIX , PDP. 1979www.books-shop.com UNIX UNIX, , . "" UNIX, , . , UNIX. . UNIX.System V UNIX 1975 AT&T , . , , , UNIX. UNIX , , . , AT&T 1982 UNIX System III. , , , , System III Bell Labs AT&T UNIX, . 1983 Bell Labs System V. 1984 USG (UNIX SystemDevelopment Laboratory, System V Release 2 (SVR2). , (copy on write), (InterProcess Communication, IPC) , . 1987 System V Release 3 (SVR3). AT&T AT&T (AT&T Information Systems, , : /, STREAMS. (File System Switch), .www.books-shop.com UNIX . Transport LayerInterface ().System V Release 4 (SVR4) 1989 System V Release 4. UNIX: SunOS Sun Microsystems, BSD UNIX BerkeleySoftware Distribution System V. : (BSD) /, STREAMS(System V) (SunOS) NFS (SunOS) FFS (BSD) (BSD) ANSI,POSIX, SVID.UNIX Berkeley Software Distribution UNIX 1974 . UNIX, BSD UNIX. 1978 . 1979 UNIX 3BSD. BSD, VAX. , , (virtualmemory) (demand paging). 1980 , Bolt, Beranekand Newman (BBN) (DARPA) TCP/IP BSD UNIX. 1981 , 4.2BSD UNIX.www.books-shop.com8 4.2BSD 1983 , , Ethernet. , . ARPANET, 80. , . , AT&T, , , , . . , , . 1986 4.3BSD, . 1986 1990 , NFS, VFS, . , , 4.4BSD BSD Lite, 1993 .OSF/1 1988 AT&T Sun Microsystems System V. , , IBM, DEC, HewlettPackard, Open Software Foundation (OSF), AT&T . OSF/1. , , OSF/1 UNIX. , , , UNIX System V. UNIX, , , , , . , . , , , , .www.books-shop.com : Mach, . Mach DEC Alpha, Workplace IBM. Chorus. Chorus/MiX V.3 Chorus/MiX V.4, SVR3 SVR4. UNIX , , , . UNIX PC UNIX . UNIX PC, (Andy Tanenbaum) UNIX. . Minix PC 8086 ( ). UNIX Linux, (Linus " " Intel i386, , Alpha DEC. UNIX , . , , , UNIX . , , , , UNIX. UNIX ( ), . , . "Books-shop"., , . [email protected], , , UNIX.IEEE POSIX 1980 UNIX, . . , . UNIX. 1984 ANSI, , . , , 1985 , Portable Operating System Interface for Computing Environment, POSIX ( ). 1988 POSIX 1003.11988, (Application ProgrammingInterface, API). , UNIX. 1003.11990. , , , , . POSIX, UNIX, :POSIX 1003.21992 UNIX POSIX , POSIX "" (threads) POSIX, pthreads 1984 , , , , www.books-shop.com 77 , . /Open . 1992 , Portability Guide 3 POSIX 1003.11988 XWindow System, . XPG3 , API BSD System V (SVID), STREAMS. , Spec 1994 XPG4.2. 1996 /Open OSF The Open Group, . , , Common DesktopEnvironment (CDE), Motif. DistributedComputing Environment (DCE), OSF.SVID 1984 USG System V Interface Definition, SVID, UNIX System V. , ( ) System V. SVID . . System V Verification Suite, , , System V. SVR4 SVID ( ) , , SWS.ANSI 1989 (American National Standards Institute, ANSI) . , , ( UNIX). , .www.books-shop.com12 UNIX , UNIX. , , . , , . SunOS Solaris Sun Microsystems, HPUX HewlettPackard, AIX IBM, IRIX Silicon Graphics. , , , , , . , , : . , , " UNIX". . " UNIX" , System V, BSD OSF/1. , , , Digital UNIX System V BSD UNIX, BSD. . . 1 ( ) UNIX . ? SVRx /etc/rc*.d /vmunixxBSD sh(1), csh(1)www.books-shop.com UNIX 13 1 ()"" SVRxS5 ( : ), lp(1), UFS ( : < 255 lpq(1), daemon)ps aux UNIX.AIX UNIX IBM SVR2 BSD OSF/1. (SMIT).HPUX UNIX HewlettPackard. 1996 HPUX 10.10 HPUX 10.20, ( 128 ) 3,75 . 1997 64 .IRIX UNIX Silicon Graphics, (MIPS). BSD UNIX, IRIX (6.x) System V Release 4. 64 .Digital UNIX OSF/1 Digital Equipment Corporation (DEC). DEC OSF/1 BSD UNIX. System V. 64 , Alpha, , UNIX, DCE, CDE, . , Fast Ethernet ATM.SCO UNIX 1988 Santa Cruz Operation (SCO), Microsoft InteractiveSystems System V Release 3.2 www.books-shop.com14 Intel 386. SCO AT&T SCO UNIXSystem V/386. 1995 SCO SCO Release 5 ( Everest) UNIX SVR4. 900 , , 2000 .Solaris UNIX SVR4 Sun Microsystems. 2.5.1 , 64 . , UltraSPARC, Intel 486, Pentium, Pentium Pro PowerPC. 1998 64 . UNIX UNIX . , . UNIX , , , , , UNIX . , , Solaris SCO, UNIX . UNIX, :1. , , . UNIX, , , 20% , . , . , UNIX . , UNIX , , .2. UNIX . . . www.books-shop.com UNIX , , , , (telnet, ftp, , DNS . .), .3. . UNIX, . , .4. , . , , .5. , . , . , , , .6. , , . UNIX , . 1.. 1. UNIXwww.books-shop.com16 (kernel). , . , . / (, , ), , . . , , , , UNIX. , . . : , . . . , . . , . (open), (read) (close) , read(2) close(2) .main{int fd;char/* ( )fd = 0_RDONLY) buf 80read(fd, buf,}www.books-shop.com UNIX 17 2. . 2. UNIX :1. 2. / , , . (2), read(2), , . , , / . ( ), . , , /.www.books-shop.com18 ( ). : (, ) , , . , . , (scheduler), (, , /). , , . , /, . . . , , , " ". , , ( , ), . : , . . . / / (, , . .). , .www.books-shop.com UNIX UNIX . , UNIX , . UNIX : . , / . , , . , . , . , UNIX; UNIX ; , , , ; UNIX, , . , UNIX , ; shell ; , UNIX. . , Internet , , . . "Books-shop"., , . [email protected] 1. UNIX UNIX , . , . , UNIX , . , , , CDROM, , , . UNIX "" ,, . , , , , UNIX, . , UNIX (), system). , . (root directory), (), , . UNIX , , ( , CDROM ), . , . , , , , , , . , , UNIX 4., , , . ( inode), , : , , . , . , .www.books-shop.com 21 UNIX 6 , : (regular file) (directory) (special device file) FIFO (named pipe) (link) , . . , . , , . .. . , , , . , . , , , . . 1.1 . , . ( mode), .. 1.1. www.books-shop.com22 1. UNIX . UNIX (character) (block) . , . , . , . , 5.FIFO , . FIFO System V UNIX, . 3.. , . , . . , , , , (hard link). , (second) , first (. 1.2).$ pwd$ first . , ls(l), first second . . . , , , . . , (first second) ,. . ( ). . , "", , "" . (. . ).www.books-shop.com 23. 1.2. , , , ls(l) www.books-shop.com24 J. UNIX$ i12567 first12567 second , , , /:$ 1 2 andrei staff 7245 Jan 17 8:05 second . , . , . , , , , . , (. 1.3). , , . . s :$ pwd$ first$ cd /home/sergey$ 1 1 andrei staff 15 Jan 17 8:05 symfirst ( ) second ( ). , symfirst , first second, 15 . , 15 . , symfirst ). ( , . , symfirst www.books-shop.com 25. 1.3. . TCP/IP. , www.books-shop.com26 1. UNIXBSD UNIX , , , . " BSD UNIX" 3. UNIX , . , , . .. 1.4. UNIX . UNIX. , www.books-shop.com 27 , . , , . /dev , . /dev , . , /dev/dsk ./etc . . /etc/rc2.d . , (run level), /etc/rc2 . . BSD UNIX , /etc/re UNIX System V default, (, UNIX System V /lib . libx.a ( libx.so), , . , X Window System libXll.a. "" . , UNIX , . . "" , . www.books-shop.com28 1. UNIX . , , 4./mnt () . /mnt , . 4./ /home . , andrei , , /u/andrei. UNIX /usr. , . . UNIX , . ./var UNIX System V /usr/spool, , . ./tmp , UNIX. . UNIX : (user owner) (group owner). , , . . (primary), (supplementary).www.books-shop.com . , . , , . , :1 2 3 4 5 6 7 8 1 group 235520 Dec 22 1 andy student 3450 Nov 12 quest , . UNIX. , SCO UNIX , , DigitalUNIX , , . , sergey $ sergey , . , staff , :$ staff * , , . , , . "" :1. , (. . 1. (group ID) EGID ( System V);) GID , ( BSD). , System V, BSD. SGID . . " " . "Books-shop"., , . [email protected] 1. UNIX , . , (. 1.1), ( ), , , ( ) . 1.1. chgrp(1) BSD 4.x SVR4 , ( POSIX) UNIX , :User access (u) Group access (g) , Other access () ( )UNIX : (read, (write, w) (execute, ). :rwxrxr1211andyandyandyandygroupgroupgroupgroup364826448897622DecAugDec2215221119:1311:0315: 1309: 13tempa . out ( , ). , ''. , , www.books-shop.com 31 , , (superuser) . . , 'g' , , ), , V Y ) , ('+' , '' '=' ) . . , $ chmod ownfile . , . :$ chmod a+w text $ chmod go=r text , $ chmod runme $ chmod text2 , , , www.books-shop.com32 1. UNIX . , $ 754 * : (0 , 1 ) , , . W X7 5 4 , $ chmod o=r * . , , . , cat(l), , . . , (w). , , (). , shell. , . , , , , , : , , . , , , , , . . , . , . , .www.books-shop.com ( ) , . (, "" , . , (. . ) cd (]). . , , , . , . , , "" , , , . , , "", . "" .$ ?$ darkroom $ 1 2 group 65 Dec 22 19:13 darkroom$ ar+x darkroom "" $ 1 2 andy group 65 Dec 22 19:13 darkroom$ cp f darkroom darkroom $ cd darkroom $ darkroom denied ...$ cat , ok (, , ) . , , , . , . , , , , . , www.books-shop.com34 UNIX , . , , , Sticky bit . . . 1.2 , . cd scatcat runme /u/andrei () report.txt report.txt runme runme WX , XXXXXXW, , , , . , 9 , 3 . , ( ), . :1. , . . www.books-shop.com .2. , :) (, ), ,) . , , , :) , ,) .4. (other) , , . . , , , , . , : 2 group 65 Dec 22 19:13 andy group, , filel. . , . , , andy (), , . , . , . , . chmod(l), Y . 1.3. , SGID filel $ chmod g+s f i l el . . 1.3 , , .www.books-shop.com36 UNIX 1.3. tSS1Sticky bitSet UID Sticky bit save text mode) UNIX . (, (. . ) , . ( ) SUID SGID , . ( ) , . SUID SGID , . , , , , , . SUID , SGID . . , ( /etc/passwd /etc/shadow, , ). , . SUID (, . , , . SUID SGID , .www.books-shop.com ( root), , , . . , .$rsrsrx 3 root sys 15688 Oct 25 1995 /us r /bi n/pas s wd*, . , , (, ). , . 4. (. 1.4. t Sticky bit , s Set SGID , BSD UNIX , . , , , . Sticky bit , . , , , . /tmp, , . SGID . . UNIX System V BSD, .www.books-shop.com38 1. UNIX :$ 1rsrsrx5 sys sys 367 Dec 19 20:293 root sys 15688 Oct 25 1995 / chmod(1) chmod(1) Sticky bit , UNIX . , . , . , , . , . . , , . , , , , . (run levels), , , , . , , . , www.books-shop.com , , / , . ( ) . , . , UNIX . , , , , . , . , , cat(l), . , (, FTP, Telnet), . UNIX . , , . , . , . UNIX . UNIX , (signals), (pipes), (shared memory), (semaphores), (messages) , . . . , , . : shed ( ), vhand ( ), ( ) "Books-shop"., , . [email protected] 1. UNIXkmadaemon ( ). ink, UNIX. init , (/etc/init), . , ( ), . ( , . 3) UNIX: , , . . . , , . , . , , . . , ls(l) . (login shell), UNIX. , login shell . , , ( ) . . , , , . shell, . , ps(l) , shell.www.books-shop.com UNIX , , . Process ID PID, . , (. . ) . , . . , , . , PID . , . Parent Process ID , . (Nice Number) , . , , . ( ) , . (TTY) , , . . (RID) (EUID) , . ( ). , . . , , . , www.books-shop.com42 1. UNIX SUID, (, ). (RGID) (EGID) , . . , ( SGID). ps(l) (process status) , , : pageout bdemiclock " UNIX".$ PSrootrootrootrootrootfedfedfedef |01236273143521head 200000131130451200000000DecDecDecDecDecDecDec17171717171717TTY99799pts/3pts/4TIME0:0:7;0:0:0::00:01:01:27:01 UNIX , fork(2) , . . , , . , , fork(2). , PID. , . , . . , (2). , . , , /, , .www.books-shop.com 43 UNIX , fork(2), , . ., , ( shell) (shell) fork(2), shell. , shell (2), , shell. ls(l) shell, ls(l) . ls(l) "". . . 1.5. , 2.. 1.5. "" , ps(l), , : :UID PID745PPID110STIME10:11:34TTY TIME0:01CMDwww.books-shop.com44 1. UNIX ls(l), shell UID PID PPID TTY TIME 745 1 10 10:11:34 0:01 sh 802 745 14 11:00:00 0:00 sh shell UID PID PPID STIME TTY TIME CMDuserl 745 1 10 10:11:34 0:01 shuserl 802 745 12 11:00:00 0:00 ls(l) :UID PID PPID STIME TTY TIME CMDuserl 745 1 10 10:11:34 0:01 sh forkandexec. , fork(2) (2). UNIX fork(2). forkandexec, (2). "" . " " , , . 1.6 sched ( ). . . , , . , 0, SIGFPE, , +, SIGINT. , fork , , 0. 2.www.books-shop.com 45. 1.6. "" UNIX kill , a pid , . , , ( , , , :$ &$ kill $! kill(1) SIGTERM; $! PID , :1. . , , , 0 , . , , kill(l), shell, .www.books-shop.com46 I. UNIX2. . , .3. , . , SIGINT , , "". , SIGKILL SIGSTOP , . 15 , . SIGTERM. SIGKILL (9), , :$ kill 9 pid , . : . , , . , , . , NFS (Network FileSystem), , , . , SIGINT , , , . ( ) (, SIGKILL SIGSTOP). , SI GHUP DNS . . 2 3. UNIX. kill I .www.books-shop.com , UNIX "" ( ) . , , , .UNIX . . (, , , ) . . (, , , ). , / . : , , . . , , . /dev Digital UNIX, SCSI: 1 root system 8, 1 Apr 18 11:03 1 root system 8, 1 Apr 18 13:15 , . , . , , . (rawwww.books-shop.com48 1. UNIXinterface). , , , :crwcrwr 1 1crwrwrw 1crwrwrw 1rootrootrootrootrootbinbinsystemsystemsystemsystemsystemterminalterminal8, 18, 113,17, 0V, 13, 23, 3Apr 18Apr 18Apr 18Apr 18Apr 18Apr 18Apr 1811:0413:1518:0815:2015:2016:1016:10/dev/rrzOb/dev/tty02/dev/tty03 . ( . (major) (minor) . . (, (, ). . 1.7.. 1.7. www.books-shop.com UNIX UNIX. , , . , , , , . . , ( ). Solaris:/dev/dsk/cOt4dOs2 , ( ) :/dev/rdsk/cOt4dOs2 /dev/dsk ( ) /dev/rdsk ( ). UNIX System V. , SVR4, : s n k , / ( SCSI ID), m , (LUN) SCSI. /dev/rdsk/cOt4dOs2 ( 0) LUN=2 . SVR4 . . , Solaris /dev/sdOa /dev/dsk/cOt3dOs, : root root 12 Oct 31 17:48 /dev/sdOa SCO UNIX /dev/root, /dev/usr . , (root, usr). "Books-shop"., , . [email protected] 1. UNIX . , , (, 1 . .). , /dev/ttynn, . . 1.6 . 1.6. /dev/rstn /dev/rst1/dev/cdn /dev/cdO SCSI CDROM/dev/ttypn/dev/ptypn /dev/ptyp5/dev/zero () () , 0 , 0 UNIX, , . . , . , . , , . , , . www.books-shop.com . , UNIX. , , , , , UNIX . , . . ( login name). (User Identifier)., . , ( , ). , . ( , ), , (GroupIdentifier). UID GID , . , , (). , . , /etc/shadow., /etc/group , . , /etc/passwd. , /etc/passwd . (, ), /etc/passwd , . , . UNIX .www.books-shop.com52 1. UNIX , . no : (), . :name . login. . , , . . , . , , , passwdencod. . . , passwdencod '' ( '!')., '*', . , '*' . , '*'. .www.books-shop.com53UIDGIDcomments . , , , . (, ). UID = 0, . . /etc/group, , . . (, )., "" . ,, . (, ) . . . , , , . , UNIX . UNIX . (Bourne shell), (C shell) shell), . , , , (restricted shell), . , , , . /etc/shells, , .www.books-shop.com54 1. , ( ) . : , . . , . : . , . , , , , , . , . , , . . , . , , . , . , ( , ). , : ; 2 ; 3 , . . , , :1. . , , . , '*' .2. , . 8 12.www.books-shop.com 55 : . , , , . , . , , .4. , .5. , . , . . ( , , ). . UNIX . ( UID ): root , , UID=0. . , "" . . , root . UID. UID=0 , bin , UNIXcron , , , , news , www.books-shop.com56 UNIX() nobody , NFSuucp copy (uucp), UNIX . , , , : root wheel , GID=0user users staff , UNIX UNIX UNIX , , , . , , , draganddrop, , , UNIX , , Windows NT. . . , UNIX . , , shell. UNIX shell. shell UNIX : Bourne shell shell shell , shell (bash), . UNIX, , :www.books-shop.com UNIX 571. , , shell. UNIX (. 1.8): , , . , , , shell. Shell , . . Shell , , , ( , ) , . , exit, .2. . , Bourne. shell , , , UNIX. shell (script). , . , , : , , . , , . "" , , . . 3.www.books-shop.com58 1. UNIX : Bourne shell (sh) shell (csh) Korn shell (ksh) BourneAgain shell (bash) . .. 1.8. , www.books-shop.com UNIX4. , shell. (, ), . Bourne shell , , , . , , , shell , , . , . , . Bourne shell, , . . , , , . , shell . ., , . Bourne shell '#': "" ( , core ..) , :find /home name core print core , : Bourne shell. , ( "Books-shop"., , . [email protected] 1. UNIX ), , . . , : var , a value . , , name echo :$ echo name:$ . , shell . :, , var , a command , :$ pwd , :$$ echo $cdir$ cd /usr/bin$ pwd/us r /bi n$ cd $cdir$ pwd , var, $var . . 1.7.www.books-shop.com UNIX 61 1.7. $var var; , var , var var, ; string. var , var , string var , string . string , var: parameter not set var , , echo:$ var=userl$ varl=user2$ echo $varluser2$ echouserl l$ echo want to redefinedo you want to redefi ne var? UNIX , . :PATHMAILTERMPS1PS2 shell shell ansi#> , : N , :www.books-shop.com62 1. UNIX() PATH=/usr/bin: 7 1 :$ pwd/u/us r$ cd some/new/directory$ pwd ( ) cd :$ cd $HOME . PATH , . , :$ run run . run , PATH :$ ( , ). , , shell . PATH ':'. , ('.'), shell . , PATH '.'. . , ( ), , /tmp, , , www.books-shop.com UNIXUNIX ( . , . ('.') ( PATH) , "" . , , , , PATH, . MAIL , . MAIL TERM . , , (, ). , , . PS1 PS2 . . , . , PS1. , , , , PS1 : , , , telemak, : , , . :$ while : > do > echo > done . ( , .)www.books-shop.com(54 1. UNIX, , . , , shell ( ). shell , . . , MAIL, , , , , TERM. . . , , shell , . , (. . #, $#). . 1.8. 1.8. shell $2, . . . $ shell , , . , : $2 $3 . .$@ , . , : . . , . . 4echo $0echo $1 $2 $3 2 4echo $1 $2 $3www.books-shop.com UNIX $1, $2, ... $9 . $1 $2 2 . . . , shift n, ( ). . $0 . argv[], . $# . . $ test2.shif [ $# 2 ] usage: t est 2. sh arg2then $ h2echo usage: $0 argl arg2 $exit 1fi if , . ($?) . , 0, . exit , (. ). , grep(l) sergey . grep(l) 0. , grep(l) , , sergey . test3.sh:grep sergey /etc/passwdif [ $? ne 0 ]thenecho sergey fi UNIX , PID. , PID. PID $$. , , :www.books-shop.com66 1. UNIX test4.sh: $ / /: . , , , , , , . , , . shell /. , , . . 1.9 /, . 1.9 . 1.9. >file file file file file To , filen>&m " ": str. To , . :$ loggerwww.books-shop.com UNIX67 logger . file.log , . , '>' , , . , , . , , , 1 2. , . , :$ logger . log. 1.9. . , , :$ run 2>/dev/null , . ., $ 2>S1 & /dev/null. '&' , 1. , , . , Shell , www.books-shop.com UNIX , ( ). :$ ps ef I grep myproc. ps(l) , grep(l) , , myproc. . $ ps ef | grep myproc I awk print $2 myproc, (. . " ". :$ at Dec 31cat | elm " , at(l) , ( ), ( 31 ). " " , :cat(l) ., , shell, : :$ ps ef I grep myproc | grep v grep , , , myproc: myproc grep(l) myproc (ps , ).www.books-shop.com UNIX shell, shell , . , shell (, , shell . shell.: . 0 (). , :while :dodone , shell, runme. , . include_script, .. runmebreak [] for while. , ps ef I awk print $1 Iwhile read uid piddoif [$pid eq $PID]thenecho pid=$pid user=$uidbreakfidonecd , echo [string] string ()exec runme, . , login shell( , ) exec Is, "Books-shop"., , . [email protected] obswww.books-shop.com UNIX71trap commandsig2type nameunset var2 .wait pad command, , sig. . "" , name , ( , , ). 2 , , . , PATH, PS1,PS2, shell. , , . shell, . :{} , shell. shell .mcd{cd}, , , , :1. . , , . www.books-shop.com72 UNIX, shell , , , .2. , : ., var f i nd $var name sh print . , :f i nd /usr/bi n name sh print , . :* ( ), '.',[abc] ( ),? . /. (>, 1 andy user 498 Jan 6 09: 53 uni x0. t xt$ 1975 AT&T , . , , , UNIX. www.books-shop.com , , . . :#includecaddr_t addr, size_t int prot,int f l ags , int fi l des, len f i l des, of f , addr., prot , , (2). . 2.12 prot . prot. PROT_READ | . 2.12. protPROT READPROT WRITEPROT EXECPROT NONE wX addr 0, . , . len , 96 , 4 , 4096 . 96 , 4000 . 3.www.books-shop.com138 2. UNIX 4000 , . , , , . (prot = PROT_EXEC) . , , . (, ), . . . 2.13 , 2.13. flags MAP_FIXED , addrMAP_NORESERVE He , , , . , , , MAP_SHARED . . . . , , addr. addr 0, . , , , . , .www.books-shop.com 139 . . , ,. . . . . #includeraain(int argc, char{intcaddr_t addr_src,struct stat filestat; , | /* lseek(fd_dst, 1,/*3 fd_src,PROT_READ | fd_dst, 0);/* addr_src,exit}, , , lseek(2) . . . , . "Books-shop"., , . [email protected] 2. UNIX , . "",, , . int char uid_t owner, gid_tint fildes, owner, gid_tint char uid_t owner, gid_t , . , (. . ). , . owner group . BSD UNIX . , , "" , , . , , , . System V , UNIX System V . SUID SGID, , . , EUID EGID. UID GID , EUID EGID . . , , , . , , ,www.books-shop.com 141 . SUID SGID. . iinclude int chmod(const char *path, mode_t f i l des, mode ( SUID, SGID Sticky bit), , . 2.14. (, ) . 2.14. mode S ISUID 04000 SUIDS_ISGID SGID, 7, 5, 1 , # 6, 4, 2 S ISVTX 01000 Sticky bit 00700 , S_IRUSR 00400 S 00200 00100 00070 , S IRGRP 00040 S 00020 S IXGRP 00010 00007 , S IROTH 00004 S 00002 S_IXOTH 00001 www.books-shop.com142 2. UNIX , , . , , | | S_IXUSR. S_ISGID , , SGID, . , , . ls(l) .ttinclude main{int fd;/* */fd = 1 1 */ | 1 1} , :$rwsrwsrws11andyandyuseruseruser000JanJanJanD666 .19:19:19:; :28:28:28mymymyfilefilefile , (root directory) (currentworking directory). (, , , , , www.books-shop.com . '/', . ., , , int charint , , path., , , , , , , . . . int charint, :char , , == 1) . , . . ( ) , , .www.books-shop.com144 2. UNIX , , , , , , , . inode. int char struct stat (const char struct statint fildes, struct stat stat, . 2.15. stat mode t st mode ino_t inode. st_ino st_dev , ( )dev t st devdev_t , t st uid t st uid gid_t st_gid . off t st size st_atimetime t sttime t st ctime long st bl ksi zelong st_blocks 512 . , :www.books-shop.com 145 2.16. SSSS FIFO , ( , ) , 0 1970 . , . , : #include argc, char{struct statchar , &s) ) ptype = " ";else st_mode) ) ptype = "";else ptype = ";else ptype = ". ";else ptype =else ptype = "";else ptype = "FIFO";else ptype = " =/* = &/* inode*/ =/* =/*, = (%d,www.books-shop.com 2. UNIX = =/* = (%d, = , = = =} , , . . :$type = perm =inode = 13 = 1dev = (1, = 286 = 100rdev = (0, 0)size = 1064atime = Wed Jan 8 17:25:34 1997mtime = Wed Jan 8 17:19:27 1997ctime = Wed Jan 8 17:19:27 1997$13 1 andy user 1064 Jan 8 f 1 . , . . . . , , . : www.books-shop.com /47 : , . , . . . , . " " 3. ; . , , , . , , . PID PID , , , . PPID , ( ) . , : UID, EUID, EGID. , , . , , UNIX. , , . SGID. , login shell, . UID (EUID) GID(EGID) shell , /etc/passwd. , , .www.books-shop.com148 2. UNIX , , , , shell. login shell, . , "" SUID SGID. , , , ! . .. 2.10. www.books-shop.com 149 :uid tuid_tgid_tgid_t . :ttinclude intint gid) setgid(2) , setegid(2) . , . , . . 2.17. 2.17. passwd char char *pw_passwd , ; /etc/shadow, gid_t char ( GECOS), char char "Books-shop"., , . [email protected] 2. UNIX, , . :linclude struct passwd char, : passwd *pw;char char char/* logname, pw =/* , if ( pw == 0 )/* else{ arg,} execve(2) , . " " . , . , , (heap ). ., , , . :www.books-shop.com1. , , :char ptype = "Unknown fi l e type"; ptype , .2. ,:char ptype , , , BSS. , , . . :fund{intcharstatic int = . , . 3.4. . , , . , , , ., (heap) , , . (break address). . www.books-shop.com752 2. UNIX UNIX brk(2) intvoid brk(2) endds , , (. 2.11). sbrk(2) incr. incr 0, , , . brk(2) , / . voidvoid size_tvoid *ptr, size_tvoid si ze . calloc(3C) nel em , el si ze. . realloc(3C) ( , size). , ( ) , . , 100 4096 , 4.www.books-shop.com /53 , . free(3C) , ptr., calloc(3C) realloc(3C), , . , double , 8, , . sbrk(2) brk(2). , , , free(3C). , , , , free(3C), . #includetti ncl ude {charcharchar obrk = = 64 naddr = nbrk = Ox%x,= % ( naddr, nbrk, nbrk , obrk =www.books-shop.com 2. UNIX " = % ( obrk} : 20 = 208, 22 ) (Ox20ac8) 22 ( 0 )$ , free(3C), . , , . , . . shell , , , . , . UNIX . : , . . h> , , , , . , , : , , , , , , , , , . , : , , , www.books-shop.com /56 . . , . , : PPID , , , , fork(2) . . , , . PID , , , , 0. 1, (, , ). fork(2) , , , , . :/* }else }} , , , , www.books-shop.com156 2. UNIX. , , , : , . , . , , , .UNIX , , . . . :int (const char const char *argO,const char *argn, char *int char charint execle (const char argO [ ,const char char * charint execve (const char char argv [charint execlp (const char *file, const char *argO,const char *argn, char *int execvp (const char *file, char *const execve(2), ( ), . , , . . 2.12 , . fork(2), . , : PID PPID, , ( , SUID SGID), , , , , , , FD CLOEXEC.www.books-shop.com 157. 2.12. (2) . , , UNIX. / . . 3. 1 (2), , .www.books-shop.com 2. , , "" "". , ls(l), . , . "" . . . , . idtype, id_t id, int options);pid_t pid, int *stat_loc, int , . stat_l oc, . stat_l oc : () , . WIFEXITED(status) , ( , . WIFSIGNALLED(status) , , . WIFSIGNALLED(status) , core. . idtype id , :www.books-shop.com /59 idtype , , PID id. waitid(2) , , id.P_ALL , . options , , , options . (trap) (breakpoint) . . , . , (. . ). , , . i nf op siginfo_t, . . . . /* get "Books-shop"., , . [email protected] 2. UNIX/* : and and,pid =if (pid == 0){/*3/* pexit}else (shell) . , , , . UNIX, . "", () . , BSD, System V, , (reliable) . , BSD, System V. POSIX.1 . , . , , +, SIGINT. , +, UNIX 15 , . , ttinclude int t pid, intwww.books-shop.com , . . : ( ) . , ( SIGINT, , , , 0, . ., . , (, ). , , . . , , . SIGALRM, , , kill(2) , . , , , , . , , . . , : , . ., SIGSTOP , . SIGKILL "" , SIGSTOP . . .www.books-shop.com 2. UNIX . 2.18. , . core ( . . core : ; SGID, ; ;D core ( . "" ). 2.18. , , , alarm(2) setitimer(2).+ . , . , ( , ). , .+ , .+ , 0 .www.books-shop.com 2.18 ()SIGQUIT , , , (). . . , . SIGHUP , , , .SIGILLSIGINT , . +). , . , .SIGPIPE , ( ). , . . , (UPS).+ +.SIGSTOP +. .+ .www.books-shop.com164 2, UNIX 2.18 ()SIGTERMSIGTTINSIGTTOU , . " " , . . kill(1) . ( ) . ( ) . . . UNIX , , UNIX. fork(2) . (2) . , , (2). : void (*signal (int sig, void sig , . , :SIG_DFL , , . . . , . , .www.books-shop.com . . . , , . . . , , . . signal(3C). , : static void signo){ {pause : SIGINT, SIGUSR1 SIGINT SIGUSR1 ( ), . , , , 1, errno EINTR. , SIGINT , ( ).www.books-shop.com 2. UNIX , :$ &[1] 8365 PID $ kill SIGINT 8365 SIGINT SIGINT $ kill 8365 SIGUSR2 $ kill SIGUSR1 8365 User Signal 1 SIGUSR1 $ . POSIX. 1 UNIX . , POSIX, (signal set), sigset_t. . 32 , 32. :frincludeint *set, intint *set, int signo);int *set, int , si gset t, . , . , . , . sigaddset(3C) . , signo . 1 , . :www.books-shop.com 167 int sigaction (int sig, const struct sigaction *act,struct sigaction si gacti on, :void (*sa_handler) () sigvoid (int, siginfo_t *, void *) sig sigset_t int , , SIG_DFL . sa sigaction NULL, , . , , . , . . , , , . , , . , UNIX , , . , . : , sigaltstack(2) , . , .SA_RESETHAND* , SIG_DFL, . , .SA_NODEFER , , sa mask. , .* UNIX BSD.www.books-shop.com168 2. UNIXSA_RESTART , , , , , , , sa_handler. sig SIGCHLD, . wait3(2), .SA_NOCLDSTOP* sig SIGCHLD, . UNIX BSD 4.x si gacti on :struct sigaction {voidsigset_tint :void signo, int code, struct sigcontext signo , code , a scp . UNIX System V . SA_SIGINFO, sig , , , si gi nf o_t , , . si gi nf o_t :int int si_errno int si_code , , , , ioctl(2), www.books-shop.com si_signo . si_code : , , si gi nf o_t :pid_t si_piduid t si uid UID , ; si_code , . si_code , si_signo, . 2.19 2.19. si_code si_signo si code SIGILL ILL_ILLOPNILL ILLTRPILL PRVOPC (opcode) (trap) SIGFPE FPE INTDIVFPE_INTOVFFPE_FLTDIVFPE_FLTOVFFPE FLTUNDFPE FLTRESFPE FLTINVFPE_FLTSUB (underflow) "Books-shop"., , . [email protected]/70 2. UNIX 2.19 () si signo si code SIGSEGV SEGV MAPPERSEGV ACCERR SIGBUS BUS ADRALNBUS ADRERRBUS OBJERR , SIGTRAP TRAPTRAP TRACE SIGCHLD CLD_EXITEDCLD KILLEDCLD DUMPEDCLD TRAPPEDCLD STOPPEDCLD CONTINUED "" SIGPOLL POLL INPOLL OUTPOLL_MSGPOLL ERRPOLL_PRIPOLL HUP www.books-shop.com 171 , (. 2.20). 2.20. siginfo_t si signoSIGILLSIGFPESIGSEGVSIGBUSSIGCHLDSIGPOLL t tintlongsi addrsi addrsi pidsi statussi band ( STREAMS) tinclude int how, *set, how, : set set set NULL, how . oset NULL, , , . , : int , , , set. , set, , , .www.books-shop.com 2. UNIXt t i ncl ude int sigset_t , , . , . , , BSD UNIX System V . , . BSD UNIX. "" , , .t t i ncl ude t t i ncl udet t i ncl ude t t i ncl ude tti ncl ude /* "" */void (*mysi gnal (int signo, void ( *hndl r ) ( i nt ){struct sigaction act, oact;/* = = 0;if (signo != SIGALRM) lags |=/* < 0)}/*static void signo){/* (SIGINT,*/printf ( " www.books-shop.commysignal (SIGUSR2,}, , . , fork(2) . (process group ID). , . , , , . , . , . , kill(2) , . . , , (session), . UNIX , , , . , " " , , . , . : getpgrp(2) ttincludettincludepid_twww.books-shop.com 2. UNIX pid, , . , , , . setpgid(2) .tti ncl ude int pid, pid_t pid pgid. ( ). , (2), . , pgid, (group leader) . , . , , . t t i ncl ude#includepid_t get si d(pi d_t , pid , , , getsid(2)., , . :#includepid_t , . . , , . , (control l i ngterminal), , , www.books-shop.com . / , ,, . . ( shell) , ( , ). shell , SIGHUP . , , , . (foregroud group), (background SIGINT SIGQUIT, , . , , SIGSTP,SI GTTI N :$ find / name &$ cat | sort , , cat(l) sort(l), . , . , , . , "" , , . . (job control). , shell.www.books-shop.com 2. UNIXandyandy2436243124072435 SID 240724072405240724352431240724352407240724072407100015:51:3015:51:2515:31:0915:51:30TTYttyOlttyOlTIME0:000:000:000:00CMDsortfind / name fooshcat (sh, find, cat sort) , ttyOl. , (2435) (2407). find(l), (2431). , , a . ps(l) , , , , cat(l) . , , cat(l) , ttyOl. , ( SIGINT. ( ), , , SIGINT. . , . .. 2.13. , $ ps efj | egrepwww.books-shop.com , , shell. SIGINT ( +) shell .UNIX . , . "" , , . ., . , , , , . ttincludeint resource, struct rlimitint resource, const struct rlimit resource , . rlimit :, , (soft) (hard) . , ., 64, 1024. . . . ( ). , . rlim_max RLIM_INFINITY. (, ) . . 2.21.www.books-shop.com178 2. UNIX 2.21. ( resource) core, . 0, core . core . . SIGXCPU. , . . . brk(2) FSIZE , . 0, . SIGXFSZ. , EFBIG.RLIMIT NOFILE . , EMFILE.RLIMIT STACK . SIGSEGV. . . ( System V.) brk(2) ENOMEM.RLIMIT NPROC , .( BSD UNIX.) , www.books-shop.com 2.21 () (RSS Resident SetSize). , . ( BSD UNIX.) , , RSS.RLIMIT MEMLOCK () , mlock(2). ( BSD UNIX.) mlock(2) EAGAIN. , : void resource, char *rname){struct rlimit if ==elseprintf ( if ==printfelse} STACK, "RLIMIT "Books-shop"., , . [email protected] 2. UNIX/* BSD */ RLIMIT_NPROC/* BSD */ttifdef/* BSD */ttifdef RLIMIT_MEMLOCK )#endif/* System V */#endif} Solaris 2.5 :$ infinite infiniteinfinite infinite2147479552 2147479552infinite infinite64 10248388608 2147479552infinite infinite , UNIX. , , . UNIX. . . , www.books-shop.com , , , , ( ) (). , . , , . " ", , . , , :1. , / . , , .2. ( ), /. , , , . , , "" UNIX.3. . , (SIGINT , .4. syslog(3), .5. . , , , , . , . : ttinclude www.books-shop.com 2. UNIX argc, char **ar gv){ fd;struct rlimit/* init, . , / :SIGTTOU, SIGTTIN,if != 1){signal (SIGTSTP, SIG_IGN)/* , , . , , . . . PID , , , , . . shell , , , . , , shell if !=0) /* /* , }/* . . for (fd = 0; fd < fd++) UNIX System V. BSD UNIX , , TIOCNOTTY .www.books-shop.com PID , " " (. . * / ", | " , } . syslog(3), syslogd(lM), , , . : void (int priority, char *logstring, /* logstring , pri ori ty. :LOG_EMERG "" . . , , , . , , .LOG_ERR . .LOG_NOTICE , , . , , , . , .www.books-shop.com 2. UNIX , . l ogstri ng , , , errno. . . :void int logopt, ident . logopt , :LOG_PID . ident, , fork(2).LOG_CONS ., f aci l i t y :LOG_KERN , . , ( ). , . , . , USENET.LOG_CRON , , void closelog , . , . : , www.books-shop.com , (2).ttinclude extern char ** environ;#define 80 #define CDttdefine ECHO 2 EXEC 3define PROGRAM 1000/*, , , , , / path arguments. , int char charmain{int command;intcharcharwhile (1){/* write (1, 2)/* cmdsize = read(0, =command ={/* , (CD) : break; (1, args[0],break; args,write (2, "shell: cannot execute",21)break;www.books-shop.com 2, UNIX/* , , =if (pid < 0}wr i t e ( 2, "shel l : cannot f or k" ,else if (pid == 0){ args,wri te"shel l : cannot execut e",}else/*0 break;}}} , . , , . shell , execve(2) . , . UNIX . UNIX , , . . , , . , UNIX , UNIX, .www.books-shop.com UNIX . , , , , , / . . (run levels) , . , , UNIX, , FTP, (news) . : , , UNIX , . UNIX ( ) . , "", . , . . ! , UNIX . UNIX , .www.books-shop.com 3. , ( , ). "", "" "" . , , , , . . , . UNIX UNIX System V BSD. , UNIX , , , , , , . . 3.1 , . , , /. UNIX , . UNIX , PDP7 . , PDP7 , fork(2). UNIX (2), . , , ( ) , . 1972 UNIX , . . / , , "" /www.books-shop.com ). , UNIX 1973 . .. 3.1. UNIX (kernel mode) (user mode). , . . , , , . , , . . , . , , , , . : . "Books-shop"., , . [email protected] 3. , , , , . , , . , , , . ., , . , . user, , , UNIX. . 3.1 SCO UNIX, , , . char (, , . .)char unsigned int , unsi gned shortUID unsi gned short p suid int short p_pgrp ( )short (PID)shortsigset tunsigned inttime tp sizep , t caddr t p LDT struct pregion *p_region short , unsigned int [ ] uareawww.books-shop.com , , , , . , , , . , , , . curproc. , , curproc, . user, block, , (. . ). curproc, user (, ) . . 3.2 UNIX. uarea , . , , , , , . , , uarea . . 3.2, uarea , (kernel stack). , . . . . 3.3 , .www.books-shop.com 3. . 3.2. 1. . .2. . .www.books-shop.com 3. , , ( , . (asleep), , /. , . fork(2) : , .7. exit(2) (zombie, defunct). , , , . .4.5.6.. 3.3. , , .www.books-shop.com 3. 6, fork(2). , " " 3 , . , 1 . , , . . . , sleep () (4). , . , " ", wakeup , " (context switch), , , , . , , , , . . , . , , . , , , . : , , . , , . , , . UNIX , www.books-shop.com . "" SIGSTOP, SI GTTI N SIGTTOU. , , , , "". , " ". SIGCONT, "" " ", , , "". SVR4., exit(2) . . ,, , "". , , . . , , (Random Access Memory, RAM) . , , , . , , , . , UNIX. , , , , . . , , , . . , (, , ). . "" .www.books-shop.com 3. UNIX . (MemoryManagement Unit, MMU) , . . , , . ( , , ), , , . . , , UNIX. , : , . . . . . . , , , . UNIX . . , . " ", : , ; /; 7% . .www.books-shop.com , , . UNIX 64 , , , . , , . , . . , , , , , . : , , , , , . , , , . . , . . , . , . , 1 , 8 , 80 ! . , , . . , . www.books-shop.com198 3. . , . , . ( )"" . ., . ( . . , , . . . 3.4 . , , , , . , .. 3.4. www.books-shop.com 199 , , . , , , . , , (page fault), , , . , . () . , SCO UNIX Intel. , UNIX, , , , . Intel , . , , . , , . : . (, INDEX) , , , . , , . . , , . , . "Books-shop"., , . [email protected] 3. (Local Descriptor Table LDT) (Global Descriptor Table GDT). , LDT , GDT (, ). LDT, GDT . , , , . 3.5.1 5INDEX TI RPL. 3.5. 0, GDT, LDT. RPL . , , , , , , SIGSEGV. LDT GDT . , , , , , , , ,, . , , . (, , ) : 32 . 32 . . , . , , , . , , (, ), .www.books-shop.com , Descriptor Privilege Level (DPL), RPL . , , . . RPL DPL. . , , , . UNIX , , . . . , , . , , . , . , . . . , , , (. 3.6).. 3.6. www.books-shop.com202 3. , , . , , . , , 3 , . . 1 , . " ". , , , (, ). , , . , , , , , . . (4 Intel) , . Intel 4 . , , . , . , , . , , . , , , . , .www.books-shop.com 203 , , . , . 3.7.. 3.7. , 22 31 , (Page Directory Entry, PDE). , , 1024 (page table). , . , 12 21 , (Page Table Entry, ). 4 , 1024. , ., , 12 . , = 4 . . 3.7 , . PDE ( 10 ) . . ,www.books-shop.com 3. , , . , , 32 (. 3.2), . 3.2. . , (=0) , , , . U/S . U/S = 0, () . , . ( ). . , , . , , , , , , . . . 3.8. (CR3 Intel) . SCO UNIX , , CR3 . ( ) , , 1 , , . 256 . , , , Intel, . , . , , , .www.books-shop.com 205 0x00000000. 3.8. , , , 768 . 3 . , (), , , , . . 3.9 COFF ELF. , 3 . . , , . , .w