-
.
,
. , -
, 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 ;
* , ;
* , -
.
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) ,
.
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".
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, ,
, -
. -
, . -
.
, read, ,
. -
, .
, -
, , ,
, , -
.
, ( 3.4)
, ,
, , -
. , , -
, (
5.12). ,
. , , -
,
"/tmp" .
.
UNIX, , -
,
. , -
, , " , -
, "
(. [Ritchie 81]). , UNIX
, , V -
. -
, -
, - , -
- (
). 5.9
.
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
.
, -
. 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.
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
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.
, -
.
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, ("/"),
".."
, -
. -
, .
() ,
, .
:
chown(pathname,owner,group)
chmod(pathname,mode)
, ,
, namei.
(
, ).
, (. -
7.5) iput. -
"" . ,
, , ,
, .
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.
-
(" - "),
.
, , -
.
. : , -
, ,
, .
open, -
pipe - . ,
-
, read, write close. ,
, pipe, -
. (. 5.15),
B D E, -
, A C. ,
-
, . -
, .
:
pipe(fdptr);
104
fdptr - ,
.
, , -
. -
: -
, .
, read, write .
. -
, .
+------------------------------------------------------------+
| pipe |
| : |
| : |
| |
| { |
| ( |
| ialloc); |
| , -|
| ; |
| , |
| ; |
| -|
| , - , |
| , |
| ; |
| 2; |
| , -|
| , , , 1;|
| } |
+------------------------------------------------------------+
5.16. ()
5.16 .
, "-
", ialloc. -
,
.
. , -
.
, -
, "" -
.
,
( 1), ,
"" ( 2 - -
). ,
, .
(" -
"); ,
.
lseek
.
105
- , ,
, ,
. -
, , , ,
-
, . -
(
unlink), -
: ,
.
. ,
, ,
. , -
, ,
, .
, ;
. , -
,
, , -
().
, ,
, , . -
"no delay", open
, .
, -
. ,
.
,
, .
, -
; , ,
, . -
, , ,
, ;
, 1, -
.
, : -
, ,
write.
+--------------------+--------------------+
| | |
+----------+---------+----------+---------+
| +----------------+
+-- | --------------+
v v
+---+---+---+---+---+---+---+---+---+---+
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+---+---+---+---+---+---+---+---+---+---+
5.17.
106
,
, -
,
.
, -
" - " (
5.17).
- : , -
; ,
; ,
; , .
, , -
:
, , . -
, , -
write,
-
. : -
,
.
,
, (
, 0). ; -
0, , -
. -
, ( )
, ,
write.
, -
.
, ,
. ,
, , -
. , , -
.
-
, .
read , -
-
( ).
,
, read , ,
, -
. , , -
- , -
, ,
. , ,
"no delay" ( ), read
, .
,
( 10),
, .
,
,
.
, , -
, -
, .
107
, ,
( , , -
); , -
, ,
. , , -
,
,
.
, ,
, ,
, -
.
, -
: ,
,
open. , -
-
. , ,
,
,
. ,
, , -
: ,
, .
.te1 5.12.4
,
, , , -
, . -
-
. -
0 ,
, -
- .
0
, , -
( 7) -
. -
, , - -
. , -
, -
, , . ,
,
, ,
. -
, -
, ,
. ,
.
5.18 -
. ,
108
+---------------------------------+
| char string[] = "hello"; |
| main() |
| { |
| char buf[1024]; |
| char *cp1,*cp2; |
| int fds[2]; |
| |
| cp1 = string; |
| cp2 = buf; |
| while(*cp1) |
| *cp2++ = *cp1++; |
| pipe(fds); |
| for (;;) |
| { |
| write(fds[1],buf,6); |
| read(fds[0],buf,6); |
| } |
| } |
+---------------------------------+
5.18.
"hello" .
, , , ,
, - .
, , 5.19, -
"fifo". -
() , -
+------------------------------------------------------------+
| #include |
| char string[] = "hello"; |
| main(argc,argv) |
| int argc; |
| char *argv[]; |
| { |
| int fd; |
| char buf[256]; |
| |
| /* |
| */ |
| mknod("fifo",010777,0); |
| if(argc == 2) |
| fd = open("fifo",O_WRONLY); |
| else |
| fd = open("fifo",O_RDONLY); |
| for (;;) |
| if(argc == 2) |
| write(fd,string,6); |
| else |
| read(fd,buf,6); |
| } |
+------------------------------------------------------------+
5.19.
109
"hello"; -
, . -
,
"fifo",
.
( ).
dup
,
. . :
newfd = dup(fd);
fd - , , newfd - ,
. dup ,
- -
-
-
+---------+ +------------+ +--------------+
0| ----+----+ | | | - |
+---------+ | | | | - |
1| ----+---++-->| | | - |
+---------+ | +------------+ | - |
2| ----+--++--->| - | | - |
+---------+ +---->| - | | - |
3| ----+----+ | - | | - |
+---------+ | | - | +--------------+
4| ----+---+| | - | +---->| - |
+---------+ || | - | | | (/etc/ |
5| ----+--+|| +------------+ | +-->| 2 passwd)|
+---------+ ||| | - | | | +--------------+
6| ----++ ||+-->| +--+ | | - |
+---------++-||--->| 2 | | | - |
7| | || +------------+ | | - |
+---------+ || | - | | | - |
| - | || +------------+ | | - |
+---------+ || | | | | - |
|+--->| 1 +----|+ | - |
| +------------+ || | - |
| | - | || +--------------+
| | - | || | - |
| | - | |+->| (local)|
| | - | | | 1 |
| | - | | +--------------+
| +------------+ | | - |
| | | | | - |
+---->| 1 +----+ | - |
+------------+ | - |
| - | +--------------+
| - |
+------------+
5.20. dup
110
,
, . ,
, 5.20, , -
: (open)
"/etc/passwd" ( 3), "local"
( 4), "/etc/passwd" ( -
5) , , (dup) 3, -
6.
, dup - , , -
, , -
, .
,
, , ,
, .
, 5.21. i -
,
"/etc/passwd", j - ,
i dup.
, -
i j,
. , -
read
, buf1 buf2 .
,
+--------------------------------------------------------+
| #include |
| main() |
| { |
| int i,j; |
| char buf1[512],buf2[512]; |
| |
| i = open("/etc/passwd",O_RDONLY); |
| j = dup(i); |
| read(i,buf1,sizeof(buf1)); |
| read(j,buf2,sizeof(buf2)); |
| close(i); |
| read(j,buf2,sizeof(buf2)); |
| } |
+--------------------------------------------------------+
5.21. , -
dup
( 5.2). close -
, - -
, . , -
"" ( -
1), , ,
. 7
pipe dup
.
111
,
, -
, . , -
"", .
10. -
, -
, , (. 2).
mount () -
,
umount () .
mount, ,
, -
.
mount:
mount(special pathname,directory pathname,options);
special pathname - ,
, directory pathname - -
,
( , ), options , -
" " (
+ - - - - - - - - - - - - - - - - - - - - - - - - +
/
| | |
+----------------+--+-------------+
| | | | |
bin etc usr
| | | - |
+-----+-----+ +----+----+ -
| | | | | | - |
cc date sh getty passwd -
+ - - - - - - - - - - - - - - - - - - - - - - - - +
-
+ - - - - - - - - - - - - - - - - - - - - - - - - +
/
| | |
+----------------+--+-------------+
| | | | |
bin include src
| | | | |
/dev/dsk1 +-----+-----+ | |
| | | | | | |
awk banner yacc stdio.h uts
+ - - - - - - - - - - - - - - - - - - - - - - - - +
5.22. mount
, write creat, -
). , mount :
mount("/dev/dsk1","/usr",0);
,
"/dev/dsk1", "/usr" (.
112
5.22). "/dev/dsk1" , ..
, . -
,
, .
mount
"/usr".
, .
link , V -
, (.
5.15).
. :
* , (-
);
* , ;
* ("/"
"/dev/dsk1" 5.22);
* , (
5.22 "usr", ).
-
, mount,
-
.
+------------------------------------------------------------+
| mount |
| : |
| |
| (" ") |
| : |
| { |
| ( ) |
| (); |
| (- |
| namei); |
| ; |
| , |
| ( namei); |
| ( |
| > 1) |
| { |
| ( iput); |
| (); |
| } |
| ; |
| |
| ; |
| ; |
| ; |
| ; |
| ( |
| iget), ; |
| , |
| ; |
| ( iput); |
| ;|
| } |
+------------------------------------------------------------+
5.23.
113
5.23 .
,
.
mount umount
, , -
.
.
, -
, , ,
, ,
. -
, 1 ( 1
- ?), (.
5.27). -
, -
. , -
,
,
. -
,
mount . -
, -
, , -
, (. 5.26).
,
, , -
( 10).
,
.
( getblk)
,
read. -
, ,
, "..", ,
. -
.
, ()
, -
. -
- .
,
0. ( -
)
, -
. ,
, ialloc, -
. ,
, (. 5.17
). -
, ,
. ,
" ", -
. 5.24 -
mount.
114
namei iget ,
. -
: , -
, , (
), . -
shell'.
+------------------+ +--------------------+
+------------------+ | |
| , + - - + | |
| | | |
| | | | | +-------+
| "-|<---+ | |+->| |
| " | || | || +-------+
| =1| | | ||
+------------------+ |+ >+--------------------+|
| | | | ---++
+------------------+ +---+ -|
| | | |
| | +---+- |
| =0| | +--------------------+
+------------------+ | | |
+------------------+<---+ | |
| -| | |
| | | |
| | | |
| =1| +--------------------+
+------------------+
+------------------+
5.24.
mount /dev/dsk1 /usr
cd /usr/src/uts
cd ../../..
mount -
mount,
"/dev/dsk1" "/usr". -
cd ( ) shell -
chdir, , ,
"/usr". cd
,
".." .
-
, , , -
, iget ( 5.25),
, 4.3, ,
, , -
. ,
, .
-
. , -
, ,
115
+------------------------------------------------------------+
| iget |
| : |
| : |
| { |
| |
| { |
| ( ) |
| { |
| ( ) |
| { |
| ( ); |
| ; /* */ |
| } |
| /* */ |
| ( ) |
| { |
| - |
| ; |
| |
| ; |
| ; |
| ; /* */ |
| } |
| ( ) |
| ; |
| ; |
| (); |
| } |
| |
| /* */ |
| ; |
| ; |
| -, ;|
| ( bread); |
| (, |
| 1); |
| (); |
| } |
| } |
+------------------------------------------------------------+
5.25. -
.
"/usr"
, , ,
" ", -
.
-
, , , -
, namei ( 5.26).
, 4.11. ,
,
, -
. ,
116
+------------------------------------------------------------+
| namei /* */|
| : |
| : |
| { |
| ( ) |
| = ( iget); |
| |
| = |
| ( iget); |
| |
| ( ) |
| { |
| ; |
| |
| ; |
| ( - |
| "..") |
| ; /* */|
| : |
| ( ), - |
| bmap, bread brelse; |
| ( |
| ( )) |
| { |
| -|
| ; |
| ( - |
| |
| "..") |
| { |
| /* */ |
| |
| ; |
| ( iput); |
| = ;|
| ; |
| - |
| ; |
| ( ".."); |
| } |
| ( iput); |
| = |
| ( iget); |
| } |
| /* |
| */ |
| ( ); |
| } |
| ( ); |
| } |
+------------------------------------------------------------+
5.26.
, , "..",
. ,
117
, ,
, "..",
. ,
, "..".
(cd "../../..") ,
"/usr/src/uts".
namei,
. -
"/usr/src" ".."
. ".." -
, ( ) -
- "usr" -
namei. , -
".." . ,
".." , -
, ".." . -
,
"usr", ( , -
"usr") (
"usr" ) .
"/usr" -
".." ("/"). -
chdir , -
, .
umount:
umount(special filename);
special filename . -
( 5.27) -
, ,
( iput)
, .
, -
, ,
,
. -
, ,
( 7), - , .
- , umount
: , -
.
" ", -
, "" .
, ,
( 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
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), -
.
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. ,
, ; -
.
,
. ,
, -
- , -
. ,
, :
, 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
, ,
.
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
.
, : -
.
; - ,
. - -
, .
" "
(.
[Killian 84]) V
.
, ( 13) -
. -
UNIX,
,
.
V
+---------------+ +------+ +-------+
V | open | +-----+- -+-------->| |
| close | | +------+ +-------+
| read | | +---+- -+---+ | |
| write |<---+ | +------+ | +-------+
| - |<-----|---+- -+---|---->| |
| - | | +------+ | +-------+
| - | | | | | | - |
| - | | +------+ | | - |
+---------------+ | | | | | - |
| ropen | | +------+ | +-------+
| rclose | | | - | |
| rread | | | - | | -
| rwrite |<-----+ | - | |
| - | | - | | +-------+
| - | | - | | | |
| - | | - | | +-------+
| - | | - | +---->| |
+---------------+ | - | +-------+
| - | | - | | |
| - | | - | +-------+
| - | | - | | |
| - | | - | +-------+
| - | | - | | - |
+---------------+ +------+ +-------+
5.34.
129
-
. ,
, -
, , .
,
, , -
, , , .
, , -
. 5.34
, V,
() . ,
, ,
. , -
;
,
UNIX "", -
, .
,
, . -
,
(. 5.34).
: , , -
( namei iget), -
( iput), , , -
( ), -
. 13
.
. -
, , ,
,
: -
, .
fsck -
.
( 10)
.
, fsck.
. , -
. -
,
.
, .
, -
, . -
, , -
, , -
, .
,
, -
130
. , -
, -
, .
, , -
, , ,
- . ,
-
.
, ,
.
,
. , -
(), -
. -
,
,
, .
, unlink
.
(,
), - . , -
, .
, , ,
, , .
, -
, ,
. -
, ,
, - .
, , , -
,
- . ,
.
, -
, , -
. -
.
,
. -
, : ,
. -
, ,
.
, UNIX
. , fsck,
.
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
Last-modified: Thu, 12 Feb 1998 07:19:20 GMT