process_status


When the current process spawns a child process, the current process and the child process execute concurrently. If you want the current process to wait until one of its child processes terminates, use one of the wait() family of functions. The wait() function returns a process status object containing information about the child's termination. If the current process does not have any children, an os_unix_toolkit_error is generated.

The following example illustrates the use of the process_status class with the wait_for_child() function.

Example <ospace/unix/examples/prcstat1.cpp>
#include <iostream>
#include <ospace/unix.h>

int
main()
  {
  os_unix_toolkit initialize;

  os_process child( "date" );
  // Wait for my child.
  os_process_status status = os_this_process::wait_for_child( child );
  cout << status << endl;
  cout << "terminated_normally: " << status.terminated_normally();
  cout << endl;
  cout << "terminated_by_signal: " << status.terminated_by_signal();
  cout << endl;
  cout << "exit_code: " << status.exit_code() << endl;
  return 0;
  }

Thu Oct 26 01:49:32 CDT 1995
os_process_status( 11194 )
terminated_normally: 1
terminated_by_signal: 0
exit_code: 0

You can optionally use a bitwise OR flag as the second parameter to all the wait() functions. This can be either of the following flags.

< CLASS="ColumnListWidewDottedLine"> WNOHANG Return immediately if status of a child process is unavailable > < CLASS="ColumnListWidewDottedLine"> WUNTRACED In addition to terminated children, return the status of child processes stopped and not yet reported >

In the following example, the first call to wait_for_child() returns immediately with an undefined process status, because the child process has not stopped.

Example <ospace/unix/examples/prcstat2.cpp>
#include <iostream>
#include <ospace/unix.h>

int
main()
  {
  os_unix_toolkit initialize;

  os_process child( "sleep", "5", 0 );
  os_process_status status =
    os_this_process::wait_for_child( child, WNOHANG );
  cout << "Status of child: " << status << endl;
  status = os_this_process::wait_for_child( child );
  cout << "Status of child: " << status << endl;
  cout << "  terminated_normally: " << status.terminated_normally();
  cout << endl;
  os_process child2( "sleep", "5", 0 );
  os_process child3( "sleep", "5", 0 );
  child2.terminate(); // Terminate child2.
  cout << endl << "Waiting for all children..." << endl << endl;
  try
    {
    while ( true )
      {
      status = os_this_process::wait_for_any_child();
      cout << "Status of child: " << status << endl;
      cout << "  terminated_normally: " << status.terminated_normally();
      cout << endl;
      cout << "  terminated_by_signal: "
        << status.terminated_by_signal();
      cout << endl;
      cout << "  exit_code: " << (int) status << endl;
      cout << "  signal: " << status.signal() << endl;
      }
    }
  catch ( os_unix_toolkit_error& )
    {
    // No more children left.
    }
  return 0;
  }
Status of child: os_process_status( undefined )
Status of child: os_process_status( 10644 )
  terminated_normally: 1

Waiting for all children...

Status of child: os_process_status( 10645 )
  terminated_normally: 0
  terminated_by_signal: 1
  exit_code: 0
  signal: os_signal( 15, Terminated )
Status of child: os_process_status( 10646 )
  terminated_normally: 1
  terminated_by_signal: 0
  exit_code: 0
  signal: os_signal( undefined )

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.