FBB::Process(3bobcat)

Running Child Processes
(libbobcat-dev_6.04.00)

2005-2023

NAME

FBB::Process - Runs external programs

SYNOPSIS

#include <bobcat/process>
Linking option: -lbobcat

DESCRIPTION

The FBB::Process class offers an extensive interface to calling external programs and/or scripts from a C++ program (so-called child-processes). The class offers an easy to use, stream-based interface to the standard input, standard output and standard error streams of child processes.

Objects of the class Process use standard process-executing functions, like members of the execl(2) family or sh(1) to execute child processes. Thus, child processes can be executable programs or shell-scripts.

The standard input, output and error streams of child processes may be accessed through their Process parent objects. Input expected by child processes may be inserted by Process objects, and output generated by child processes may be extracted from Process objects.

When using (output) redirection with the USE_SHELL path specification (see below for the path and IOMode specifications), the IGNORE_COUT IOMode (and possibly IGNORE_CERR) should normally be specified.

Process objects may repeatedly be used to execute the same or different child processes. Before the next child process is started, the Process object first terminates its currently active child process. Alternatively, a currently active child process is automatically ended if the Process object goes out of scope, if its stop or eoi (end-of-information) member is called, or if the eoi manipulator is inserted into the Process object.

Programs called as child processes may be specified when constructing a Process object or by using Process's setCommand member. Process constructors (or Process set-members) never start child processes. Child processes are started through start members or the assignment operator.

Child processes may receive information at their standard input streams through information inserted into Process objects. In these cases the Process objects must inform their child processes that they have received all input. For this the close or eoi member or the eoi manipulator can be used. After calling the close member, the waitForChild member should be called as well. This is not necessary if either the eoi member or the eoi manipulator is used.

If waitForChild is not called (but information sent to the child which could not be fully processed by the child process in case the child process terminated as a result of the Process object going out of scope), then the operating system issues a Broken pipe message, indicating that information in a pipe was lost.

Arguments passed to child processes may be surrounded by double or single quotes. Arguments surrounded by double quotes have their double quotes removed, while interpreting any escape-sequences that may have been used within. Arguments surrounded by single quotes have their single quotes removed, while accepting their content as-is. In addition unquoted escape-sequences may be specified: those escape sequences are evaluated and replaced by their intended characters (e.g., \100 is converted to @).

A full command specification may be surrounded by backtics (`-characters). These backtick characters are removed by the Process object when the command is started.

Child processes may be allowed a limited amount of time (in seconds) to complete. By default no time limit is imposed upon child processes.

By default the standard input, output and error streams of child processes are accessed through their Process parent processes: information inserted into the Process object is forwarded to the child process's standard input stream, information sent by the child process to its standard output stream can be extracted from its parent Process object, and information sent by the child process to its standard error stream may be obtained through Process's childErrStream member.

If the parent and child processes have agreed on some communication process, then information may alternatingly be sent to and received from the child process through the Process's ostream and istream facilities. Alternatively, unspecified amounts of information written by child processes may be processed by separate threads (cf. this manual page's EXAMPLES section).

Process objects use Pipe objects (cf. pipe(3bobcat)) for communication with its child processes. To ensure that these pipes are properly closed the members waitForChild, stop or the eoi manipulator should be used. Once a Process object ceases to exist pipes to its child process are also closed.

NAMESPACE

FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.

INHERITS FROM

FBB::Fork(3bobcat) (private),
FBB:IOStream(3bobcat), and by implication: FBB::Eoi,
FBB:ProcessEnums

The struct ProcessEnums defines enumerations and support functions which are used by several classes. Its enumerations are documented below; there is no separate ProcessEnums man-page.

ENUMERATIONS

enum ProcessType:

The enum ProcessType defines how a child process is started or located. Its values are specified at constructor-time or through the setProcessType member. This enumeration defines the following symbolic constants:

enum IOMode:

Values of the enum IOMode are used to define which of the child process's standard streams can be accessed through the Process object. Its symbolic constants may be combined using the bit_or operator. By default CIN | COUT | CERR is used (see below).

The following symbolic constants are available:

enum ChildOutput:
The ChildOutput enumeration defines values returned by the available member (see below) indicating to which standard stream the child process has written information. This enumeration defines the following values:

The latter two values may be combined using the bit_or operator. The bit_and operator,returning a bool value can be used to test whether information on a specific output stream is available.

PROCESS PARAMETERS

Four process parameters may be specified: the sizes of the stream buffers which are used when communicating with child processes; to specify which of the standard streams of child processes can be accessed from the Process object combinations of IOMode values are used; to specify how child programs are found a ProcessType value is used; to specify the maximum time (in seconds) the child program is allowed to run a size_t values is used.

By default, the stream buffers hold 200 bytes; all the child's standard streams (standard input, output and error) are accessible from the Parent process; the PATH environment variable is not used to locate the child program; and the child processes will be allowed an unlimited amount of time to run.

After constructing a Process object all default parameters may be modified. These parameters may either be altered for a single process or a Process object's general defaults may be modified. The set* members (see below) may be used to change the default process parameters. When parameters are specified otherwise, they will only be active for the next process.

CONSTRUCTORS

The command provided to the following constructors may be the (initial part of the) specification of an external program to run. When the program is eventually started it may start and end with a back-tick (`). The back-ticks will be removed just before the specified program is executed.

Child processes are not started automatically following Process object constructions. A start member or the assignment operator (see below) is used to start the specified child process.

Constructors expecting an IOMode argument may be provided with multiple IOMode values by combining them using the bit-or operator.

After constructing a Process object its parameters can be changed using set-member functions, function call operators or start members.

Copy and move constructors (and assignment operators) are not available.

OVERLOADED OPERATORS

MEMBERS

MANIPULATOR

EXAMPLES

The first example shows how a program only producing output can be called. Its child process simply is /bin/ls:

int main()
{
    Process process(Process::COUT, "/bin/ls -Fla");

    process.start();
    cout << process.childOutStream().rdbuf();
}

The next example shows how a child program can be given a limited amount of execution time: lines entered at the keyboard are echoed to the standard output stream for at most 5 seconds:

int main()
{
    Process process(Process::CIN | Process::COUT, "/bin/cat");
    process.setTimeLimit(5);

    process.start();

    while (true)
    {
        cout << "? ";
        string line;
        if (not getline(cin, line))
            return 0;

        process << line << endl;           // to /bin/cat
        line.clear();

        if (not getline(process, line))    // from /bin/cat
            break;

        cout << "Received: " << line << endl;
    }

    cout << "/bin/cat time limit of 5 seconds reached: child process ended\n";
}

The final example shows how multi threading can be used to access the child program's standard output and standard error streams through the Process object:

void collect(ostream *outStream, streambuf *rdbuf)
{
    *outStream << rdbuf << flush;
}

int main()
{
    string cmd(getcwd(0, 0));
    cmd += "/cincoutcerr";

    Process all(Process::ALL, cmd);

    all.start();

    thread outThread(collect, &cout, all.childOutStream().rdbuf());
    thread errThread(collect, &cerr, all.childErrStream().rdbuf());

    all << cin.rdbuf() << eoi;

    outThread.join();
    errThread.join();
}

Additional examples are found in the distribution's bobcat/process/driver directory.

FILES

bobcat/process - defines the class interface

SEE ALSO

bobcat(7), execle(3), exec(3bobcat), coutextractor(3bobcat), cerrextractor(3bobcat), fork(3bobcat), cininserter(3bobcat), proc(3bobcat), sh(1), stdextractor(3bobcat).

BUGS

None reported

BOBCAT PROJECT FILES

BOBCAT

Bobcat is an acronym of `Brokken's Own Base Classes And Templates'.

COPYRIGHT

This is free software, distributed under the terms of the GNU General Public License (GPL).

AUTHOR

Frank B. Brokken (f.b.brokken@rug.nl).