5.  - . , . , - , open, read, write, lseek close, - , , creat mknod, , , - : chdir, chroot, chown, stat fstat. : pipe dup shell'; mount umount ; link unlink . - , , - , . - . : , , - , - , , , . +----------------------------------------------------------------+ +------+--------------+--------+-------+-------+---------+-------+ | - | | - | - | - | - | -| | - | | | | | | | | | namei | - | - | | -| - | | -| | | - | | -| - | | - | | | | | | | | | | | | | | | | | | | | | | | +------+--------------+--------+-------+-------+---------+-------+ | open | open stat | | | | | | | creat| creat link | creat | chown | read | | | | dup | chdir unlink| mknod | chmod | write | mount | chdir | | pipe | chroot mknod | link | stat | lseek | umount | chown | | close| chown mount | unlink | | | | | | | chmod umount| | | | | | +------+--------------+--------+-------+-------+---------+-------+ +---+--+--------------+--------+-------+-------+---------+----+--+ | | +-------------+------------------+------------------------+ | namei | | | +-------------+ ialloc ifree | alloc free bmap | | iget iput | | | +-------------+------------------+------------------------+ +---------------------------------------------------------+ | | +---------------------------------------------------------+ | getblk brelse bread breada bwrite | +---------------------------------------------------------+ 5.1. 85 5.1 - , . , , - : * , ; * , namei ; * , ialloc ifree; * , ; * , - alloc, free ; * , ; * , - . 5.1 OPEN open ( ) - , , . open: fd = open(pathname,flags,modes); pathname - , flags (, - ), modes , . open (*), . , , , - -, , - -, , , open. , - namei (. 5.2). , , - . , , . 0 , , . , , , . (- ) , ( ), . , . . --------------------------------------- (*) -1. , -1, . 86 +------------------------------------------------------------+ | open | | : | | | | ( ) | | : | | { | | ( | | namei); | | ( ) | | ( ); | | , - | | , ; | | | | , ;| | ( ) | | ( free); | | ( ); /* | | , | | namei */ | | ( ); | | } | +------------------------------------------------------------+ 5.2. , , "/etc/passwd" , , "local" (**), : fd1 = open("/etc/passwd",O_RDONLY); fd2 = open("local",O_RDWR); fd3 = open("/etc/passwd",O_WRONLY); 5.3 , . open , - - , ("/etc/passwd") . , . - "/etc/passwd" , , 3 5 (. ). - , open. , : --------------------------------------- (**) open (- ), - . - . ( "", ) . , , - . 87 - - +---------+ +------------+ +--------------+ 0| | | | | - | +---------+ | | | - | 1| | | | | - | +---------+ +------------+ | - | 2| | | - | | - | +---------+ | - | | - | 3| ----+----+ | - | | - | +---------+ | | - | +--------------+ 4| ----+---+| | - | +---->| - | +---------+ || | - | |+--->| (/etc/ | 5| ----+--+|| +------------+ || | 2 passwd)| +---------+ ||| | - | || +--------------+ 6| | ||+-->| +--+| | - | +---------+ || | 1 | | | - | 7| | || +------------+ | | - | +---------+ || | - | | | - | | - | || | - | | | - | +---------+ || +------------+ | | - | || | - -| | | - | |+--->| -+---|-+ | - | | | 1 | | | | - | | +------------+ | | | - | | | - | | | +--------------+ | | - | | | | - | | | - | | +->| (local)| | | - | | | 1 | | | - | | +--------------+ | +------------+ | | - | | | - | | | - | +---->| +---+ | - | | 1 | | - | +------------+ | - | | - | | - | | - | | - | +------------+ +--------------+ 5.3. fd1 = open("/etc/passwd",O_RDONLY); fd2 = open("private",O_RDONLY); 5.4 - , ( ) . - open , - , - . - , - . - 88 - - ( A) +---------+ +------------+ +--------------+ 0| | | | | - | +---------+ | | | - | 1| | | | | - | +---------+ +------------+ | - | 2| | | - | | - | +---------+ | - | | - | 3| ----+----+ | - | | - | +---------+ | | - | +--------------+ 4| ----+---+| | - | +---->| - | +---------+ || | - | |+--->| (/etc/ | 5| ----+--+|| +------------+ ||+-->| 3 passwd)| +---------+ ||| | - | ||| +--------------+ | - | ||+-->| +--+|| | - | | - | || | 1 | || | - | | - | || +------------+ || | - | +---------+ || | - | || | - | || | - | || | - | ( B) || | - | || | - | +---------+ || | - | || | - | 0| | || +------------+ || | - | +---------+ || | - -| || | - | 1| | |+--->| -+---||+ | - | +---------+ | | 1 | ||| | - | 2| | | +------------+ ||| | - | +---------+ | | - | ||| +--------------+ 3| ----+--|--+ | - | ||| | - | +---------+ | | | - | ||+->| (local)| 4| ----+-+| | | - | || | 1 | +---------+ || | | - | || +--------------+ 5| | || | | - | || | - | +---------+ || | +------------+ || | - | | - | || | | - | || | - | | - | || +->| +---+| | - | | - | || | 1 | | | - | +---------+ || +------------+ | | - | || | - | | | - | || | - | | +--------------+ || | - | | | - | || +------------+ |+->| (private)| || | - | || | 1 | |+---->| +----+| +--------------+ | | 1 | | | - | | +------------+ | | - | | | - | | +--------------+ | | - | | | +------------+ | | | - | | +----->| +-----+ | 1 | +------------+ 5.4. , 89 " ". , , - , , - (. [Thompson 78], .1943). - dup fork, 5.13 7.1, . (0, 1 2) - : , . UNIX - , - . - , . , , - 4, 6 11, , 0 ( ). - , , 7. (. 10) , . 5.2 READ read (): number = read(fd,buffer,count) fd - , open, buffer - - , read, count - - , , number - - . 5.5 read, . , , (. 5.3). - ( 5.6), . , - "", - , , - , - , , , , ( ), - , -. , - - , , , , . , , - . , - 90 +------------------------------------------------------------+ | read | | : | | - | | | | , - | | | | : , -| | | | { | | -| | ; | | ; | | , | | , , | | - ; | | ; | | ; | | | | | | ; | | ( -| | ) | | { | | | | ( bmap); | | , | | ; | | ( 0) | | /* */ | | ; /* */ | | ( breada, | | , bread - - | | ); | | | | ; | | - | | , | | , - | | ; | | ; /* | | bread */ | | } | | ; | | | | ; | | ( ); | | } | +------------------------------------------------------------+ 5.5. +------------------------------------------------------+ | mode | | count | | offset | | address , ,| | | | flag | | | +------------------------------------------------------+ 5.6. -, 91 bmap, , - -, , . , , ( bread breada) - . - - , - - , , , , . - , , - , , , - - . , - , , . - ; - . lseek ( 5.6) , . +------------------------------------------------------+ | #include | | main() | | { | | int fd; | | char lilbuf[20],bigbuf[1024]; | | | | fd = open("/etc/passwd",O_RDONLY); | | read(fd,lilbuf,20); | | read(fd,bigbuf,1024); | | read(fd,lilbuf,20); | | } | +------------------------------------------------------+ 5.7. , 5.7. open - , fd - read. read, - , " ", . , (: lilbuf, 20 0), - . , - , , - . , , 1024 , lilbuf - 20 . 20 0. read - , 20, , 20 , - , , .. 20. read - , 92 , , - , . bigbuf, , - (1024), (20), . , , . read , , . , , 1004 1024 . 1004 - bigbuf - , 1024, 1004 bigbuf - 20 , . , read. (1024) (1), - , - , , - . , . , 20 - . , 1044, , - . read , , , 1044, , . , - , - , . - read , , , . - , ; - , , - , - - (. 5.4). , , : , - , - , , . - , , , . , - breada. , , - . 4.9, , - , . - , , - read, . , - , , . , - 93 (. 5.1). read, . , , . - , , read . , ; - , , . , - read , , - , . , , . , - . , , , , , - . "/etc/ passwd", - , - , (, , ) - . - , , - . - read, , , - . , , , +------------------------------------------------------------+ | #include | | /* A */ | | main() | | { | | int fd; | | char buf[512]; | | fd = open("/etc/passwd",O_RDONLY); | | read(fd,buf,sizeof(buf)); /* 1 */ | | read(fd,buf,sizeof(buf)); /* 2 */ | | } | | | | /* B */ | | main() | | { | | int fd,i; | | char buf[512]; | | for (i = 0; i < sizeof(buf); i++) | | buf[i] = 'a'; | | fd = open("/etc/passwd",O_WRONLY); | | write(fd,buf,sizeof(buf)); /* 1 */ | | write(fd,buf,sizeof(buf)); /* 2 */ | | } | +------------------------------------------------------------+ 5.8. , 94 ( 5.8). , - open , read write, - : 1, 2, 1, 2, 1, 1, 2, 2, 1, 1, 2, 2 .. - , A, , , ; - , , . ( 5.4) - . , 5.9 , - , - . , - , , buf1 buf2 - , "/etc/passwd". 5.3 WRI write (): number = write(fd,buffer,count); fd, buffer, count number , - read. - . , , - , , , alloc, - . - , , , - +------------------------------------------------------------+ | #include | | main() | | { | | int fd1,fd2; | | char buf1[512],buf2[512]; | | | | fd1 = open("/etc/passwd",O_RDONLY); | | fd2 = open("/etc/passwd",O_RDONLY); | | read(fd1,buf1,sizeof(buf1)); | | read(fd2,buf2,sizeof(buf2)); | | } | +------------------------------------------------------------+ 5.9. . write, , ; - , , . , , - 95 . , , 10240, . bmap, , , - . - , . -