. , read, , . - , . , - , , , , , - . , ( 3.4) , , , , - . , , - , ( 5.12). , . , , - , "/tmp" . . 5.4  UNIX, , - , . , - , , " , - , " (. [Ritchie 81]). , UNIX , , V - . - , - , - , - - ( ). 5.9 . 5.5 , - - LSEE read write - , - lseek , -, . : position = lseek(fd,offset,reference); fd - , , offset - - , reference , offset , - - . , position, , 96 . , - , 5.10, , , lseek, - , 1023 ( reference, 1), . , 1024- . reference 0, , 2, . lseek , , - - . , lseek, ; - read write . 5.6 CLOS , - . close (): +--------------------------------------------------------+ | #include | | main(argc,argv) | | int argc; | | char *argv[]; | | { | | int fd,skval; | | char c; | | | | if(argc != 2) | | exit(); | | fd = open(argv[1],O_RDONLY); | | if (fd == -1) | | exit(); | | while ((skval = read(fd,&c,1)) == 1) | | { | | printf("char %c\n",c); | | skval = lseek(fd,1023L,1); | | printf("new seek val %d\n",skval); | | } | | } | +--------------------------------------------------------+ 5.10. , lseek close(fd); fd - . , - . , , 1, , dup fork, , - , ; - . , 1, , open ( iput). , - , ; - , 0. close , 97 . - , - - . , , , . , - . 5.11, , , - 5.4, , - . , 3 4 - +---------+ +------------+ +--------------+ 0| | | | | - | +---------+ | | | - | 1| | | | | - | +---------+ +------------+ | - | 2| | | - | | - | +---------+ | - | | - | 3| ----+----+ | - | | - | +---------+ | | - | +--------------+ 4| ----+---+| | - | +---->| - | +---------+ || | - | | | (/etc/ | 5| ----+--+|| +------------+ | +-->| 2 passwd)| +---------+ ||| | - | | | +--------------+ | - | ||+-->| +--+ | | - | | - | || | 1 | | | - | | - | || +------------+ | | - | +---------+ || | - | | | - | || | - | | | - | +---------+ || | - | | | - | 0| | || +------------+ | | - | +---------+ || | - | | | - | 1| | |+--->| +----|+ | - | +---------+ | | 1 | || | - | 2| | | +------------+ || | - | +---------+ | | - | || +--------------+ 3| NULL | | | - | || | - | +---------+ | | - | |+->| (local)| 4| NULL | | | - | | | 1 | +---------+ | | - | | +--------------+ 5| | | | - | | | - | +---------+ | +------------+ | | - | | - | | | 0 | | | - | +---------+ | +------------+ | | - | | | - | | +--------------+ | | - | | | - | | +------------+ | | (private)| | | 1 | | | 0 | +---->| +----+ +--------------+ +------------+ | - | | - | +--------------+ | - | +------------+ | 0 | +------------+ 5.11. 98 , . 0, . "/etc/passwd" "private" . "private" , 0, . - "private", , , 4.1.2. 5.7  open , creat . - creat: fd = creat(pathname,modes); pathname, modes fd , open. , ; , ( +------------------------------------------------------------+ | creat | | : | | | | : | | { | | ( namei);| | ( ) | | { | | ( ) | | { | | ( iput); | | (); | | } | | } | | /* */ | | { | | (- | | ialloc); | | : | | | | ; | | } | | , -| | ; | | ( ) | | ( free); | | ( ); | | ( ); | | } | +------------------------------------------------------------+ 5.12. 99 0) (***). 5.12 . , - namei , . , , , namei . , - . , - . - ; - . , . . creat, , . , , , ialloc ( 4.6). - , . , , . . ( bwrite), - . - , , , , - . , , , - , ( . 5.16.1). creat, - . , "" , creat: , - free, . - , : , . , , , . creat , , open. , , , . --------------------------------------- (***) open , O_CREAT () O_TRUNC (). O_CREAT , . , , O_TRUNC. 100 5.8  mknod , - , . - creat , . - mknod: mknod(pathname,type and permissions,dev) pathname - , type and permissions - (, ) - , dev - ( 10). 5.13 , mknod . +------------------------------------------------------------+ | | | : ( ) | | | | | | , | | ( | | ) | | : | | { | | ( | | ) | | (); | | , | | ( namei); | | ( ) | | { | | ( iput); | | (); | | } | | | | ( ialloc); | | : | | ; | | ( | | iput); | | ( -| | ) | | -| | ; | | ( iput); | | } | +------------------------------------------------------------+ 5.13. , . , - - . , - , , . , , . mknod , - , ( 101 "." ".."). 5.33 - , . +------------------------------------------------------------+ | | | : | | : | | { | | ( | | namei); | | ( - | | ) | | { | | ( iput); | | (); | | } | | ; | | ( | | iput); | | | | ; | | } | +------------------------------------------------------------+ 5.14. 5.9  , - . iget, - . fork , , , , . chdir ( 5.14) - . chdir: chdir(pathname); pathname - , . - , namei, , - . , , ( iput), - , . namei - , . chdir , , 1, 0. chdir - open, . , - chdir, , . 102 , "/". - , , - iget . chroot. , . - : chroot(pathname); pathname - , . chroot, - , . , . , , - , , - chroot. ( ) , namei, ("/"), ".." , - . - , . 5.10  () , , . : chown(pathname,owner,group) chmod(pathname,mode) , , , namei. ( , ). , (. - 7.5) iput. - "" . , , , , , . 5.11 STAT FST stat fstat - : , , , , , . - : stat(pathname,statbuffer); fstat(fd,statbuffer); pathname - , fd - , open, statbuffer - , 103 . statbuffer. 5.33 stat fstat. - - - - - - - - - - - A - - | - - +-------------+--------------+ - - | | - B C | +-------+-------+ | | D - E - - - - - - - - - - 5.15. 5.12  - (" - "), . , , - . . : , - , , , . open, - pipe - . , - , read, write close. , , pipe, - . (. 5.15), B D E, - , A C. , - , . - , . 5.12.1 pip : pipe(fdptr); 104 fdptr - , . , , - . - : - , . , read, write . . - , . +------------------------------------------------------------+ | pipe | | : | | : | | | | { | | ( | | ialloc); | | , -| | ; | | , | | ; | | -| | , - , | | , | | ; | | 2; | | , -| | , , , 1;| | } | +------------------------------------------------------------+ 5.16. () 5.16 . , "- ", ialloc. - , . . , - . , - , "" - . , ( 1), , "" ( 2 - - ). , , . (" - "); , . lseek . 105 5.12.2  - , , , , . - , , , , - , . - ( unlink), - : , . . , , , . , - , , , . , ; . , - , , , - (). , , , , . - "no delay", open , . , - . , . 5.12.3  , , . , - ; , , , . - , , , , ; , 1, - . , : - , , write. +--------------------+--------------------+ | | | +----------+---------+----------+---------+ | +----------------+ +-- | --------------+ v v +---+---+---+---+---+---+---+---+---+---+ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | +---+---+---+---+---+---+---+---+---+---+ 5.17. 106 , , - , . , - " - " ( 5.17). - : , - ; , ; , ; , . , , - : , , . - , , - write, - . : - , . , , ( , 0). ; - 0, , - . - , ( ) , , write. , - . , , . , , , - . , , - .