我编写了一个辅助函数,使用受此answer启发的fork()和execv()启动进程。它用于启动,例如mysqldump进行数据库备份。 该代码在具有不同程序的几个不同位置完全正常。
现在我遇到一个失败的星座: 这是一个调用systemctl来停止一个单元。运行systemctl工作,单元停止。但是在中间进程中,当wait()为子进程时,wait()会挂起,直到超时进程结束。 如果我检查,如果工作进程完成了kill(),我可以告诉它确实如此。
重要提示:除了wait()没有发出工作进程结束信号之外,程序不会出现行为异常或段错误! 我的代码(见下文)中是否存在可能触发该行为的错误? 我读过Threads and fork(): think twice before mixing them但我找不到与我的问题有关的任何内容。
有什么奇怪的: 深入,深入,深入使用JSON-RPC程序。如果我使用JSON-RPC停用代码,一切正常!?
环境: 使用该函数的程序是一个多线程应用程序。所有线程都会阻止信号。主线程通过sigtimedwait()处理信号。
使用示例main函数的代码(通过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;
}
}
修改
预期输出应包含
如果出现错误,输出看起来像这样
运行示例代码时,您很可能会看到预期的输出。我无法在一个简单的例子中重现错误的结果。
答案 0 :(得分:4)
我发现了问题:
在函数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);
强>
导致
如果父进行了wait(),则只有当所有子进程都退出时才会返回此调用,然后返回-1并将errno设置为ECHILD。“
如 5.5 Voodoo:wait and SIGCHLD 一节中提到的here。
这也在WAIT(2)的手册页中进行了描述
错误[...]
ECHILD [...](这可能会发生 如果SIGCHLD的操作设置为SIG_IGN,则自己的孩子。 另请参阅有关线程的Linux Notes部分。)
愚蠢的是我没有正确检查返回值。 在尝试之前
if(exitedPid == workerPid) {
我应该检查exitedPid
是!= -1
。
如果我这样做,errno
会给我ECHILD
。如果我首先知道的话,我会阅读手册页,并且可能更快地找到问题...
狡猾的猫鼬只是为了弄乱信号处理,无论应用程序想要做些什么。另外,当使用mg_stop停止时,mongoose不会恢复信号处理的改变。
其他信息: 导致此问题的代码在2013年9月使用this commit在mongoose中更改。
答案 1 :(得分:0)
在我们的应用程序中,我们遇到了类似的问题。在子进程forks()反复出现的激烈情况下,子进程再也不会返回。可以监视子进程的PID,如果它没有返回超出特定的应用程序定义的阈值,则可以通过发送kill / Term信号来终止该进程。