gpt4 book ai didi

c++ - Linux:fork & execv,等待子进程挂起

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:03 24 4
gpt4 key购买 nike

受此 answer 的启发,我编写了一个辅助函数来使用 fork() 和 execv() 启动进程.它用于启动,例如mysqldump 进行数据库备份。该代码在不同程序的几个不同位置完全可以正常工作。

现在我找到了一个失败的星座:它是调用 systemctl 来停止一个单元。运行 systemctl 工作,单元停止。但是在中间进程中,当为子进程wait()时,wait()一直挂起,直到超时进程结束。如果我检查,如果工作进程使用 kill() 完成,我可以确定它完成了。

重要提示:除了 wait() 不会发出工作进程结束的信号外,程序不会出现异常或段错误!我的代码(见下文)中是否有任何不正确的地方可能会触发该行为?我读过 Threads and fork(): think twice before mixing them但我在那里找不到与我的问题相关的任何内容。

奇怪的是:Deep, deep, deep 在程序中使用了JSON-RPC。如果我使用 JSON-RPC 停用代码,一切正常!?

环境:使用该函数的程序是一个多线程应用程序。所有线程的信号都被阻塞。主线程通过 sigtimedwait() 处理信号。

代码(生产代码,其中通过 std::cout 将日志记录换成输出)具有示例主要功能:

#include <iostream>

#include <unistd.h>
#include <sys/wait.h>

namespace {

bool checkStatus(const int status) {
return( WIFEXITED(status) && ( WEXITSTATUS(status) == 0 ) );
}

}

bool startProcess(const char* const path, const char* const argv[], const unsigned int timeoutInSeconds, pid_t& processId, const int* const fileDescriptor) {
auto result = true;

const pid_t intermediatePid = fork();
if(intermediatePid == 0) {
// intermediate process
std::cout << "Intermediate process: Started (" << getpid() << ")." << std::endl;
const pid_t workerPid = fork();
if(workerPid == 0) {
// worker process
if(fileDescriptor) {
std::cout << "Worker process: Redirecting file descriptor to stdin." << std::endl;
const auto dupResult = dup2(*fileDescriptor, STDIN_FILENO);
if(-1 == dupResult) {
std::cout << "Worker process: Duplication of file descriptor failed." << std::endl;
_exit(EXIT_FAILURE);
}
}
execv(path, const_cast<char**>(argv));

std::cout << "Intermediate process: Worker failed!" << std::endl;
_exit(EXIT_FAILURE);
} else if(-1 == workerPid) {
std::cout << "Intermediate process: Starting worker failed!" << std::endl;
_exit(EXIT_FAILURE);
}

const pid_t timeoutPid = fork();
if(timeoutPid == 0) {
// timeout process
std::cout << "Timeout process: Started (" << getpid() << ")." << std::endl;
sleep(timeoutInSeconds);
std::cout << "Timeout process: Finished." << std::endl;
_exit(EXIT_SUCCESS);
} else if(-1 == timeoutPid) {
std::cout << "Intermediate process: Starting timeout process failed." << std::endl;
kill(workerPid, SIGKILL);
std::cout << "Intermediate process: Finished." << std::endl;
_exit(EXIT_FAILURE);
}

// ---------------------------------------
// This code is only used for double checking if the worker is still running.
// The if condition never evaluated to true in my tests.
const auto killResult = kill(workerPid, 0);
if((-1 == killResult) && (ESRCH == errno)) {
std::cout << "Intermediate process: Worker is not running." << std::endl;
}
// ---------------------------------------

std::cout << "Intermediate process: Waiting for child processes." << std::endl;
int status = -1;
const pid_t exitedPid = wait(&status);

// ---------------------------------------
// This code is only used for double checking if the worker is still running.
// The if condition evaluates to true in the case of an error.
const auto killResult2 = kill(workerPid, 0);
if((-1 == killResult2) && (ESRCH == errno)) {
std::cout << "Intermediate process: Worker is not running." << std::endl;
}
// ---------------------------------------

std::cout << "Intermediate process: Child process finished. Status: " << status << "." << std::endl;
if(exitedPid == workerPid) {
std::cout << "Intermediate process: Killing timeout process." << std::endl;
kill(timeoutPid, SIGKILL);
} else {
std::cout << "Intermediate process: Killing worker process." << std::endl;
kill(workerPid, SIGKILL);
std::cout << "Intermediate process: Waiting for worker process to terminate." << std::endl;
wait(nullptr);
std::cout << "Intermediate process: Finished." << std::endl;
_exit(EXIT_FAILURE);
}
std::cout << "Intermediate process: Waiting for timeout process to terminate." << std::endl;
wait(nullptr);
std::cout << "Intermediate process: Finished." << std::endl;
_exit(checkStatus(status) ? EXIT_SUCCESS : EXIT_FAILURE);

} else if(-1 == intermediatePid) {
// error
std::cout << "Parent process: Error starting intermediate process!" << std::endl;
result = false;
} else {
// parent process
std::cout << "Parent process: Intermediate process started. PID: " << intermediatePid << "." << std::endl;
processId = intermediatePid;
}

return(result);
}

bool waitForProcess(const pid_t processId) {
int status = 0;
const auto waitResult = waitpid(processId, &status, 0);
auto result = false;
if(waitResult == processId) {
result = checkStatus(status);
}
return(result);
}

int main() {
pid_t pid = 0;
const char* const path = "/bin/ls";
const char* argv[] = { "/bin/ls", "--help", nullptr };
const unsigned int timeoutInS = 5;
const auto startResult = startProcess(path, argv, timeoutInS, pid, nullptr);
if(startResult) {
const auto waitResult = waitForProcess(pid);
std::cout << "waitForProcess returned " << waitResult << "." << std::endl;
} else {
std::cout << "startProcess failed!" << std::endl;
}
}

编辑

预期的输出应该包含

  • 中间进程:WAITING子进程。
  • 中间进程:子进程完成。状态:0。
  • 中间进程:杀死超时进程。

如果出现错误,输出如下所示

  • 中间进程:WAITING子进程。
  • 中间进程:子进程完成。状态:-1
  • 中间进程:杀死工作进程。

当您运行示例代码时,您很可能会看到预期的输出。我无法在一个简单的示例中重现错误的结果。

最佳答案

我发现了问题:

在函数 mg_start 的 mongoose(JSON-RPC 使用 mongoose)源代码中,我找到了以下代码

#if !defined(_WIN32) && !defined(__SYMBIAN32__)
// Ignore SIGPIPE signal, so if browser cancels the request, it
// won't kill the whole process.
(void) signal(SIGPIPE, SIG_IGN);
// Also ignoring SIGCHLD to let the OS to reap zombies properly.
(void) signal(SIGCHLD, SIG_IGN);
#endif // !_WIN32

(void) signal(SIGCHLD, SIG_IGN);

原因

if the parent does a wait(), this call will return only when all children have exited, and then returns -1 with errno set to ECHILD."

如前所述here5.5 Voodoo:WAITING和 SIGCHLD 部分。

这在 WAIT(2) 的手册页中也有描述

ERRORS [...]

ECHILD [...] (This can happen for one's own child if the action for SIGCHLD is set to SIG_IGN. See also the Linux Notes section about threads.)

我很愚蠢,没有正确检查返回值。在尝试之前

if(exitedPid == workerPid) {

我应该检查 exitedPid!= -1

如果我这样做,errno 会给我 ECHILD。如果我一开始就知道这一点,我会阅读手册页并可能更快地找到问题...

淘气的 Mongoose 只是为了搞乱信号处理,不管应用程序想要做什么。此外,当使用 mg_stop 停止时,mongoose 不会恢复信号处理的改变。

附加信息:导致此问题的代码已于 2013 年 9 月在 mongoose 中更改为 this commit .

关于c++ - Linux:fork & execv,等待子进程挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26971660/

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