, , - , , , . , , - . 5.34 , V, () . , , , . , - ; , UNIX "", - , . , , . - , (. 5.34). : , , - ( namei iget), - ( iput), , , - ( ), - . 13 . 5.18  . - , , , , : - , . fsck - . ( 10) . , fsck. . , - . - , . , . , - , . - , , - , , - , . , , - 130 . , - , - , . , , - , , , - . , - . , , . , . , - (), - . - , , , . , unlink . (, ), - . , - , . , , , , , . , - , , . - , , , - . , , , - , - . , . , - , , - . - . 5.19  , . - , : , . - , , . , UNIX . , fsck, . 5.20  1. , 5.35. - read ? - 131 , read. 2. 5.35 , lseek(fd,9000L,0); read. ? 3. , , - , . , - , . , , ? 4. - - - , . - fread fwrite ? - fopen fclose ? +------------------------------------------------------------+ | #include | | main() | | { | | int fd; | | char buf[1024]; | | fd = creat("junk",0666); | | lseek(fd,2000L,2); /* 2000 */ | | write(fd,"hello",5); | | close(fd); | | | | fd = open("junk",O_RDONLY); | | read(fd,buf,1024); /* */ | | read(fd,buf,1024); /* , 0 */| | read(fd,buf,1024); | | } | +------------------------------------------------------------+ 5.35. 5. , , , , . , ? +---------------------------------------------------------+ | #include | | main() | | { | | int fd; | | char buf[256]; | | | | fd = open("/etc/passwd",O_RDONLY); | | if (read(fd,buf,1024) < 0) | | printf(" \n"); | | } | +---------------------------------------------------------+ 5.36. 132 6. , 5.36. ? . , buf 1024 ? - , ? *7. BSD - : * , ; * , ; * ; * , ; * , - ; * write. , . , ? , - ? close , - write ? *8. , 4 . , , , , . , . *9. V fcntl : fcntl(fd,cmd,arg); fd - , cmd - , arg , ( ) (. ). * , , - - , * , * - . , , . , . , - . - ? *10. - , : A - "one" "two", B "two" "one". , - . , . - ? 11. - - 133 , . , - , ? - ? 12. (. [Ritchie 81]), , , , , - . , , . 13. , - . , - , - . . +----------------------------------------------------------+ | main(argc,argv) | | int argc; | | char *argv[]; | | { | | if (argc != 2) | | { | | printf(": \n"); | | exit(); | | } | | | | /* : , - | | */ | | /* - | | */ | | if (mknod(argv[1],040777,0) == -1) | | printf("mknod \n"); | | } | +----------------------------------------------------------+ 5.37. , *14. ( 5.37), - ( "." ".."). , , , - ls -l, ls -ld, cd. ? 15. , , , , , - . () , - - . 16. , , . , - ls, "-i" ? , "-l" ? . , , - , . 17. , , : * . * "" . * 134 , . *18. , , . ? 19. , 5.38, , . (: pwd, ). 20. , - , , . 21. chroot ? - , - . ? 22. ( 5.19), - "hello" +----------------------------------------------------------+ | main(argc,argv) | | int argc; | | char *argv[]; | | { | | if (argc != 2) | | { | | printf(" 1 - \n"); | | exit(); | | } | | | | if (chdir(argv[1]) == -1) | | printf("%s \n",argv[1]);| | } | +----------------------------------------------------------+ 5.38. chdir . , 1024 6 ( - 6) ? , read write , ? 23. , ( 5.19), mknod , ? ? , ? ? 24. , , . ? open, , - , read ? 25. dup2 ( 7), : dup2(oldfd,newfd); oldfd - , newfd ? , newfd - ? *26. - ? 135 27. , "/mnt/a/b/c", "/mnt" . mount ? - , pwd ? - mount , "/mnt" 1. . 28. ".." , - : - , - "..". ? , - , - . 29. , . - write, creat, link, unlink, chown chmod ? - ? *30. , , - . umount creat . . *31. umount , , , , mount iget, . umount - ? : * umount iput . ( - , ?) * umount , - , . ( ?) 32. , ls -ld 1. ? 33. mkdir ( ) ? ( - : "." ".." ?) *34. " " link , . ; , . . *35. , unlink("."); ? , - . 36. , , , . , - , , - , . , - , - . 37. , - 1. 38. , : , , ? 136