The Process Tree (And Environment, And procfs)

Process ID (PID) And Parent Process ID (PPID)

  • Every process has an ID (PID … Process ID)

  • Every process but the first (PID 1) has a parent process)

  • ⟶ process tree

../../../../../../_images/process-tree.svg

System Calls: getpid(), getppid()

#include <unistd.h>

#include <iostream>
using namespace std;

int main(int argc, char** argv)
{
    cout << "PID: " << getpid() << endl;               // <--- self's PID
    cout << "PPID: " << getppid() << endl;             // <--- parent's PID

    return 0;
}

Environment Variables

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

#include <iostream>
using namespace std;

int main(int argc, char** argv)
{
    const char* path = getenv("PATH");                 // <--- PATH environment variable
    const char* home = getenv("HOME");                 // <--- HOME environment variable
    const char* user = getenv("USER");                 // <--- USER environment variable
    const char* customvar = getenv("CUSTOMVAR");       // <--- CUSTOMVAR environment variable

    cout << "PATH: " << path << endl;
    cout << "HOME: " << home << endl;
    cout << "USER: " << user << endl;
    if (customvar != NULL)
        cout << "CUSTOMVAR: " << customvar << endl;
    else
        cout << "CUSTOMVAR not set" << endl;

    return 0;
}