gpt4 book ai didi

c - linux下C中的文件描述符示例

转载 作者:太空宇宙 更新时间:2023-11-04 10:08:07 26 4
gpt4 key购买 nike

为了更好地理解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

有谁能解释一下当读取描述符关闭时发生了什么?我的期望是子 block 下的代码每次都由两个进程执行,但这只有在子 block 中启用 close(p[0]) 时才会发生。为什么?

最佳答案

您有一个竞争条件:两个进程正在调用 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, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\4\2\0\0\0\0\0"..., 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, "\7\0\0\0", 4) = 4
// parent happens to win the read - often the case with multiple CPUs
[pid 6639] <... read resumed> "\7\0\0\0", 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, "\7\0\0\0", 4) = 4
[pid 25597] getpid() = 25597
[pid 25597] write(1, "shared print: 25597\n", 20shared print: 25597
) = 20
[pid 25597] read(3, "\7\0\0\0", 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 ...>

另请注意,您可以将管道设置为非阻塞模式,但是您必须处理EAGAIN(通常通过在select family) 所以它在这里无济于事:

int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);

关于c - linux下C中的文件描述符示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50890653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com