221 . , 1, - , - . - : 1. 1 , "- " . , - , , 1 - . , 1, - exec, "/etc/init". 1 init, - . , , - exec, 1 ? - , - , - , exec , . , init, - exec - . init ( 7.31) , - , - . , , - init "/etc/inittab". "id" ( , - . .), (. 7.43) , (. 7.32). init - , - , , , , . , - ( 2) init - getty-, , . , getty-, login, - shell (. 10). init - (wait), , "" , "" - . UNIX , - , . - , . , , , - , - .. init , , . , - . . (, vhand), . , , , . - , , . , 222 +------------------------------------------------------------+ | init /* init, | | " 1" */ | | : | | : | | { | | fd = open("/etc/inittab",O_RDONLY); | | while (line_read(fd,buffer)) | | { | | /* */ | | if (invoked state != buffer state) | | continue; /* while */ | | /* | | */ | | if (fork() == 0) | | { | | execl(" "); | | exit(); | | } | | /* init */ | | /* while */ | | } | | | | while ((id = wait((int*) 0)) != -1) | | { | | /* ; | | * , - | | * */ | | /* , - | | * */ | | } | | } | +------------------------------------------------------------+ 7.31. init +------------------------------------------------------------+ | : , , , | | | | | | '#' | | | | co::respawn:/etc/getty console console # | | 46:2:respawn:/etc/getty -t 60 tty46 4800H # | +------------------------------------------------------------+ 7.32. inittab , , , - , . 7.10  , . - fork , - , . - fork , - 223 , , , . - exit, - " ". - -, wait. exec - , - . , , - . "sticky-bit" - , . - , , setuid setuid-. brk . signal - . - - - . , kill, , , setpgrp. shell init , , - . Shell - , - , , , - , . init , , - . - , init - , "/etc/inittab". 7.11  1. , 7.33. - - . +------------------------------------+ | main() | | { | | printf("hello\n"); | | if (fork() == 0) | | printf("world\n"); | | } | +------------------------------------+ 7.33. , fork - 2. , 7.34, 7.4. 3. , 7.5 - , , - . , , - ? 4. , - 224 ? ( , - .) ? 5. kill. 6. 7.35 " - " . ? 7. , , . - "core" , , . . - , "core". , - ? , ? 8. ( 7.12), , , . , , : +------------------------------------------------------------+ | #include | | int fdrd,fdwt; | | char c; | | | | main(argc,argv) | | int argc; | | char *argv[]; | | { | | if (argc != 3) | | exit(1); | | fork(); | | | | if ((fdrd = open(argv[1],O_RDONLY)) == -1) | | exit(1); | | if (((fdwt = creat(argv[2],0666)) == -1) && | | ((fdwt = open(argv[2],O_WRONLY)) == -1)) | | exit(1); | | rdwrt(); | | } | | rdwrt() | | { | | for (;;) | | { | | if (read(fdrd,&c,1) != 1) | | return; | | write(fdwt,&c,1); | | } | | } | +------------------------------------------------------------+ 7.34. , - - * , - ; 225 * , - signal . 9. , , . , - ? BSD. *10. , , longjump. ; , V , - ( ). BSD - . ? +------------------------------------------------------------+ | #include | | main() | | { | | extern catcher(); | | | | signal(SIGCLD,catcher); | | if (fork() == 0) | | exit(); | | /* */ | | pause(); | | } | | | | catcher() | | { | | printf("- \n"); | | signal(SIGCLD,catcher); | | } | +------------------------------------------------------------+ 7.35. , " " 11. mkdir mknod, - link, - "." ".." . . , mkdir - ? SIGKILL, ? mkdir. 12. - ( - , ), - . ? *13. , . - 226 . (, "break".) , ? *14. , - , . - , - () . ? , ( - ) ( - ), ? 15. newpgrp(pid,ngrp); pid - ngrp ( ). , . 16. : wait - . 17. nowait(pid); pid - , , . , , - , . ? " ". 18. - (startup), main, - . - exit, startup main. , startup exit (- - ) ? 19. , wait, exit ? , - - exit() exit(n). exit , , wait ? . 20. , , , - 7.36 exec . , ? +----------------------------------+ | main(argc,argv) | | int argc; | | char *argv[]; | | { | | execl(argv[0],argv[0],0); | | } | +----------------------------------+ 7.36 21. exec ( - ) . , 7.37 ? , "a.out" - 227 , , - 7.36 ? 22. , "read-only" ( ), , , . . (: " - ".) ? - ? 23. open, chmod, unlink unmount , "sticky-bit" ? , , , ? 24. , "/etc/passwd", . passwd , . ? +-----------------------------------------------------+ | main() | | { | | if (fork() == 0) | | { | | execl("a.out",0); | | printf(" exec\n");| | } | | } | +-----------------------------------------------------+ 7.37 *25. , , setuid- . 26. , "a. out" - : +-----------------------------------------------------+ | main() | | { | | char *endpt; | | char *sbrk(); | | int brk(); | | | | endpt = sbrk(0); | | printf("endpt = %ud sbrk\n", (int) endpt); | | | | while (endpt--) | | { | | if (brk(endpt) == -1) | | { | | printf("brk %ud | | \n",endpt); | | exit(); | | } | | } | | } | +-----------------------------------------------------+ 7.38 228 chmod 4777 a.out chown root a.out chmod "" setuid (4 4777); "root" . - ? 27. , - 7.38 ? . 28. malloc brk, free , malloc. : ptr = malloc(size); free(ptr); size - , , ptr - - . free, ptr malloc. - . 29. , - 7.39 ? - , . +-----------------------------------------------------+ | main() | | { | | int i; | | char *cp; | | extern char *sbrk(); | | | | cp = sbrk(10); | | for (i = 0; i < 10; i++) | | *cp++ = 'a' + i; | | sbrk(-10); | | cp = sbrk(10); | | for (i = 0; i < 10; i++) | | printf("char %d = '%c'\n",i,*cp++); | | } | +-----------------------------------------------------+ 7.39. , sbrk 30. shell , - , ? , , - shell ? ? 31. shell ">>" - , , : run >> outfile "outfile" ( ) . - , . 32. shell , exit, "", - "" ( ). , , 7.40, "truth". , , shell - : while truth 229 +------------------+ | main() | | { | | exit(0); | | } | +------------------+ 7.40 do truth & done 33. 7.29: - - ? 34. shell . , , . 35. PATH , shell' . execlp execvp PATH , , "/". . *36. , shell - , PATH. , shell ? 37. shell cd ( ) ? shell : cd pathname & ? 38. "delete" "break", , shell', . - , shell', . shell ( 7.28) ? 39. nohup command_line "" - (quit) , (command_line). shell ? 40. shell: nroff -mm bigfile1 > big1out & nroff -mm bigfile2 > big2out shell ( 7.28). , nroff - ? shell ? 41. shell' - : "Bus error - core dumped" ( - ). , - ; shell , ? 42. 1 init. - , init, - . , , , . , init 230 2 ( ), init 2 . shell init. , - init ? 43. "/etc/inittab" , - . , getty- "respawn" (), , init getty-, - . , init getty-, . init ? 44. . , : , , , . , , , ( ). - ? - 231