D.J.A.
D.J.A.

Reputation: 75

How can a parent-process find out if the child-process was terminated?

I want to write a program in which the parent creates exactly 1 child process. The child process should print its pid to the standard output and then finish. The parent process should waits until it is sure that the child has terminated. The parent terminates after it has waited for the child process.

Thats what I got so far:

#include <unistd.h>     
#include <stdlib.h> 
#include <stdio.h>

int main(void) 
{ 
    int child;
    child = fork();
    if (child == 0)
        {
            printf("Child process has PID: %d. \n", getpid());
            exit(0);    
        }

        // how can the parent process find out it the child process was terminated successful? 

        printf("Child process terminated successfully\n");
        return EXIT_SUCCESS;

} 


How can the parent-process find out if the child process was terminated? I can't use wait() or waitpid() in this programm.

Thanks for your help!

Upvotes: 2

Views: 5686

Answers (2)

bruno
bruno

Reputation: 32596

As I said in remark use the signal SIGCHLD, for instance :

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

void handler(int sig)
{
  pid_t chpid = wait(NULL);

  /* WARNING : to show the call of the handler, do not do that
     in a 'real' code, we are in a handler of a signal */
  printf("Child pid %d ended (signal %s)\n", chpid, sig); 

  /* does 'something' to allow the parent to know chpid 
     terminated in a way compatible with parent requirement */
}

int main(void)
{
  signal(SIGCHLD, handler);

  if (!fork())
  {
    printf("Child pid is %d\n", getpid());
    sleep(1);
    return 0;
  }
  printf("Parent pid is %d\n", getpid());
  getchar();

  return 0;
}

Note when the signal arrive you have to call wait(NULL) but because by definition the child terminated the function returns immediately

Compilation and execution :

pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra s.c
pi@raspberrypi:/tmp $ ./a.out
Parent pid is 21743
Child pid is 21744
Child pid 21744 ended (signal 17)
<enter>
pi@raspberrypi:/tmp $ 

Of course the signal 17 is SIGCHLD because it is the only one the program catches

Upvotes: 1

Jackson
Jackson

Reputation: 5657

When a child process terminates a SIGCHLD signal will be sent to the parent, by default the parent will ignore the SIGCHLD, however you can register a signal handler that will catch it.

You need to be careful what you do in the signal handler - quite a few standard function aren't safe to use.

The SIGCHLD approach turns up in code when the parent has it's own work to do and can't just wait for the child. If the parent just spawns children and then waits for them to finish the wait() and waitpid() are the best solution.

Finally if you don't call wait() or waitpid() you risk creating a zombie process, the child process expects it's parent to receive it's exit status through a call to one of these functions.

Upvotes: 3

Related Questions