【问题标题】:IO streams in fork()'ed processfork() 进程中的 IO 流
【发布时间】:2011-07-29 10:58:29
【问题描述】:

以下示例代码执行,但分叉进程的输出不可读:在我按 Enter 之前,控制台上没有显示任何内容,然后“读取失败!”显示。

问题是:为什么会这样,我如何通过fork()'ed 进程与stdin 和stdout 交互?

/* example1.c */

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>

int main() {
    pid_t pid = fork();

    if (!pid) {
        // child

        char s[64];
        printf("Enter something: ");
        char *res = fgets(s, 64, stdin);
        if (!res) {
            printf("Read failed!\n");
        } else {
            printf("You entered: %s", s);
        }
    }

    return 0;
}

更新:

IO 流异常行为的另一个例子:

/* example2.c */

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>

int main() {
    pid_t pid = fork();

    if (!pid) {
        // child

        char *argv[] = {
            "-c",
            "/home/user/echo.sh",
            NULL
        };

        execv("/bin/sh", argv);
    }
    return 0;
}

echo.sh脚本:

#!/bin/sh

read -p "Enter something: " INPUT
echo "You entered $INPUT"

这个返回

Enter something: /home/user/echo.sh: line 3: read: read error: 0: Input/output error
You entered

更新 2:

看起来这段代码完全符合我们的需要:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>

int main() {
    pid_t pid = vfork();
    if (!pid) {
        // child
        system("/home/user/echo.sh");
    }
    return 0;
}

解决方案是将fork 替换为vfork。我只是不知道为什么这个工作......

【问题讨论】:

  • 可能是因为父母在孩子完成之前就退出了?
  • @0A0D:谢谢,似乎是这样,使用system() 调用而不是fork/execv 可以轻松解决问题。但是如何在继承 IO 流的单独进程中启动可执行文件?我的意思是,我需要能够随时终止父进程,而无需等待子进程退出。
  • @Andy - vfork 暂停调用进程,直到子进程终止。这和在父进程中调用wait完全一样。
  • 虽然 vfork() 在当前 Linux 上挂起父级直到子级退出或执行,但不能保证这样做,事实上已经有 vfork 的实现是 #define vfork fork。

标签: c linux console io fork


【解决方案1】:

我想你想要wait(2)。如

/* example1.c */

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>

int main() {
    int status;
    pid_t pid = fork();

    if (!pid) {
        // child

        char s[64];
        printf("Enter something: ");
        char *res = fgets(s, 64, stdin);
        if (!res) {
            printf("Read failed!\n");
        } else {
            printf("You entered: %s", s);
        }
    }
    else
    {
        while (wait(&status) != pid);
    }
    return 0;
}

【讨论】:

  • 它有效,谢谢。但我需要的是在后台运行外部程序,而不是等待它完成。但是,正如@0A0D 已经提到的那样,您的解决方案确实可以防止流在子进程完成之前关闭。
  • 或者,直接调用_exit(0)而不是wait()。
  • @Maxim: 不能这样做:在fork()'ing 和从父级退出之间可能执行了很多其他代码,所以调用_exit,它不能处理很多exit 所做的事情,似乎是不可能的。
  • @Andy - 所以你想运行一个程序,它产生一个 sibling 进程,然后在兄弟进程继续运行时退出?
  • @Seth - 是的,这正是我想要的。
【解决方案2】:

这是因为您的子进程现在位于一个孤立的进程组中,其中没有进程是 shell 的直接子进程(应该执行作业控制)。

孤立的进程组: 至少没有一个成员的进程组其父进程不在进程组中,但在同一会话中(~ 是shell 的直接子代)。

当父子都在运行时,情况是这样的:

$ ps fax -o pid,pgid,sid,ppid,tty,stat,time,cmd
27177 27177 27177 32170 pts/6    Ss   00:00:00  |   \_ /bin/bash
 4706  4706 27177 27177 pts/6    S+   00:00:00  |       \_ ./ex1
 4707  4706 27177  4706 pts/6    S+   00:00:00  |           \_ ./ex1

在进程组 4706 上有两个进程,4706 和 4707。4706 是 27177 的子进程,它在同一个会话 (27177) 中,但在不同的进程组 (27177) 中:它是用于处理作业控制的 shell进程组 4706。

当父母去世时,情况是这样的:

$ ps fax -o pid,pgid,sid,ppid,tty,stat,time,cmd
27177 27177 27177 32170 pts/6    Ss+  00:00:00  |   \_ /bin/bash
 4664  4663 27177     1 pts/6    S    00:00:00 ./ex1

在进程组 4663 中只有一个进程 4664,并且它的父进程 (init) 不在同一个会话中。 shell 无法处理此进程组的作业控制,因此,read() 和 write() 得到 EIO。

【讨论】:

    【解决方案3】:

    如果您使用的是 UNIX/Linux,则当 stdout 进入控制台时,它是行缓冲的。也就是说,在您执行以下任一操作之前,您都看不到任何输出:

    • fflush(stdout)
    • prinf("\n")
    • stdout 缓冲区溢出。

    当 stdout 转到其他地方(如文件管道)时,它是完全缓冲的,也就是说,printf("\n") 不会刷新缓冲区。

    【讨论】:

    • 好吧,这并不能解决从stdin读取错误的问题。
    • 第一步读取失败时检查errno中的错误码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多