- | 4 +----------------------->| 3 | -| | | | +---+---+ ++------+ - | | ^ ^ | | | | | | | | | | | +---+ | - - | | | | | | | | | |- ++------+ | | |-| | fork | | | | 8 |<----- | | | | | | | | ++------+ | | | | | | | | - | | | +---+ | | | | | | | | ( | | | | ) v v | v +-------+ +---+---+ | | | | | 6 +----------------------->| 5 | | | | | +-------+ +-------+ , , 7.6. 189 . , " ", . , , signal. issig , , - . +------------------------------------------------------------+ | issig /* */ | | : | | : "", - | | , | | "" - | | { | | ( , - | | , | | ) | | { | | , ; | | ( " ") | | { | | ( ) | | , | | , -| | ; | | ( -| | ) | | (); | | } | | ( ) | | (); | | () , | | , | | ; | | } | | (); | | } | +------------------------------------------------------------+ 7.7. 7.2.1  , , , . : , () . , , exit, signal , . signal: oldfunction = signal(signum,function); signum - , , , function - , oldfunction - . signal 1 0: function = 1, 190 signum ( , " ", 7.4), = 0 ( ), . - , - . , - , , . - . +------------------------------------------------------------+ | psig /* | | */ | | : | | : | | { | | ; | | ; | | ( signal, - | | - | | ) | | ; | | ( , - | | ) | | { | | | | ; | | /* | | */ | | , | | ; | | : | | , - | | ; | | : | | | | , - | | ; | | ; | | } | | ( - | | ) | | { | | "core"; | | "core" -| | ; | | } | | exit; | | } | +------------------------------------------------------------+ 7.8. ( 7.8), () , - . - , () (. 7.7). - 191 , . - , - - , , , - . , - . , , "delete" "break" , , - , , "" - . . (quit), , , , . , , - - , , (). , - , , . - , , , . - , , , , : 1. , - . 2. - . 3. , , - , - , - . , - ( ) - , - ( ). 4. : - , . , ; - , - , . ( 7.9), - (SIGINT) ( - kill). 7.10 - , - VAX 11/780. kill () ee; - , kill, chmk (- ) 10a. - 10c. . , - , - 10c . - 192 , 104, . 7.11 . - . - , - . , - signal. - +-------------------------------------------+ | #include | | main() | | { | | extern catcher(); | | signal(SIGINT,catcher); | | kill(0,SIGINT); | | } | | | | catcher() | | { | | } | +-------------------------------------------+ 7.9. +--------------------------------------------------------+ | **** VAX DISASSEMBLER **** | | | | _main() | | e4: | | e6: pushab Ox18(pc) | | ec: pushl $Ox2 | | # signal | | ee: calls $Ox2,Ox23(pc) | | f5: pushl $Ox2 | | f7: clrl -(sp) | | # -| | kill | | f9: calls $Ox2,Ox8(pc) | | 100: ret | | 101: halt | | 102: halt | | 103: halt | | _catcher() | | 104: | | 106: ret | | 107: halt | | _kill() | | 108: | | # | | | | 10a: chmk $Ox25 | | 10c: bgequ Ox6 | | 10e: jmp Ox14(pc) | | 114: clrl r0 | | 116: ret | +--------------------------------------------------------+ 7.10. 193 | | | | | | +-->+--------------------+ | | | | - | | | +-- --+ | | | | | | | | | | ---->| (10c)| +--------------------+<--+ - +--------------------+ | | - | | | | - | | | | - | | +--------------------+ - +--------------------+ - - +--------------------+ - +--------------------+ | | - | | | (10c) -|---------------- | (104) | +--------------------+ +--------------------+ | -| | -| | - | | - | | | | | +--------------------+ +--------------------+ 1 1 7.11. : , , , . , , - . , - 7.12. signal , sigcatcher. , nice, - (. 8), . - 5 , - nice . , ( kill). , , - , - , kill , - . , - . - signal , 194 +------------------------------------------------------------+ | #include | | sigcatcher() | | { | | printf("PID %d \n",getpid()); /* | | PID */ | | signal(SIGINT,sigcatcher); | | } | | | | main() | | { | | int ppid; | | | | signal(SIGINT,sigcatcher); | | | | if (fork() == 0) | | { | | /* */ | | sleep(5); /* | | 5 */ | | ppid = getppid(); /* - | | */ | | for (;;) | | if (kill(ppid,SIGINT) == -1) | | exit(); | | } | | | | /* , -| | */ | | nice(10); | | for (;;) | | ; | | } | +------------------------------------------------------------+ 7.12. , . , : 1. . 2. - , , , signal . 3. , . 4. , . , . , - nice , . ( ), , , - , , , , - . , , . , 195 . , : , - , . , , , , . , , . , , , - , - . BSD, , - - ; , , () . , - , . , - : - . , , - . ( longjump), . , - , , . , , , - , BSD. , . , - , - , longjump. , , . . - , , , . - , , - . , ( sleep), , - . , , sleep (. 2), , - . , " " , . , " ", , . " - " , - . , , , . - , , - , 7.4. , signal " " (death of child), , , . 7.4 . 196 7.2.2  , UNIX (PID), - "", . , , - shell', , "delete" "break", "", . , - . ; . , , - , setpgrp. : grp = setpgrp(); grp - . fork - - . setpgrp , (. 10.3.5). 7.2.3  kill. - : kill(pid,signum) pid ( - ), signum - . pid - : * pid - , pid. * pid 0, , , kill. * pid -1, , , - , kill ( . 7.6). , , - , , 0 1. * pid - , -1, , , pid. , , , , , ( ) , , kill - . , 7.13, - 10 . - - , , , , . getpid getpgrp , , pause - . kill - , . 197 +------------------------------------------------------------+ | #include | | main() | | { | | register int i; | | | | setpgrp(); | | for (i = 0; i < 10; i++) | | { | | if (fork() == 0) | | { | | /* */ | | if (i & 1) | | setpgrp(); | | printf("pid = %d pgrp = %d\n",getpid(),getpgrp());| | pause(); /* - | | */ | | } | | } | | kill(0,SIGINT); | | } | +------------------------------------------------------------+ 7.13. setpgrp "" , - , "" - . 7.3  UNIX , exit. " - " (. 6.1), . : exit(status); status - , . - exit , ( - : (startup), - , exit main, ). , exit , ( ). status . - , - . ( ) 1 (init), - , . getty-, , , , - . 7.14 exit. , , - . , exit, 198 +------------------------------------------------------------+ | exit | | : , - | | | | : | | { | | ; | | ( , - | | ) | | { | | , , | | ""; | | ; | | } | | ( -| | close); | | ( iput); | | , | | ( freereg); | | ; | | ( -| | ); | | - | | init (1); | | - , | | init " "; | | " " -| | ; | | ; | | } | +------------------------------------------------------------+ 7.14. exit , (. 10.3.5), , - , "". , shell' - , " " (Ctrl-d), - , , exit, "". , , , , (, exit) - . , , . , close iput - ( ). , - ( detachreg) . exit (status), - . 7.4 wait , - . - , - , , - , -, - 199 . - , - . , - 1 (init). , 1 . - , - init " " , (. 7.9); , . wait. , ; , -