c - 为什么关闭管道需要很长时间才能终止子进程?

标签 c pthreads pipe fork

我的程序在等待子进程 (gzip) 完成时遇到问题,并且花费了很长时间。

在它开始等待之前,它关闭了到 gzip 的输入流,所以这应该会触发它很快终止。我检查了系统,gzip 没有消耗任何 CPU 或等待 IO(写入磁盘)。

非常奇怪的是它停止等待的时间......

我们在内部使用 pthreads 的程序。它并排处理 4 个 pthread。每个线程处理许多工作单元,对于每个工作单元,它都会启动一个新的 gzip 进程(使用 fork()execve()) 写入结果。当 gzip 没有终止时,线程会挂起,但当其他线程关闭它们的实例时,它会突然终止。

为清楚起见,我正在设置一个管道:my program(pthread) --> gzip --> file.gz

我想这可以部分用 CPU 负载来解释。但是当进程间隔几分钟启动并且整个系统由于这个锁定问题而最终只使用 4 个核心中的一个时,这似乎不太可能。

启动 gzip 的代码如下。 execPipeProcess 被调用,这样 child 直接写入文件,但从我的程序中读取。即:

execPipeProcess(&process, "gzip", -1, gzFileFd)

有什么建议吗?

typedef struct {
    int processID;
    const char * command;
    int stdin;
    int stdout;
} ChildProcess;


void closeAndWait(ChildProcess * process) {
    if (process->stdin >= 0) {
                stdLog("Closing post process stdin");
                if (close(process->stdin)) {
                exitError(-1,errno, "Failed to close stdin for %s",  process->command);
                }
        }
    if (process->stdout >= 0) {
                stdLog("Closing post process stdin");
                if (close(process->stdout)) {
            exitError(-1,errno, "Failed to close stdout for %s", process->command);
                }
        }

    int status;
        stdLog("waiting on post process %d", process->processID);
    if (waitpid(process->processID, &status, 0) == -1) {
        exitError(-1, errno, "Could not wait for %s", process->command);
    }
        stdLog("post process finished");

    if (!WIFEXITED(status)) exitError(-1, 0, "Command did not exit properly %s", process->command);
    if (WEXITSTATUS(status)) exitError(-1, 0, "Command %s returned %d not 0", process->command, WEXITSTATUS(status));
    process->processID = 0;
}



void execPipeProcess(ChildProcess * process, const char* szCommand, int in, int out) {
    // Expand any args
    wordexp_t words;
    if (wordexp (szCommand, &words, 0)) exitError(-1, 0, "Could not expand command %s\n", szCommand);


    // Runs the command
    char nChar;
    int nResult;

    if (in < 0) {
        int aStdinPipe[2];
        if (pipe(aStdinPipe) < 0) {
            exitError(-1, errno, "allocating pipe for child input redirect failed");
        }
        process->stdin = aStdinPipe[PIPE_WRITE];
        in = aStdinPipe[PIPE_READ];
    }
    else {
        process->stdin = -1;
    }
    if (out < 0) {
        int aStdoutPipe[2];
        if (pipe(aStdoutPipe) < 0) {
            exitError(-1, errno, "allocating pipe for child input redirect failed");
        }
        process->stdout = aStdoutPipe[PIPE_READ];
        out = aStdoutPipe[PIPE_WRITE];
    }
    else {
        process->stdout = -1;
    }

    process->processID = fork();
    if (0 == process->processID) {
        // child continues here

        // these are for use by parent only
        if (process->stdin >= 0) close(process->stdin);
        if (process->stdout >= 0) close(process->stdout);

        // redirect stdin
        if (STDIN_FILENO != in) {
            if (dup2(in, STDIN_FILENO) == -1) {
              exitError(-1, errno, "redirecting stdin failed");
            }
            close(in);
        }

        // redirect stdout
        if (STDOUT_FILENO != out) {
            if (dup2(out, STDOUT_FILENO) == -1) {
              exitError(-1, errno, "redirecting stdout failed");
            }
            close(out);
        }

        // we're done with these; they've been duplicated to STDIN and STDOUT

        // run child process image
        // replace this with any exec* function find easier to use ("man exec")
        nResult = execvp(words.we_wordv[0], words.we_wordv);

        // if we get here at all, an error occurred, but we are in the child
        // process, so just exit
        exitError(-1, errno, "could not run %s", szCommand);
  } else if (process->processID > 0) {
        wordfree(&words);
        // parent continues here

        // close unused file descriptors, these are for child only
        close(in);
        close(out);
        process->command = szCommand;
    } else {
        exitError(-1,errno, "Failed to fork");
    }
}

最佳答案

子进程继承打开的文件描述符。

每个后续的 gzip 子进程不仅继承用于与该特定实例通信的管道文件描述符,还继承用于连接到先前子进程实例的管道的文件描述符。

这意味着当主进程执行关闭时,stdin 管道仍然打开,因为在几个子进程中还有一些其他文件描述符用于同一管道。一旦那些终止管道最终关闭。

一个快速的修复方法是通过设置 close-on-exec 标志来防止子进程继承专用于主进程的管道文件描述符。

由于涉及多个线程,因此应该对子进程进行序列化,以防止子进程继承用于另一个子进程的管道 fds。

关于c - 为什么关闭管道需要很长时间才能终止子进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28159204/

相关文章:

c++ - 实时搜索引擎的算法有哪些?

c++ - zlib c++ 和解压文件

shell - 与命名管道分离而不终止源程序

python - 难以通过 python 使用 qhull 管道

c - 如何将特定值传递给一个线程?

c - 在刚刚从该 FIFO 中读取所有数据后,是否应该从 FIFO 中读取 block ?

c - gtk+-2.0,林间空地

c - 杀死一个 fork 的 child

c++ - 在 C++ 中实现二进制信号量类

c - C 中的单线程与 pthread 的多线程