- . " ", - , "" . , , ( 7), - , . , - , , . - , - mount, - 118 +------------------------------------------------------------+ | umount | | : , - | | | | : | | { | | ( ) | | (); | | ( namei); | | -| | ; | | - | | , ; | | ( iput); | | - | | , | | ; /* 7 */ | | , , | | ; | | (- - | | ) | | (); | | -| | ; | | ; | | ( iput); /* iget | | */ | | - | | ; | | () | | ; | | - | | ; | | ; | | , " - | | "; | | ( iput); /* iget | | */ | | , ; | | , ;| | } | +------------------------------------------------------------+ 5.27. , . - , - ; . , , . , , " ", mount, - . - , umount . 119 / | usr +------------+-------------+ | | src include | +----+----+ uts sys realfile.h | - - sys -------------------- - +-------+-------+ - inode.h testfile.h ------------------ 5.28. , link 5.15 LIN link - , - . link: link(source file name, target file name); source file name - , target file name - () , link. , , . , , - . , : link("/usr/src/uts/sys","/usr/include/sys"); link("/usr/include/realfile.h","/usr/src/uts/sys/testfile.h"); : "/usr/src/uts/sys/testfile.h", "/usr/include/sys/testfile.h" "/usr/include/realfile" (. 5.28). ( ) , - , . - , , - , , , , . , - . - , - mkdir, - . mkdir . 5.29 link. , namei, , - , ( ) . ; , link - , . - , , - 120 +------------------------------------------------------------+ | link | | : | | | | : | | { | | ( | | namei); | | ( | | ) | | { | | ( iput); | | (); | | } | | ; | | ; | | ; | | -| | ( namei); | | ( - | | - | | ) | | { | | , ; | | (); | | } | | - | | : | | - | | ; | | ( | | iput); | | ( iput); | | } | +------------------------------------------------------------+ 5.29. iput. , - . , , : , 1 , , , . , () - , . link , , 0, - , 2. , , : link("source","/dir/target"); "source", , , 74, . "dir", - "target", "dir" "target" 74. "source" iput. - "source" 1, - 121 2. , , - . , - , : A: link("a/b/c/d","e/f/g"); B: link("e/f","a/b/c/d/ee"); ( ). , A "a/b/c/d" , B "e/f". " " , , . ( 5.30 .) A - "e/f", , "f" . B "a/b/c/d" - "d". A , B, B, , - , A. - , - . () , - . , , . - . : link("a/b/c","a/b/c/d"); "c"; - , , "c" "d". , , - ( ), ? , , ( 7). , . , , - , . - , , ( - ), . , - "/bin" "/usr/bin" ( ) "/bin/sh" ( shell), - . 5.16 UNLIN unlink . - unlink: unlink(pathname); pathname , . - unlink, , 122 A B +------------------------------------------------------------- | - | - "e" | - - | - "e" | "a" - | "a" - | "b" - | "b" - | "c" - | "c" - | "d" - | - | - | "e" - | - - | "e" - | - - | +-----------------------------------------------+ | | - "e" | | | | | +-----------------------------------------------+ | - "e" | - "e" | - "f" | - "a" | - "a" | - - | - - | - | - "d" | - - | - "d" | - A | - | "e" | "e" | | "f" | - | "f" | B | +-------------------------------+ | | ( ) | v +-------------------------------+ 5.30. link . , : unlink("myfile"); fd = open("myfile",O_RDONLY); open , - myfile. - 123 , - . , , - . 5.31 unlink. namei, - . - , iget. ( , ".", ). ( ) ( 7) : 0 , . - , , , iput - . iput , 0, , . . +------------------------------------------------------------+ | unlink | | : | | : | | { | | | | ( namei); | | /* ... */| | ( ".") | | ; | | | | (-| | iget); | | ( , - | | ) | | { | | ( iput); | | (); | | } | | ( | | 1) | | ; | | : -| | ; | | ( | | iput); | | ; | | ( iput); | | /* iput , 0, | | * , | | * ( free) | | * ( ifree); | | */ | | } | +------------------------------------------------------------+ 5.31. 124 , , - , ( - free). , , , - , . 0. , - , , ifree. , , ; - . 5.16.1  , . , , - - , - . , , : , 1 - , , . , - ( ) . , , , . , , . , , , (. 5.18). , , "a" "b", - "a" - unlink. , , - "a", . , "b" 2, "a" , . "b", , - , . , - : , "b", 1, , "a". "a" "b" - , , , 1. unlink "a", 0, , "b" . creat, - , 1, . , (fsck, 5.18), - . , , . , , 125 . , , , , - , - . , , - , . - , . - , . . - fsck, . - , , , - - . , , - , - . fsck , , . 5.16.2  unlink , . rmdir , , ( - - ). rmdir , - ; - read unlink. , , rmdir - , , creat. . , unlink, - , . link , . , - ; - , - . , . , unlink - link. , - namei, , . , A "a/ b/c/d" - "c". - , (. iget bread). B "c", , A. B A. A , B - , "c" . , A , . namei, , . , , , - , - , "c". 126 A , , (. 5.32). , ; , , - - A B C +------------------------------------------------------------ | - - - | - "" - | - - | - , - | - "c" - | - - | - - | - - | - - - | - - - | "b" - - - | "c" - - | - - - | "c" - - | , - - | "c" - - | - - | - - | - - | - - - | - - - | - , "c" - | - - | - - - | - "c", - - | - , - | - =0 - | - - - | - - | - - "n" | - - - | - - , - | - - | - - "c" | - - | - - | - - | - - "n" | - - | - - | , - - | "c" ( - | "n") - | "n" - | - - | "n" - - | "d" - v 5.32. - unlink 127 +------------------------------------------------------------+ | #include | | #include | | #include | | | | main(argc,argv) | | int argc; | | char *argv[]; | | { | | int fd; | | char buf[1024]; | | struct stat statbuf; | | | | if (argc != 2) /* */ | | exit(); | | fd = open(argv[1],O_RDONLY); | | if (fd == -1) /* open | | */ | | exit(); | | if (unlink(argv[1]) == -1) /* | | */ | | exit(); | | if (stat(argv[1],&statbuf) == -1) /* - | | */ | | printf("stat %s \n",argv[1]);| | /* */ | | else | | printf("stat %s !\n",argv[1]);| | if (fstat(fd,&statbuf) == -1) /* | | */ | | printf("fstat %s !\n",argv[1]);| | else | | printf("fstat %s \n",argv[1]);| | /* */ | | while (read(fd,buf,sizeof(buf)) > 0) /* - | | */ | | printf("%1024s",buf); /* | | 1 */ | | } | +------------------------------------------------------------+ 5.33. - . , - , . ( , , , ). open, unlink . unlink , , . - . , open , - iput - unlink. , , , . , iput 0, . , , , , 128 unlink , unlink, , , . - , . 5.33 , , . - stat , unlink (- , ), fstat , . , 1024 . , : - . ; - , . - - , . 5.17  " " (. [Killian 84]) V . , ( 13) - . - UNIX, , . V +---------------+ +------+ +-------+ V | open | +-----+- -+-------->| | | close | | +------+ +-------+ | read | | +---+- -+---+ | | | write |<---+ | +------+ | +-------+ | - |<-----|---+- -+---|---->| | | - | | +------+ | +-------+ | - | | | | | | - | | - | | +------+ | | - | +---------------+ | | | | | - | | ropen | | +------+ | +-------+ | rclose | | | - | | | rread | | | - | | - | rwrite |<-----+ | - | | | - | | - | | +-------+ | - | | - | | | | | - | | - | | +-------+ | - | | - | +---->| | +---------------+ | - | +-------+ | - | | - | | | | - | | - | +-------+ | - | | - | | | | - | | - | +-------+ | - | | - | | - | +---------------+ +------+ +-------+ 5.34. 129 - . , , - , , . ,