process_group


Processes can be conveniently grouped together using a process group. Each process group has a unique numeric identifier called the process group ID. Each process belongs to a single process group, and a child process inherits its parent's process group.

One process in each process group is called the group leader. In most cases, all additional processes in a process group are descendants of the process group leader. The process ID and process group ID are the same value for the group leader process.

You can send a signal to every process in a process group using the signal() function defined in os_process_group . Similarly, the current process can wait for any child in a particular process group using os_this_process::wait_for_any_child_in() .

When sending signals to process groups, permissions affect whether the signal is actually received. For the signal to be received, the caller's user ID or effective user ID must match the effective or saved user ID of the receiving process. The single exception to this rule is resume() , which can always be sent to all members of the calling process' session.

If the caller has an effective user ID of superuser, all processes in the process group receive the signal.

In the following example, the parent process spawns three children. The third child changes its process group by becoming the leader of a new process group. When the parent sends the SIGTERM signal to every member of the parent's process group, the third child is unaffected.

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

int
function( int argc, const char* argv[] )
  {
  if ( string( argv[ 0 ] ) == "YES" )
    // Become the leader of a new process group.
    os_this_process::become_process_group_leader ();
  for ( int i = 1; i <= 3; i++ ) // Loop three times.
    {
    cout << "pid " << os_this_process::pid()
      << ", process group "
      << os_this_process::process_group() << endl;
    os_this_process::sleep( 3 );
    }
  return 0;
  }

int
main()
  {
  os_unix_toolkit initialize;

  os_process_group process_group = os_this_process::process_group();
  cout << "Process group of parent = " << process_group << endl;

  // Spawn three children.
  os_process child1( function, "NO" , 0 ); // Inherits process group.
  os_process child2( function, "NO" , 0 ); // Inherits process group.
  os_process child3( function, "YES" , 0 ); // Gets new process group.

  os_this_process::sleep( 2 );
  // Kill all processes in my process group, including myself.
  os_process_group( os_this_process::process_group() ).terminate();

  cout << "THIS LINE SHOULD NEVER EXECUTE" << endl;
  return 0;
  }

Process group of parent = os_process_group( 406 )
pid 407, process group 406
pid 408, process group 406
pid 409, process group 409
Terminated
pid 409, process group 409
pid 409, process group 409

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