Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6661601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:18:01+00:00 2026-05-26T02:18:01+00:00

I am trying to create a process that manage some other process in the

  • 0

I am trying to create a process that manage some other process in the way that if a child die then the parent restart the process and the process that depend from it.

The problem is that I notice that if I create a tree structure of process when I restart a process in the middle of this structure I am unable to be signaled when new child process terminates.

I write an example; suppose that we have 3 process, grandparent, parent and child.
Grandparent fork and start parent that fork and start child (I put the code at the end of this post). Now if I kill child everything works well, child is restarted correctly.

The problem occurs if I kill parent… The grandparent restart parent that restart child, but if I kill child the process remain in the Zombie state and the SIGCHLD is not delivered to the parent process.

In other words:

  • Start grandparent process and wait that all 3 processes have been up
  • Kill parent process and wait that grandparent restart parent that restart child
  • now kill child process, the process remain in the zombie state.

I’m not able to understand this behavior… I have read a tons of example and documentation about signal and wait, try to reset default handler before the fork in parent and grandparent, but nothing seem to work…
Here is the code sample…

grandparent.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);
}

parent.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);
}

child.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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:18:02+00:00Added an answer on May 26, 2026 at 2:18 am

    Well, I have a guess…

    Inside the signal handler, the SIGCHLD signal is blocked (i.e., it is a member of the process’s signal mask).

    So when the grandparent calls execl from inside the signal handler, the new parent starts up with SIGCHLD blocked. Thus it never sees the signal and never waits for the new child.

    Try calling sigprocmask at the beginning of parent.cpp in order to (a) verify this theory and (b) unblock SIGCHLD.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a service with C# that launches a process that
Lets say that I'm trying to create a new process with the following code:
I'm trying to create a child process in another process. I am writing both
I'm trying to create a process to automatically generate thumbnails of the images uploaded
I'm trying to create an in-process unit test for my service to client interactions
As part of our build process and evolving database, I'm trying to create a
I am trying create a WCF service that leverages the WPF MediaPlayer on the
I'm trying to create a performance counter that can monitor the performance time of
I'm trying to start SQLServer2008SP1 express installer from the NT service process. Process is
I'm trying to make a Windows app that checks some things in the background,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.