this_process


The os_this_process class is comprised of a wide set of static member functions for controlling the current process. You can neither create nor destroy instances of this class.

Functions available to the os_this_process class make use of the following classes.

All of these classes are discussed in more detail in this chapter.

Examples in this section illustrate a variety of uses for the this_process class.

Accessing Basic Process Attributes

The following example gathers information about the current process, including the group, priority, and effective user of this process.

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

int
main()
  {
  os_unix_toolkit initialize;

  cout << "os_this_process:" << endl;
  cout << "  pid:           " << os_this_process::pid() << endl;
  cout << "  parent pid:    " << os_this_process::parent() << endl;
  cout << "  process group: " << os_this_process::process_group();
  cout << endl;
  cout << "  umask:         " << os_this_process::umask() << endl;
  cout << "  priority:      " << os_this_process::priority() << endl;
  cout << "  current working directory: " << endl;
  cout << "      " << os_this_process::working_directory() << endl;
  cout << "  uid:           " << os_this_process::user() << endl;
  cout << "  effective uid: " << os_this_process::effective_user();
  cout << endl;
  cout << "  gid:           " << os_this_process::group() << endl;
  cout << "  effective gid: " << os_this_process::effective_group();
  cout << endl;
  return 0;
  }

os_this_process:
  pid:           3737
  parent pid:    3218
  process group: 3737
  umask:         2
  priority:      0
  current working directory:
      /xray/users/fred/toolkit/ospace/unix/examples/
  uid:           105
  effective uid: 105
  gid:           21
  effective gid: 21
  

Executing a Utility in a Subshell

The following example executes a utility in a subshell. The system() function executes the date utility in a subshell, sleeps for two seconds, and then executes the date utility again.

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

int
main()
  {
  os_unix_toolkit initialize;

  os_this_process::system( "date" );
  cout << "sleep( 2 )..." << endl;
  os_this_process::sleep( 2 );
  os_this_process::system( "date" );
  return 0;
  }

Wed Sep 27 13:09:23 CDT 1995
sleep( 2 )...
Wed Sep 27 13:09:25 CDT 1995

Changing the Working Directory

To change the working directory for the current process, use either cd() or working_directory() . The following example uses both of these functions.

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

int
main()
  {
  os_file_toolkit init_file;
  os_unix_toolkit init_unix;

  os_path path = os_this_process::working_directory();
  cout << "working directory: " << endl << "  " << path << endl;
  cout << "cd (\"..\")" << endl; // Move up one level.
  os_this_process::cd( ".." );
  cout << "working directory: " << endl;
  cout << "  " << os_this_process::working_directory() << endl;
  cout << "cd (\"" << path << "\")" << endl;
  os_this_process::cd( path ); // Move back to original directory.
  cout << "working directory: " << endl;
  cout << "  " << os_this_process::working_directory() << endl;
  return 0;
  }

working directory:
  /usr/alpha/home/osi/toolkit/ospace/unix/examples/
cd ("..")
working directory:
  /usr/alpha/home/osi/toolkit/ospace/unix/
cd ("/usr/alpha/home/osi/toolkit/ospace/unix/examples/")
working directory:
  /usr/alpha/home/osi/toolkit/ospace/unix/examples/
  

Accessing and Modifying a Process umask

When a process creates a file, the creation mode request is preprocessed by removing all the bits in the umask of the process. To display the UNIX online help information about umask , and how shells support umask , type man umask at the command prompt.

In the following example, file1.txt is created with the default umask -------w- , and file2.txt is created with umask ----w--w- . The second umask value inhibits group write permission.

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

int
main()
  {
  os_file_toolkit init_file;
  os_unix_toolkit init_unix;

  os_mode umask = os_this_process::umask(); // Get current umask.
  cout << "umask = " << umask << endl;

  cout << "Create file1.txt with mode rw-rw-rw-" << endl;
  os_file file1( "file1.txt", O_CREAT, O_RDONLY, 0666 );
  os_this_process::system( "ls -l file1.txt" );

  umask.group_write( true );
  os_this_process::umask( umask ); // Set new umask.
  cout << "umask = " << umask << endl;
  cout << "Create file2.txt with mode rw-rw-rw-" << endl;
  os_file file2( "file2.txt", O_CREAT, O_RDONLY, 0666 );
  os_this_process::system( "ls -l file2.txt" );

  os_file_system::remove( "file1.txt" );
  os_file_system::remove( "file2.txt" );
  return 0;
  }

umask = --------w-
Create file1.txt with mode rw-rw-rw-
-rw-rw-r--  1 osi             0 Oct 26 01:26 file1.txt
umask = -----w--w-
Create file2.txt with mode rw-rw-rw-
-rw-r--r--  1 osi             0 Oct 26 01:26 file2.txt

Accessing and Modifying User and Group IDs

A process with the appropriate privileges can change its effective user and group IDs. This technique is used by the login process to convert the current user and group IDs to those of the user logging in.

The following example is executed by root and demonstrates effective_user() and effective_group() . If you want to run this example on your own system, replace the variable fred with a valid user name, and replace the variable cavemen with a valid group name.

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

void
display()
  {
  cout
    << "real user = " << os_this_process::real_user() << endl
    << "effective user = " << os_this_process::effective_user() << endl
    << "real group = " << os_this_process::real_group() << endl
    << "effective group = " << os_this_process::effective_group();
    << endl;
  }

int
main()
  {
  os_security_toolkit init_security;
  os_unix_toolkit init_unix;

  display();

  cout << endl << "set effective group to \"cavemen\"" << endl;
  os_this_process::effective_group( os_group( "cavemen" ) );

  cout << "set effective user to \"fred\"" << endl << endl;
  os_this_process::effective_user( os_user( "fred" ) );

  display();
  return 0;
  }

real user = 0
effective user = 0
real group = 1
effective group = 1

set effective group to "cavemen"
set effective user to "fred"

real user = 0
effective user = 100
real group = 1
effective group = 21

Terminating or Changing the Priority of the Current Process

To terminate a current process, use exit() or _exit() . Both of these functions invoke the standard shutdown sequence, which includes the following actions.

In addition, exit() causes the standard I/O library to shut down correctly. For an example of exit() , see process_status .

To access and modify the nice value of the current process, use priority() . Valid nice values range from -20 to +20, with positive values running at a lower priority than normal. Only a process with superuser privileges can specify a negative nice value.

In the following example, the priority of the current process is lowered from zero to three.

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

int
main()
  {
  os_unix_toolkit initialize;

  cout << "My priority is " << os_this_process::priority() << endl;
  os_this_process::priority( 3 ); // Run at a lower priority.
  cout << "My priority is " << os_this_process::priority() << endl;
  return 0;
  }

My priority is 0
My priority is 3

Accessing and Modifying Supplemental Groups

The priorities of all processes associated with a particular user or process group are manipulated by similar functions. To access and modify the supplemental groups of the current process, use the supplemental_groups() function. On most systems, only processes with superuser privileges can change supplemental groups. The following example runs in superuser mode.

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

int
main()
  {
  os_security_toolkit init_security;
  os_unix_toolkit init_unix;

  os_group group = os_this_process::group();
  cout << "group = " << group << endl; // Display group.
  vector< gid_t > groups = os_this_process::supplemental_groups();
  cout << "supplemental groups = ";
  vector< gid_t >::iterator i;
  for ( i = groups.begin(); i != groups.end(); i++ )
    cout << os_group( *i ) << " ";
  cout << endl;
  groups.push_back( os_group( "cavemen" ) ); // Add a new group.

  // Update supplementals.
  os_this_process::supplemental_groups( groups );

  // Get new supplementals.
  groups = os_this_process::supplemental_groups();
  cout << "supplemental groups = ";
  for ( i = groups.begin(); i != groups.end(); i++ )
    cout << os_group( *i ) << " ";
  cout << endl;
  return 0;
  }

group = os_group( daemon, 1 )
supplemental groups = os_group( daemon, 1 )
supplemental groups = os_group( daemon, 1 ) os_group( cavemen, 21 )

Accessing and Modifying the Environment

Every process has an associated environment that is a set of variable/value pairs. Common environment variables include PATH and HOME . A child process usually inherits its environment from the parent process.

To access or change a map of the environment for the current process, use the environment() function. The following example sets the existing PATH variable to a new value, removes the HISTFILE variable, and adds a new MUSIC variable.

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

void display
  (
  os_environment::const_iterator begin,
  os_environment::const_iterator end
  )
  {
  os_environment::const_iterator i;
  for ( i = begin; i != end; i++ )
    cout << "  " << (*i).first << " = " << (*i).second << endl;
  }

int
main()
  {
  os_unix_toolkit initialize;

  // Get old environment.
  os_environment env = os_this_process::environment();
  cout << "Old environment: " << endl;
  display( env.begin(), env.end() );

  env[ "PATH" ] += ":/work/bin"; // Append to current PATH.
  env.erase( "HISTFILE" ); // Remove HISTFILE.
  env[ "MUSIC" ] = "ELEMENTAL"; // Add association with MUSIC.

  os_this_process::environment( env ); // Change environment.
  env = os_this_process::environment(); // Get new environment.

  cout << "New environment: " << endl;
  display( env.begin(), env.end() );
  return 0;
  }

Old environment:
  HOSTTYPE = sparc
  HISTFILE = /nestegg0/users/tarrman/.history
  ...
  PATH = /usr/ucb:/usr/bin:/usr/etc
  ...
New environment:
  HOSTTYPE = sparc
  ...
  MUSIC = ELEMENTAL
  ...
  PATH = /usr/ucb:/usr/bin:/usr/etc:/work/bin
  

Executing Another Program in the Current Process

The current process can execute another program by using one of the execute() family of functions. The next four examples execute another program with the execute() function in one of the following ways.

The following example overlays the current process with a shell that executes the specified command.

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

int
main()
  {
  os_unix_toolkit initialize;

  // Overlay current process with a shell executing the "date" utility.
  os_this_process::execute( "/bin/sh", "date" );
  cout << "THIS LINE SHOULD NEVER EXECUTE" << endl;
  return 0;
  }

Wed Sep 27 14:04:14 CDT 1995

The following example executes a program using a null-terminated list of arguments.

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

int
main()
  {
  os_unix_toolkit initialize;

  // Spawn child using null terminated argument list.
  os_this_process::execute( "date", "-u", 0 );
  cout << "THIS LINE SHOULD NEVER BE EXECUTED" << endl;
  return 0;
  }

Wed Sep 27 19:04:36 GMT 1995

The following example executes a program using a null-terminated argument array.

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

int
main()
  {
  os_unix_toolkit initialize;

  char* argv[ 3 ];
  argv[ 0 ] = "date"; // Name of program.
  argv[ 1 ] = "-u"; // Argument causes GMT to be displayed.
  argv[ 2 ] = 0; // Null terminate the argument vector.

  // Supply name of program + argument vector.
  os_this_process::execute( "date", argv );
  cout << "THIS LINE SHOULD NEVER BE EXECUTED" << endl;
  return 0;
  }

Wed Sep 27 19:04:56 GMT 1995

Programmers use command shells that can execute a command with either I/O redirection or I/O piping. You can use the execute() function to execute a command using the following arguments.

The I/O objects can be files, sockets, or pipes, in any combination.

The following example illustrates direction of output to a file. The child process executes the date utility and sends the standard output and errors to the file exec4.out. Because all I/O objects include conversion operators that return file descriptors, you can specify an I/O object whenever a descriptor is required.

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

int
main()
  {
  os_file_toolkit init_file;
  os_unix_toolkit init_unix;

  os_file file( "exec4.out", O_CREAT | O_WRONLY );
  // Redirect standard output and error channels to file.
  os_this_process::execute( os_nil_env,os_nil_desc,file,file, "date" );
  return 0;
  }
  

Following is the content of output file exec4.out.

Wed Sep 27 14:05:18 CDT 1995

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