Hello,

I am working on a program for my operating systems class dealing with catching signals and exit status's. The long and short of the assignment is to have a parent create a child and then wait for the child to die. When the child dies the parent should capture its pid and its exit status. I haven't programmed in c++ in a long time and i think i have a good start but my parent process is not catching the signal. anyone have any suggestions. And i am using the command line arguments as well in order to do different operations depending on what the user commands but haven't gotten that far yet.

#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;

#include <unistd.h>

typedef void Sigfunc(int);
Sigfunc *signal (int, Sigfunc *);

static void childFuneral (int);
#include <stdlib.h>

void exit (int& status);
int main (int argc, char *argv[])
{
        int pid, parentID, status;

        parentID = getpid();
        pid = fork();

        if (pid < 0)
        {
                cout << "fork failed" << endl;
                return 1;
        }

        if (atoi(argv[1]) == 1)
        {
                if(pid > 0)
                {
                        /* parent*/
                        signal (SIGCHLD, childFuneral);
                }

                if (pid == 0)
                {
                        /* child*/
                        cout << "I am the child my pid is:" << endl;
                        cout << getpid() << endl;
                        cout << "My parents id is:" << endl;
                        cout << parentID << endl;
                        exit(1);
                }
        }
}

void childFuneral(int status)
{
        waitpid(0, &status, WNOHANG);
        if(WIFEXITED(status))
        {
                cout << "child exit status is = " << WEXITSTATUS(status) << endl;
        }
        else
        {
                cout << "child did not terminate normally" << endl;
        }
}

my output is below, but the parent is not outputting the child's exit status.


I am the child my pid is:
6650
My parents id is:
6649

:S Linux compiler?? Mingw?? I dont got the wait.h header nor the unistd header. Or else I'd compile it..

For anyone trying to help him, this is a linux system he is compiling on..

If you want to catch a signal delivered to a child process you are going to need to use something like ptrace. Otherwise the child handles are signals delivered to it.

If all you want is the exit status of the child then you can look at waitpid. It will, with a -1 as the pid argument, wait for any children to exit and yields the status of that child.

NOTE: You can determine how the child exited through the macros associated with that call and if it was a signal, you can read which signal it was. You can not catch the signal, however.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.