|
> 「新しいファイル・ディスクリプタはargに与えられた値以上の有効な値が与え
> られます。」と書いてありますが、この意味がわかりません、どういう意味か
> わかりすか?何か例で示していただければありがたいのですが・・・
$ cat file
abcdefghijklmnopqrstuvwxyz
$ cat a.c
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
int main(void)
{
int fd1, fd2, n; char buf[16];
fd1 = open("file", O_RDONLY);
n = read(fd1, buf, 12);
printf("fd1=%d, buf=[%.*s]\n", fd1, n, buf);
fd2 = fcntl(fd1, F_DUPFD, 6);
n = read(fd2, buf, 12);
printf("fd2=%d, buf=[%.*s]\n", fd2, n, buf);
close(fd2);
close(fd1);
return 0;
}
$ gcc a.c
$ ./a.out
fd1=3, buf=[abcdefghijkl]
fd2=6, buf=[mnopqrstuvwx]
$
fcntl(fd1, F_DUPFD, 6) は、4 や 5 が空いているのに 6 を返します。
もし、6 が既に使われていたら、7 を返すでしょう。
|