gpt4 book ai didi

c - 当多个进程尝试从同一个管道读取时会发生什么?

转载 作者:太空宇宙 更新时间:2023-11-04 02:16:22 25 4
gpt4 key购买 nike

#include <unistd.h>
#include <stdio.h>
int main()
{
char buff[100];
int pfd[2];
buff[0] = '\0';
pipe(pfd);
if (fork())
write(pfd[1],"hello world", 12);
fork();
read(pfd[0], buff, 100);
printf("%s\n", buff);
printf("goodbye\n");
}

我知道只有一个进程会写入管道,但我不明白的是,有没有可能一个进程从管道读取并只读取“hello world”的一部分,而其他进程读取“ Hello World ”的其他部分?

换句话说,当一个进程试图读取一个管道而另一个进程正在读取它时会发生什么?

最佳答案

Demons will fly from your nose!

实际上,如果他们从同一个管道读取数据,那么他们就是 holding file descriptors指向相同的 struct file在内核中。这意味着内核将决定谁获得数据。只有一个进程会读取任何给定的字节。

大多数对管道的读写都有关于 PIPE_BUF 的一些保证,您可能想调查一下。

关于c - 当多个进程尝试从同一个管道读取时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7187830/

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