2014-11-17 113 views
1

我用这个answer启发了一个使用fork()和execv()启动进程的辅助函数。它用于例如开始mysqldump进行数据库备份。 该代码在不同程序的几个不同位置完全正常工作。Linux:fork&execv,等待子进程挂起

现在我打了一个失败的星座: 这是一个调用systemctl来停止一个单位。运行systemctl工作,单元停止。但是在中间进程中,当wait()等待子进程时,wait()会挂起,直到超时过程结束。 如果我检查,如果工作进程完成kill(),我可以告诉它它已经完成。

重要提示:除了wait()不表示结束工作进程外,程序不会发生错误或故障。 我的代码中是否有任何内容(见下文)不正确,可能会触发该行为? 我读过Threads and fork(): think twice before mixing them,但我找不到与我的问题有关的任何内容。

奇怪的是: 程序中使用了深入深入的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; 
    } 
} 

编辑

预期的输出应包含

  • 中间过程:等待子过程。
  • 中间过程:子过程完成。状态:0.
  • 中间过程:杀死超时过程。

在错误的情况下输出看起来像这样

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

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

+0

而父被阻止'wait',请问孩子显示为一个僵尸? – Useless

+0

我编译和运行的代码,我没觉得有有奇怪的行为,你能提供的'预期result'和'当前result'? –

+0

@无用:没有僵尸进程。 'ps -l'返回例如用于中间的进程ID 617,用于超时的进程ID 619,但不是618。 我在杀码测试()产生相同的结果。 – DrP3pp3r

回答

4

我发现这个问题:

在猫鼬(JSON-RPC使用猫鼬)源在功能mg_start我发现下面的代码

#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);

导致该

如果父不等待(),该调用将只返回时,所有的孩子都退出,然后返回-1,errno设置为ECHILD“

所提到的部分here5.5巫术:等待和SIGCHLD

这也是手册页描述WAIT(2)

错误[...]

ECHILD [...](这种情况可能 一个自己的孩子,如果对于SIGCHLD操作设置也SIG_IGN。 看到有关线程Linux的注释部分。)

愚蠢对我而言不正确检查返回值。 试图

if(exitedPid == workerPid) { 

之前,我应该检查该exitedPid!= -1

如果我这样做errno给我ECHILD。如果我首先知道,我会阅读手册页,并可能更快地发现问题...

淘气的猫鼬只是为了弄乱信号处理,无论应用程序想做什么。此外,猫鼬在用mg_stop停止时不会恢复信号处理的改变。

附加信息: 导致此问题的代码是在改变猫鼬在2013年9月与this commit