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