2

子が死亡した場合、親がプロセスとそれに依存するプロセスを再起動するように、他のプロセスを管理するプロセスを作成しようとしています。

問題は、この構造の途中でプロセスを再起動するときにプロセスのツリー構造を作成すると、新しい子プロセスが終了したときに通知されないことに気付くことです。

例を書きます。祖父母、親、子の 3 つのプロセスがあるとします。祖父母がフォークして親を開始し、そのフォークして子を開始します (コードはこの投稿の最後に置きます)。子をすべて正常に終了させると、子は正しく再起動されます。

親を殺すと問題が発生します...祖父母は子を再起動する親を再起動しますが、子を殺すとプロセスはゾンビ状態のままになり、SIGCHLDは親プロセスに配信されません。

言い換えると:

  • 祖父母プロセスを開始し、3 つのプロセスすべてが起動するのを待ちます
  • 親プロセスを強制終了し、祖父母が親を再起動して子を再起動するのを待ちます
  • 子プロセスを強制終了すると、プロセスはゾンビ状態のままになります。

私はこの動作を理解できません...シグナルと待機に関する例とドキュメントをたくさん読みました。親と祖父母のフォークの前にデフォルトハンドラーをリセットしようとしましたが、何も機能していないようです...ここにコードサンプル...

祖父母.cpp

#include <cstdio>
#include <string>
#include <cstring>

#include <stdlib.h>
#include <signal.h>
#include <wait.h>

using namespace std;

void startProcess(string processFile);
void childDieHandler(int sig, siginfo_t *child_info, void *context);

FILE            *logFile;
int         currentChildPid;

int main(int argc, char** argv)
{
    currentChildPid = 0;
    logFile = stdout;

    daemon(1,1);


    struct sigaction sa;
    bzero(&sa, sizeof(sa));
    sa.sa_sigaction = childDieHandler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_SIGINFO;
    sigaction(SIGCHLD, &sa, NULL);

    startProcess("parent");

    while(true) {
        sleep(60);
    }

    return 0;
}

void startProcess(string processFile)
{
    fprintf(logFile, "\nGP:Starting new process %s\n",processFile.c_str());
    // Get process field and start a new process via fork + execl
    int pid = fork();
    if (pid == -1){
        fprintf(logFile,"GP:*** FORK ERROR on process %s !!!\n",processFile.c_str());
        fflush(logFile);
        return;
    }

    // New child process
    if (pid == 0) {

        string execString = get_current_dir_name()+(string)"/"+processFile;
        fprintf(logFile, "GP: %s \n",execString.c_str());

    execl(execString.c_str(), processFile.c_str(), NULL);

        fprintf(logFile, "GP:*** ERROR on execv for process %s\n",processFile.c_str());
        fflush(logFile);
        exit(1);
    } else {
        // Parent process
        fprintf(logFile, "GP:New process %s pid is %d .\n", processFile.c_str(), pid);
        fflush(logFile);
    currentChildPid = pid;
        sleep(2);
    }
}

// Intercept a signal SIGCHLD
void childDieHandler(int sig, siginfo_t *child_info, void *context){
    int status;
    pid_t childPid;
    while((childPid = waitpid(-1,&status, WNOHANG)) > 0) {
        int pid = (int) childPid;
        fprintf(logFile,"GP:*** PROCESS KILLED [pid %d]\n",pid);

    sigset_t set;
    sigpending(&set);
    if(sigismember(&set, SIGCHLD)){
        fprintf(logFile, "GP: SIGCHLD is pending or blocked!!!!\n");
        fflush(logFile);
    }

        fflush(logFile);

        // identify exited process and then restart it
        if(currentChildPid == childPid){
        // kill any child 
        system("killall child");
        fprintf(logFile,"GP: Restarting parent process...\n");
        fflush(logFile);
        startProcess("parent");
    }

    }

    fprintf(logFile,"GP:End of childDieHandler()... [%d]\n\n",(int)childPid);
    fflush(logFile);
}

親.cpp

#include <cstdio>
#include <string>
#include <cstring>

#include <stdlib.h>
#include <signal.h>
#include <wait.h>

using namespace std;

void startProcess(string processFile);
void childDieHandler(int sig, siginfo_t *child_info, void *context);

FILE            *logFile;
int         currentChildPid;

int main(int argc, char** argv)
{
    currentChildPid = 0;
    logFile = stdout;

    struct sigaction sa;
    bzero(&sa, sizeof(sa));
    sa.sa_sigaction = childDieHandler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_SIGINFO;
    sigaction(SIGCHLD, &sa, NULL);

    startProcess("child");

    while(true) {
        sleep(60);
    }

    return 0;
}

void startProcess(string processFile)
{
    fprintf(logFile, "\nP : Starting new process %s\n",processFile.c_str());
    // Get process field and start a new process via fork + execl
    int pid = fork();
    if (pid == -1){
        fprintf(logFile,"P : *** FORK ERROR on process %s !!!\n",processFile.c_str());
        fflush(logFile);
        return;
    }

    // New child process
    if (pid == 0) {
    string execString = get_current_dir_name()+(string)"/"+processFile;
        execl(execString.c_str(), processFile.c_str(), NULL);

        fprintf(logFile, "P : *** ERROR on execv for process %s\n",processFile.c_str());
        fflush(logFile);
        exit(1);
    } else {
        // Parent process
        fprintf(logFile, "P : New process %s pid is %d .\n", processFile.c_str(), pid);
        fflush(logFile);
    currentChildPid = pid;
        sleep(2);
    }
}

// Intercept a signal SIGCHLD
void childDieHandler(int sig, siginfo_t *child_info, void *context){
    int status;
    pid_t childPid;
    while((childPid = waitpid(-1,&status, WNOHANG)) > 0) {
        int pid = (int) childPid;
        fprintf(logFile,"P : *** PROCESS KILLED [pid %d]\n",pid);

    sigset_t set;
    sigpending(&set);
    if(sigismember(&set, SIGCHLD)){
        fprintf(logFile, "P : SIGCHLD is pending or blocked!!!!\n");
        fflush(logFile);
    }

        fflush(logFile);

    // identify exited process and then restart it
    if(currentChildPid == childPid){
        fprintf(logFile,"P :  Restarting child process...\n");
        fflush(logFile);
        startProcess("child");
    }

    }

    fprintf(logFile,"P : End of childDieHandler()... [%d]\n\n",(int)childPid);
    fflush(logFile);
}

子.cpp

#include <cstdio>
#include <string>
#include <cstring>

int main(int argc, char** argv)
{
    printf("\nC : I'm born...\n\n");

    while(true) {
        sleep(60);
    }

    return 0;
}
4

1 に答える 1

2

まあ、私は推測があります...

シグナルハンドラー内では、SIGCHLDシグナルはブロックされます(つまり、プロセスのシグナルマスクのメンバーです)。

したがって、祖父母がシグナルハンドラー内から呼び出すと、新しい親はブロックされた状態execlで起動します。SIGCHLDしたがって、シグナルを確認したり、新しい子を待つことはありません。

sigprocmask(a)この理論を検証し、(b)SIGCHLDのブロックを解除するために、parent.cppの先頭で呼び出してみてください。

于 2011-10-03T15:46:56.887 に答える