小编Vel*_*tas的帖子

在C中,当没有东西可读时,读取功能如何"知道"?

在下面的程序(不是我的程序,但是我修改过的程序)中,子进程对管道进行了两次写入.当我运行该程序时,我得到以下输出:

Received string: Hello, world!
This is the child process.
Run Code Online (Sandbox Code Playgroud)

如何由父进程执行的读取从管道缓冲区中捕获这两个字符串?什么(如果有的话)阻止父进程在读取第一个字符串(或第一个字符串的第一个字符串)之后假设没有其他内容可以从缓冲区中读取并退出?

有问题的计划:

int main(void)
{
    int     fd[2], nbytes;
    pid_t   childpid;
    char    string[] = "Hello, world!\n";
    char    string2[] = "This is the child process.\n";
    char    readbuffer[80];

    pipe(fd);

    if((childpid = fork()) == -1)
    {
            perror("fork");
            return 1;
    }

    if(childpid == 0)
    {
            /* Child process closes pipe's input file descriptor */
            close(fd[0]);

            /* Send "string" through the output side of pipe */
            write(fd[1], string, (strlen(string)));
            write(fd[1], string2, (strlen(string2)+1));
            return 1; …
Run Code Online (Sandbox Code Playgroud)

c fork pipe parent-child

9
推荐指数
1
解决办法
9881
查看次数

如何退出 Forth 环境?

如果我在 Forth 环境中,我该如何离开,以及如何编程一个词来离开?

forth

0
推荐指数
1
解决办法
418
查看次数

标签 统计

c ×1

fork ×1

forth ×1

parent-child ×1

pipe ×1