. , . , - , , SEM_UNDO. , , - . 0, . , - +---------------++-------+ +---------------++-------+-------+ | || | | || | | | || semid | | || semid | semid | +---------------++-------+ +---------------++-------+-------+ | || 0 | | || 0 | 1 | +---------------++-------+ +---------------++-------+-------+ | || | | || | | | || 1 | | || 1 | 1 | +---------------++-------+ +---------------++-------+-------+ () () +---------------++-------+ | || | | || semid | +---------------++-------+ | || 0 | +---------------++-------+ | || | | || 1 | +---------------++-------+ () () 11.17. 352 , - . , - , , - , 0. 11.17 'a'. - , semid, - , 0, , 1, , 1, - , 1. , - , - 0. - 1 , - , , - . , - . - , , , - . , - , - , . semctl: semctl(id,number,cmd,arg); arg : union semunion { int val; struct semid_ds *semstat; /* . - * */ unsigned short *array; } arg; arg cmd, , ioctl ( 10). - , cmd: ( .), , . - . - , IPC_RMID, , - , - . - , - : - , , - , . 11.2.4  353 . "get" - creat open, "control" , unlink. , - , , close. , , IPC, , - , , , "get". - , , . , - , . , - - , - - . , , (keys). , - . - , - . (. 13). , " ", , , - , , . , , , . , (. ). 11.3  , , , - , , - - . , , , , ( "mjb" "/usr/mail/mjb"). - , mail () - , . , mail . , , mail . , , , ; - , - . - () . UNIX fork, , , . - ( - ), . 354 , , init, , , - , . , , ; , - . uucp, - (. [Nowitz 80]). - - (, ), , - - . , - uucp; init getty- - , . - - : getty- , uucico, "/etc/passwd", - - , - . UNIX , , - . - . , . , - , , , - . - ioctl, -. , , , , - . UNIX, . V - , - - . - BSD, . 11.4  , , , , - . , - , - , () , - open read . - BSD , - "sockets" () (. [Berkeley 83]). ( ). 355 - - | | +--+ +--+ +-------------------------+--+ +--+--------------------------+ | | | | +-------------------------+--+ +--+--------------------------+ | TCP | | TCP | | | | | | | | IP | | IP | +-------------------------+--+ +--+--------------------------+ | | | | | Ethernet| |Ethernet | +-------------------------+--+ +--+--------------------------+ +---+ +---+ | | 11.18. : , ( 11.18). - ( ) , , ( - TCP IP), , . - ( ); . - -: , , - - , , - . - . , , , - ( ), - ( ). BSD 4.2 : "UNIX system" - "Internet" () - DARPA ( - ) (. [Postel 80] [Postel 81]). : - ( , ) . - . , - , - ; , . - . , , "Internet" - (TCP), - (UDP). . socket . sd = socket(format,type,protocol); Format ("UNIX system" "Internet"), type - - ( ), protocol - , . sd, socket, . 356 close. bind : bind(sd,address,length); sd - , address - , - , ( socket). Length - address; , , . , "UNIX system" . - - -. connect - : connect(sd,address,length); (. bind), address , - . - , . - , connect - send ; . - , - . - listen: listen(sd,qlength) sd - , qlength - - - , . +--------------------+ +-------------------------+ | - | | - | | | | | | - | | | | | +----+ ------ | | | | | | - | | | | |listen addr accept addr| +---------+----------+ +-----+-------------------+ | | - +--------------------------+------------- 11.19. accept , -: nsd = accept(sd,address,addrlen); sd - , address - , , addrlen - . addrlen , . - (nsd), sd. - , ( 11.19). 357 send recv . send: count = send(sd,msg,length,flags); sd - , msg - , length - , count - . flags SOF_OOB ( out-of-band - " - "), . , - , out-of-band , "delete". recv: count = recv(sd,buf,length,flags); buf - , length - , count - , . (flags) , - , - out-of-band. - , sendto recvfrom, - . send recv read write. , , , read write, . shutdown : shutdown(sd,mode) mode , (, - ) . , - , , . close. getsockname , - bind: getsockname(sd,name,length); getsockopt setsockopt - - . , 11.20. "UNIX system" sockname. listen - . accept , ; , - . - , - -; - , . - - read. - - . 11.21 -, . , , sockname. 358 +------------------------------------------------------------+ | #include | | #include | | | | main() | | { | | int sd,ns; | | char buf[256]; | | struct sockaddr sockaddr; | | int fromlen; | | | | sd = socket(AF_UNIX,SOCK_STREAM,0); | | | | /* - */ | | bind(sd,"sockname",sizeof("sockname") - 1); | | listen(sd,1); | | | | for (;;) | | { | | | | ns = accept(sd,&sockaddr,&fromlen); | | if (fork() == 0) | | { | | /* */ | | close(sd); | | read(ns,buf,sizeof(buf)); | | printf(" '%s'\n",buf); | | exit(); | | } | | close(ns); | | } | | } | +------------------------------------------------------------+ 11.20. - "UNIX system" +------------------------------------------------------------+ | #include | | #include | | | | main() | | { | | int sd,ns; | | char buf[256]; | | struct sockaddr sockaddr; | | int fromlen; | | | | sd = socket(AF_UNIX,SOCK_STREAM,0); | | | | /* | | /* */ | | if (connect(sd,"sockname",sizeof("sockname") - 1) == -1)| | exit(); | | | | write(sd,"hi guy",6); | | } | +------------------------------------------------------------+ 11.21. - "UNIX system" 359 - . - . , , - "Internet", : socket(AF_INET,SOCK_STREAM,0); , . BSD , . connect , - ( - ), , - -. - , select , . 11.5  . , , - , . ptrace , , , -, , - . V UNIX (IPC), , . - , , - - . , . UNIX . - ioctl, -- . BSD , - . V 10 , . 11.6  1. , debug wait ( 11.3) ? (: .) 2. ptrace . - , - ? - ptrace ? 3. ptrace , - pid , . , - : 360 , ? - ? 4. - , - . , ; . - , , , - ( ). - . - . , - , , - . 5. , 11.22 ? *6. , - . ? 7. , 11.14, IPC_NOWAIT, . , . 8. , P V . P ? 9. , () , () creat unlink, () - . . 10. - , , read write, . 11. . , , - - . +------------------------------------------------------------+ | #include | | #include | | #include | | #define ALLTYPES 0 | | | | main() | | { | | struct msgform | | { | | long mtype; | | char mtext[1024]; | | } msg; | | register unsigned int id; | | | | for (id = 0; ; id++) | | while (msgrcv(id,&msg,1024,ALLTYPES,IPC_NOWAIT) > 0)| | ; | | } | +------------------------------------------------------------+ 361