. , 7.15, , pause, . - PID , - status. exit , main. - , . 7.4  , wait. : +------------------------------------------------------------+ | main() | | { | | int child; | | | | if ((child = fork()) == 0) | | { | | printf("PID %d\n",getpid()); | | pause(); /* | | */ | | } | | /* */ | | printf("PID %d\n",child); | | exit(child); | | } | +------------------------------------------------------------+ 7.15. exit pid = wait(stat_addr); pid - (PID) , stat_addr - , exit , . wait 7.16. - , , - . , , , - , exit, , wait. , - 200 exit (status) , , . , - , - - , , , . . , wait, , - , . , - wait: - . , " ", . " " - - : * ( - ) , wait, issig . issig ( 7.7) - " " "". longjump sleep, wait. wait, , - ( , ), - , , wait, +------------------------------------------------------------+ | wait | | : | | status, | | | | : | | exit | | { | | (, wait, - | | ) | | (); | | | | (;;) /* */ | | { | | (, wait, -| | , ) | | { | | ; | | -| | ;| | , - | | ; | | ( , | | exit, ); | | } | | ( ) | | ; | | , -| | ( ); | | } | | } | +------------------------------------------------------------+ 7.16. wait 201 , . * , - , . * , wait, , , - , . , , 7.17, , . , ( - , argc 1). 15 , - i, - . , wait , , - , - exit. , . , exit, , , exit 8-15 ret_code wait -. , ret_code - , 256*i, i - , ret_val . ( argc > 1), signal - " ". , , wait, , exit: - - exit, " "; , , - . +------------------------------------------------------------+ | #include | | main(argc,argv) | | int argc; | | char *argv[]; | | { | | int i,ret_val,ret_code; | | | | if (argc >= 1) | | signal(SIGCLD,SIG_IGN); /* | | */ | | for (i = 0; i < 15; i++) | | if (fork() == 0) | | { | | /* - */ | | printf("- %x\n",getpid()); | | exit(i); | | } | | ret_val = wait(&ret_code); | | printf("wait ret_val %x ret_code %x\n",ret_val,ret_code);| | } | +------------------------------------------------------------+ 7.17. wait - " " 202 , , "" ; , , , , wait , . - , " ", , wait , . wait , -1. - , - , , . UNIX exit wait " ". exit . - wait , , , , . ; - "" wait. - (init) , wait, - exit , - , . exit wait , , , , , wait. , wait, - - , - exit. , 7.18. - , , read - . , - - , , , , - - . , +------------------------------------------------------------+ | #include | | main(argc,argv) | | { | | char buf[256]; | | | | if (argc != 1) | | signal(SIGCLD,SIG_IGN); /* | | */ | | while (read(0,buf,256)) | | if (fork() == 0) | | { | | /* - | | - (buf) */ | | exit(0); | | } | | } | +------------------------------------------------------------+ 7.18. "- " 203 signal, " - ", , - , . - . 7.5  exec - , - . - , - , . : execve(filename,argv,envp) filename - , argv - - , , envp - , . - exec , execl, execv, execle .. , - main(argc,argv) , +------------------------------------------------------------+ | exec | | : (1) | | (2) | | (3) | | : | | { | | ( namei); | | , - | | ; | | , | | ; | | , , | | ; | | ( , ) | | ( detachreg);| | ( , ) | | { | | ( allocreg); | | ( attachreg); | | ( | | loadreg); | | } | | , , - | | ; | | setuid-, ; | | | | ( ); | | ( iput); | | } | +------------------------------------------------------------+ 7.19. exec 204 argv , exec. , , "=" , . - , - environ, -. 7.19 exec. namei, , , - . , , - ( ). 7.20 - , . - : 1. , , - , - " ", . 2. , , : , , , . 3. , "" (, ), - . 4. , , - , . +---------------------------+ | | | | | -| | | +---------------------------+ | | 1- | | | | +---------------------------+ | | 2- | | - | | - +---------------------------+ - | - | - | - | - +---------------------------+ - | | n- | | | | +---------------------------+ 1 | (, ) | +---------------------------+ 2 | | - +---------------------------+ - | - | - | - | - +---------------------------+ n | | +---------------------------+ | | +---------------------------+ 7.20. 205 , - . ( ), , - . , PDP 11/70 , , , - 128 64 (**), - , - 9. . , - , . , . , , , - , - , . exec , - , . . ( , - ), ( ), , ( - ). . , , , , - exec , . - , . , , , ( ). exec - , , detachreg. , - . . , - , , , . , - ( allocreg, attachreg loadreg, ). : --------------------------------------- (**) PDP 11 " " , ; - - . - , . 206 , , , ("bss"). - . "bss" ( growreg) . exec. - , - . - . - , . . , - (sp) (pc): - . setuid- , 8 11, . , iput, - , namei exec. namei iput exec , , ; - exec , . . , , ; , - . - . +-------------------------------------------------------+ | main() | | { | | int status; | | if (fork() == 0) | | execl("/bin/date","date",0); | | wait(&status); | | } | +-------------------------------------------------------+ 7.21. exec ( 7.21), -, exec. fork - - - . - - exec , "/bin/date" "date", - , exec. "/bin/date" , , , - , , , . exec, argv, (- ). , , - (***). "/bin/date" "date" , , . , , "/bin/date", 207 --------------------------------------- (***) , V (mv), (cp) (ln), , . , . - . ( "date") . exec, - - "date"; "date" , -, , exit. , - , , . - : - . , - , , . , - . - , - , , . +------------------------------------------------------------+ | #include | | main() | | { | | int i,*ip; | | extern f(),sigcatch(); | | | | ip = (int *)f; /* ip -| | f */ | | for (i = 0; i < 20; i++) | | signal(i,sigcatch); | | *ip = 1; /* f */ | | printf(" ip\n"); | | f(); | | } | | | | f() | | { | | } | | | | sigcatch(n) | | int n; | | { | | printf(" %d\n",n); | | exit(1); | | } | +------------------------------------------------------------+ 7.22. , ( 7.22), ip f - 208 . , , , , - ip - , . AT&T 3B20 SIGBUS, . , main. , , - ( ), , - f. f 1. main, f, , . SIGILL . - . , UNIX - , PDP : , . - . - . - , , - . - , . , exec - , , - , "- " . , xalloc - (. 7.23). xalloc, , - . (- allocreg), ( attachreg), ( loadreg) (read-only). , - - , . - ( , ) . xalloc , , - detachreg exit exec, . , , . - , . , allocreg ( 6.5.2) , - , , exec ( namei). - - exec ( iput), , - , 1. ( unlink), ( ). 209 , , ; +------------------------------------------------------------+ | xalloc /* | | */ | | : | | : | | { | | ( )| | ; | | ( , - | | ) | | { | | /* ... | | */ | | ; | | ( ) | | { | | /* , | | | | */ | | ; | | ; | | ( | | ); | | ; | | ; | | } | | ( attachreg);| | ; | | ; | | } | | /* -- - | | */ | | ( allocreg); /* | | - | | */| | ( "") | | ; | | , | | ( attachreg); | | ( -| | ) | | /* 9 */ | | /* -| | */ | | ( | | loadreg); | | | | "read-only"; | | ; | | } | +-----------------------------------------