linux 下的 C 文件描述符示例
File descriptors example in C under linux
为了更好的理解c语言的文件描述符,我写了下面的代码
int main(){
int p[2], n=7, r;
pipe(p);
if(fork() == 0){
//close(p[0]);
printf("child: %d\n", getpid());
write(p[1], &n, sizeof(n));
}
//the below code will be executed by parent and child process
printf("shared print: %d\n", getpid());
r = read(p[0], &n, sizeof(n));
printf("shared print, value from child: %d, pid: %d\n", n, getpid());
printf("read result: %d, pid: %d\n", r, getpid());
}
我得到以下结果:
shared print: 332
child: 333
shared print: 333
shared print, value from child: 7, pid: 332
read result: 4, pid: 332
如果 child 的第一行没有注释,我得到:
shared print: 339
child: 340
shared print: 340
shared print, value from child: 7, pid: 339
shared print, value from child: 7, pid: 340
read result: 4, pid: 339
read result: -1, pid: 340
有谁能解释一下关闭读取描述符时发生了什么?我的期望是子块下的代码每次都由两个进程执行,但这只有在子块中启用 close(p[0]) 时才会发生。为什么?
如果您关闭 child 中的 p[0],child 的 read() 调用将失败,您将保留旧值 (7)。如果不关闭child中的p[0],child块的read()调用,因为那里没有什么可读的(parent读取所有数据) , 但由于 p[1] 未关闭,因此也没有允许 read() 到 return.
的 EOF
所以基本上,如果 p[0] 在 child 中未关闭,child 会卡在 read() 调用处。
您有一个竞争条件:两个进程正在调用 read
,但其中一个只有足够的数据。
如果 parent 先读取,然后 child 会挂起 - 但由于它在后台,您可能没有注意到。
如果 child 先读取,parent 会挂起,您会注意到。
使用 strace -f ./foo
有助于显示正在发生的事情:
execve("./foo", ["./foo"], [/* 52 vars */]) = 0
// loading libraries
brk(NULL) = 0x55fd6dc09000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcf9ebce000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=168993, ...}) = 0
mmap(NULL, 168993, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fcf9eba4000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "7ELF[=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=]>[=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=]"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1689360, ...}) = 0
mmap(NULL, 3795296, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fcf9e60f000
mprotect(0x7fcf9e7a4000, 2097152, PROT_NONE) = 0
mmap(0x7fcf9e9a4000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x195000) = 0x7fcf9e9a4000
mmap(0x7fcf9e9aa000, 14688, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fcf9e9aa000
close(3) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcf9eba2000
arch_prctl(ARCH_SET_FS, 0x7fcf9eba2700) = 0
mprotect(0x7fcf9e9a4000, 16384, PROT_READ) = 0
mprotect(0x55fd6c47b000, 4096, PROT_READ) = 0
mprotect(0x7fcf9ebd1000, 4096, PROT_READ) = 0
munmap(0x7fcf9eba4000, 168993) = 0
pipe([3, 4]) = 0
// the fork() function actually calls `clone` these days
clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7fcf9eba29d0) = 6640
strace: Process 6640 attached
[pid 6639] getpid() = 6639
// the processes are running at the same time, so you get a lot of <unfinished ...> even for short-duration syscalls
[pid 6640] getpid( <unfinished ...>
[pid 6639] fstat(1, <unfinished ...>
[pid 6640] <... getpid resumed> ) = 6640
// fstat is needed before the first write, to know whether stdout should be line-buffered or fully-buffered
// this happens in *both* processes since you didn't do any writes before fork()ing
// (and if you did, you would have to call fflush)
[pid 6639] <... fstat resumed> {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
[pid 6640] fstat(1, <unfinished ...>
// brk is used to implement malloc, for stdout's buffer
[pid 6639] brk(NULL <unfinished ...>
[pid 6640] <... fstat resumed> {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
[pid 6639] <... brk resumed> ) = 0x55fd6dc09000
[pid 6640] brk(NULL <unfinished ...>
[pid 6639] brk(0x55fd6dc2a000 <unfinished ...>
[pid 6640] <... brk resumed> ) = 0x55fd6dc09000
[pid 6639] <... brk resumed> ) = 0x55fd6dc2a000
[pid 6640] brk(0x55fd6dc2a000 <unfinished ...>
// note mixed output since strace and the program are writing to the same place
// note also that if output wasn't to a TTY, buffering would delay the writes.
[pid 6639] write(1, "shared print: 6639\n", 19shared print: 6639
<unfinished ...>
[pid 6640] <... brk resumed> ) = 0x55fd6dc2a000
[pid 6639] <... write resumed> ) = 19
[pid 6639] read(3, <unfinished ...>
[pid 6640] write(1, "child: 6640\n", 12child: 6640
) = 12
// little-endian
[pid 6640] write(4, "[=10=][=10=][=10=]", 4) = 4
// parent happens to win the read - often the case with multiple CPUs
[pid 6639] <... read resumed> "[=10=][=10=][=10=]", 4) = 4
[pid 6640] getpid( <unfinished ...>
[pid 6639] getpid( <unfinished ...>
[pid 6640] <... getpid resumed> ) = 6640
[pid 6639] <... getpid resumed> ) = 6639
[pid 6640] write(1, "shared print: 6640\n", 19shared print: 6640
<unfinished ...>
[pid 6639] write(1, "shared print, value from child: "..., 45shared print, value from child: 7, pid: 6639
<unfinished ...>
[pid 6640] <... write resumed> ) = 19
[pid 6639] <... write resumed> ) = 45
// losing read here
[pid 6640] read(3, <unfinished ...>
[pid 6639] getpid() = 6639
[pid 6639] write(1, "read result: 4, pid: 6639\n", 26read result: 4, pid: 6639
) = 26
// parent finishes - outside of strace, this looks like everything is done
[pid 6639] exit_group(0) = ?
[pid 6639] +++ exited with 0 +++
// parent interrupted ... child is still waiting
// (although since ^C applies to the whole process group, the child happens to get interrupted sometime after strace stops paying attention - without strace, I wouldn't've given it the signal)
^Cstrace: Process 6640 detached
对比如果添加 else sleep(1)
会发生什么,所以 child 有一些时间 运行:
// startup omitted
pipe([3, 4]) = 0
clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f469a2149d0) = 25597
strace: Process 25597 attached
[pid 25596] nanosleep({tv_sec=1, tv_nsec=0}, <unfinished ...>
[pid 25597] getpid() = 25597
[pid 25597] fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
[pid 25597] brk(NULL) = 0x555f092f8000
[pid 25597] brk(0x555f09319000) = 0x555f09319000
[pid 25597] write(1, "child: 25597\n", 13child: 25597
) = 13
// note that writing-then-reading a pipe in the same thread is only safe if at most PIPE_BUF bytes, which is guaranteed to be 512 by POSIX, but is 4096 on Linux
[pid 25597] write(4, "[=11=][=11=][=11=]", 4) = 4
[pid 25597] getpid() = 25597
[pid 25597] write(1, "shared print: 25597\n", 20shared print: 25597
) = 20
[pid 25597] read(3, "[=11=][=11=][=11=]", 4) = 4
[pid 25597] getpid() = 25597
[pid 25597] write(1, "shared print, value from child: "..., 46shared print, value from child: 7, pid: 25597
) = 46
[pid 25597] getpid() = 25597
[pid 25597] write(1, "read result: 4, pid: 25597\n", 27read result: 4, pid: 25597
) = 27
// child finishes first
[pid 25597] exit_group(0) = ?
[pid 25597] +++ exited with 0 +++
<... nanosleep resumed> {tv_sec=0, tv_nsec=998890543}) = ? ERESTART_RESTARTBLOCK (Interrupted by signal)
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=25597, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
restart_syscall(<... resuming interrupted nanosleep ...>) = 0
getpid() = 25596
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
brk(NULL) = 0x555f092f8000
brk(0x555f09319000) = 0x555f09319000
write(1, "shared print: 25596\n", 20shared print: 25596
) = 20
// parent still blocking when I press ^C
read(3, ^Cstrace: Process 25596 detached
<detached ...>
另请注意,您可以将管道设置为 non-blocking 模式,但随后您 必须 处理 EAGAIN
(通常通过调用 select
family) 所以这里没用:
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
为了更好的理解c语言的文件描述符,我写了下面的代码
int main(){
int p[2], n=7, r;
pipe(p);
if(fork() == 0){
//close(p[0]);
printf("child: %d\n", getpid());
write(p[1], &n, sizeof(n));
}
//the below code will be executed by parent and child process
printf("shared print: %d\n", getpid());
r = read(p[0], &n, sizeof(n));
printf("shared print, value from child: %d, pid: %d\n", n, getpid());
printf("read result: %d, pid: %d\n", r, getpid());
}
我得到以下结果:
shared print: 332
child: 333
shared print: 333
shared print, value from child: 7, pid: 332
read result: 4, pid: 332
如果 child 的第一行没有注释,我得到:
shared print: 339
child: 340
shared print: 340
shared print, value from child: 7, pid: 339
shared print, value from child: 7, pid: 340
read result: 4, pid: 339
read result: -1, pid: 340
有谁能解释一下关闭读取描述符时发生了什么?我的期望是子块下的代码每次都由两个进程执行,但这只有在子块中启用 close(p[0]) 时才会发生。为什么?
如果您关闭 child 中的 p[0],child 的 read() 调用将失败,您将保留旧值 (7)。如果不关闭child中的p[0],child块的read()调用,因为那里没有什么可读的(parent读取所有数据) , 但由于 p[1] 未关闭,因此也没有允许 read() 到 return.
的 EOF所以基本上,如果 p[0] 在 child 中未关闭,child 会卡在 read() 调用处。
您有一个竞争条件:两个进程正在调用 read
,但其中一个只有足够的数据。
如果 parent 先读取,然后 child 会挂起 - 但由于它在后台,您可能没有注意到。
如果 child 先读取,parent 会挂起,您会注意到。
使用 strace -f ./foo
有助于显示正在发生的事情:
execve("./foo", ["./foo"], [/* 52 vars */]) = 0
// loading libraries
brk(NULL) = 0x55fd6dc09000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcf9ebce000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=168993, ...}) = 0
mmap(NULL, 168993, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fcf9eba4000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "7ELF[=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=]>[=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=][=10=]"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1689360, ...}) = 0
mmap(NULL, 3795296, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fcf9e60f000
mprotect(0x7fcf9e7a4000, 2097152, PROT_NONE) = 0
mmap(0x7fcf9e9a4000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x195000) = 0x7fcf9e9a4000
mmap(0x7fcf9e9aa000, 14688, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fcf9e9aa000
close(3) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcf9eba2000
arch_prctl(ARCH_SET_FS, 0x7fcf9eba2700) = 0
mprotect(0x7fcf9e9a4000, 16384, PROT_READ) = 0
mprotect(0x55fd6c47b000, 4096, PROT_READ) = 0
mprotect(0x7fcf9ebd1000, 4096, PROT_READ) = 0
munmap(0x7fcf9eba4000, 168993) = 0
pipe([3, 4]) = 0
// the fork() function actually calls `clone` these days
clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7fcf9eba29d0) = 6640
strace: Process 6640 attached
[pid 6639] getpid() = 6639
// the processes are running at the same time, so you get a lot of <unfinished ...> even for short-duration syscalls
[pid 6640] getpid( <unfinished ...>
[pid 6639] fstat(1, <unfinished ...>
[pid 6640] <... getpid resumed> ) = 6640
// fstat is needed before the first write, to know whether stdout should be line-buffered or fully-buffered
// this happens in *both* processes since you didn't do any writes before fork()ing
// (and if you did, you would have to call fflush)
[pid 6639] <... fstat resumed> {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
[pid 6640] fstat(1, <unfinished ...>
// brk is used to implement malloc, for stdout's buffer
[pid 6639] brk(NULL <unfinished ...>
[pid 6640] <... fstat resumed> {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
[pid 6639] <... brk resumed> ) = 0x55fd6dc09000
[pid 6640] brk(NULL <unfinished ...>
[pid 6639] brk(0x55fd6dc2a000 <unfinished ...>
[pid 6640] <... brk resumed> ) = 0x55fd6dc09000
[pid 6639] <... brk resumed> ) = 0x55fd6dc2a000
[pid 6640] brk(0x55fd6dc2a000 <unfinished ...>
// note mixed output since strace and the program are writing to the same place
// note also that if output wasn't to a TTY, buffering would delay the writes.
[pid 6639] write(1, "shared print: 6639\n", 19shared print: 6639
<unfinished ...>
[pid 6640] <... brk resumed> ) = 0x55fd6dc2a000
[pid 6639] <... write resumed> ) = 19
[pid 6639] read(3, <unfinished ...>
[pid 6640] write(1, "child: 6640\n", 12child: 6640
) = 12
// little-endian
[pid 6640] write(4, "[=10=][=10=][=10=]", 4) = 4
// parent happens to win the read - often the case with multiple CPUs
[pid 6639] <... read resumed> "[=10=][=10=][=10=]", 4) = 4
[pid 6640] getpid( <unfinished ...>
[pid 6639] getpid( <unfinished ...>
[pid 6640] <... getpid resumed> ) = 6640
[pid 6639] <... getpid resumed> ) = 6639
[pid 6640] write(1, "shared print: 6640\n", 19shared print: 6640
<unfinished ...>
[pid 6639] write(1, "shared print, value from child: "..., 45shared print, value from child: 7, pid: 6639
<unfinished ...>
[pid 6640] <... write resumed> ) = 19
[pid 6639] <... write resumed> ) = 45
// losing read here
[pid 6640] read(3, <unfinished ...>
[pid 6639] getpid() = 6639
[pid 6639] write(1, "read result: 4, pid: 6639\n", 26read result: 4, pid: 6639
) = 26
// parent finishes - outside of strace, this looks like everything is done
[pid 6639] exit_group(0) = ?
[pid 6639] +++ exited with 0 +++
// parent interrupted ... child is still waiting
// (although since ^C applies to the whole process group, the child happens to get interrupted sometime after strace stops paying attention - without strace, I wouldn't've given it the signal)
^Cstrace: Process 6640 detached
对比如果添加 else sleep(1)
会发生什么,所以 child 有一些时间 运行:
// startup omitted
pipe([3, 4]) = 0
clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f469a2149d0) = 25597
strace: Process 25597 attached
[pid 25596] nanosleep({tv_sec=1, tv_nsec=0}, <unfinished ...>
[pid 25597] getpid() = 25597
[pid 25597] fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
[pid 25597] brk(NULL) = 0x555f092f8000
[pid 25597] brk(0x555f09319000) = 0x555f09319000
[pid 25597] write(1, "child: 25597\n", 13child: 25597
) = 13
// note that writing-then-reading a pipe in the same thread is only safe if at most PIPE_BUF bytes, which is guaranteed to be 512 by POSIX, but is 4096 on Linux
[pid 25597] write(4, "[=11=][=11=][=11=]", 4) = 4
[pid 25597] getpid() = 25597
[pid 25597] write(1, "shared print: 25597\n", 20shared print: 25597
) = 20
[pid 25597] read(3, "[=11=][=11=][=11=]", 4) = 4
[pid 25597] getpid() = 25597
[pid 25597] write(1, "shared print, value from child: "..., 46shared print, value from child: 7, pid: 25597
) = 46
[pid 25597] getpid() = 25597
[pid 25597] write(1, "read result: 4, pid: 25597\n", 27read result: 4, pid: 25597
) = 27
// child finishes first
[pid 25597] exit_group(0) = ?
[pid 25597] +++ exited with 0 +++
<... nanosleep resumed> {tv_sec=0, tv_nsec=998890543}) = ? ERESTART_RESTARTBLOCK (Interrupted by signal)
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=25597, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
restart_syscall(<... resuming interrupted nanosleep ...>) = 0
getpid() = 25596
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
brk(NULL) = 0x555f092f8000
brk(0x555f09319000) = 0x555f09319000
write(1, "shared print: 25596\n", 20shared print: 25596
) = 20
// parent still blocking when I press ^C
read(3, ^Cstrace: Process 25596 detached
<detached ...>
另请注意,您可以将管道设置为 non-blocking 模式,但随后您 必须 处理 EAGAIN
(通常通过调用 select
family) 所以这里没用:
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);