: UNIX

5.13 DU

5.13 DU

dup , . . :

newfd = dup(fd);

fd , , newfd , . dup , , , . , , 5.20, , : (open) /etc/passwd ( 3), local ( 4), /etc/passwd ( 5) , , (dup) 3, 6.


5.20. dup

, dup , , , , , . , , , , , .

, 5.21. i , /etc/passwd, j , i dup. , i j, . , read , buf1 buf2 . , ( 5.2). close , - , . , ( 1), , , . 7 pipe dup .

#include fcntl.h

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


: 0.109. /Cache: 5 / 0