// testwait.cpp

// Create a child process and have the parent wait
// for its exit status and report it to the console.
// Note: wait() will return when ANY child exits.
// If the parent is to wait until a particular child
// terminates, then use waitpid().

// Author: Dr. Jeff Blessing
// Date:   November 28, 2000

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

int main()
{
   pid_t child_pid;
   int   status;

   switch (child_pid = fork())
   {
      case (pid_t) -1:	perror("fork");		// fork failed!
			break;
      case (pid_t)  0:	cout << "Child process created" << endl;
			exit(15);
      default:	cout << "Parent process after fork()" << endl;
		wait(&status);
   }

   if (WIFEXITED(status))
      cout << "Parent reports child exited with status = "
	   << WEXITSTATUS(status) << endl;
   else if (WIFSTOPPED(status))
	   cerr << "Parent reports child stopped by signal #"
		<< WSTOPSIG(status) << endl;
   else if (WIFSIGNALED(status))
	   cerr << "Parent reports child killed by uncaught signal #"
		<< WTERMSIG(status) << endl;
   else perror("wait");		// wait failed!
   return 0;
}
