Skip to content

process

Alairion edited this page May 8, 2021 · 11 revisions

nes::process

Defined in header <nes/process.hpp>

class process;

Description

nes::process represents a process within the system. This class is used in order to create child processes.

To create a child process you have to give its path to the nes::process's constructor. You can give arguments to the created process, and also set a specific working directory for it. There are also additional options represented by nes::process_options enum.

Once launched, a process represented by nes::process must be joined, detached or killed before the destruction of the nes::process instance.

Member types

Type Description
native_handle_type The underlying, implementation-defined, representation of a process
return_code_type An integral type representing the return code of a process
id A trivially copyable type that is a unique identifier for a process

Public Member functions

Function Description
process Constructs the process object
~process Destroys the process object
operator= Assigns a new handle to the process object
join Waits for process termination
joinable Checks if the process is joinable
active Checks if the process is still running (a non-active process can be joinable)
detach Permits the process to execute independently from the process instance
kill Forces the process to end
return_code Returns the return code of the process after a call to join or kill
native_handle Returns the underlying, implementation-defined, representation of the process
get_id Returns the unique identifier for the process
stdin_stream Returns the pipe linked to the process's standard input
stdout_stream Returns the pipe linked to the process's standard output
stderr_stream Returns the pipe linked to the process's standard error

Example

Here is an example in which we create a child process.
main.cpp is the main file of the parent process.
other.cpp is the main file of the child process.
Output is the standard output of the parent process.

main.cpp

#include <iostream>

#include <nes/process.hpp>

int main()
{
    //nes::this_process namespace can be used to modify current process or get informations about it.
    std::cout << "Current process has id " << nes::this_process::get_id() << std::endl; 
    std::cout << "Its current directory is \"" << nes::this_process::working_directory() << "\"" << std::endl;

    //Create a child process
    nes::process other{"other_process", {"Hey!", "\\\"12\"\"\\\\", "\\42\\", "It's \"me\"!"}, nes::process_options::grab_stdout};
    
    //Read the entire standard output of the child process. (nes::process_options::grab_stdout must be specified on process creation)
    std::cout << other.stdout_stream().rdbuf() << std::endl;

    //As a std::thread, a nes::process must be joined if it is not detached.
    if(other.joinable())
        other.join();

    //Once joined, we can check its return code.
    std::cout << "Other process ended with code: " << other.return_code() << std::endl;
}

other.cpp

#include <iostream>

#include <nes/process.hpp>

int main(int argc, char** argv)
{
    //Display some informations about this process
    std::cout << "Hello world! I'm Other!\n";
    std::cout << "You gaved me " << argc << " arguments:";
    for(int i{}; i < argc; ++i)
        std::cout << "[" << argv[i] << "] ";
    std::cout << '\n';
    std::cout << "My working directory is \"" << nes::this_process::working_directory() << "\"" << std::endl;
}

Output

Current process has id 3612
Its current directory is "/..."
Hello world! I'm Other!
You gaved me 5 arguments:[not_enough_standards_other.exe] [Hey!] [\"12""\\\] [\42\] [It's "me"!] 
My working directory is "/..."

Other process ended with code: 0
Clone this wiki locally