thank me later

Identifiers ( POSIX / Linux Credentials )

Process Identifiers

Every process has an identifier, the process id or PID.

The first process spawned by the system (in my case systemd) has the PID set to 1.

The PID of a child process is assigned the moment fork() is called by the parent.

PIDs are integers represented by the c type pid_t (cf. <sys/types.h>).

Every process has actually many identifiers of type pid_t.

User and Group Identifiers

There is also a notion of user id and group id of a process.

These identifiers are represented by the c types uid_t and gid_t.

When a child process is created by fork(), it inherits copies of the parent's user and groups IDs.

Just like for process identifiers, a process has many identifiers of type uid_t and gid_t.

Retriving Identifiers

Process ID (PID)

This the main one I just talked you about. From a c program, you can use getpid() to retrive it.

Parent Process ID (PPID)

You can also retrive the PID of the parent process calling getppid().

Process Groups (PGRP) (aka Jobs)

The shell assigns a new process group (id) or job for every single command or pipeline it spawns.

A process group membership can be set using setpgid(pid, pgrp).

When a child process is created using fork(), it inherits its parent group id.

Session ID (SID)

A session is a collection of jobs. Child processes created fork fork() inherit the parent session id.

Sessions are used to implement foreground and background jobs.

Within a session there is at most one foreground job, everything else is a background job.

The session leader

The creator of the session is the so called session leader.

Usually, all process in a session share a controlling terminal, established by the session leader.

The controlling terminal of a session

A controlling terminal may control at most one session.

The controlling terminal is connected to the foreground job, meaning only the foreground job can read from the terminal. Every other job is a background job.

When a background job tries to read from the controlling terminal, the group receives a SIGTTIN and the job is suspended.

If TOSTOP flag is set for the controlling terminal, only the foreground job is able to write to the controlling terminal, while a background job would be suspended by a SIGTTOU.

All signals generated by a terminal key like ctrl+c are sent to the foreground job.

The controlling terminal is established when the session leader opens a terminal.

Usually within a session, jobs happen in the background. At most one job happens in the foreground, the so called foreground job.

Real User ID (uid) and Real Group ID (gid)

Retrive with getuid() and getgid(). These define who owns the process.

Effective User ID (euid) and Effective Group ID (egid)

Retrive with geteuid() and getegid(). These determines the permissions to accessing queues, shared memory, semaphores.

On many UNIX systems, but not on Linux, effective uid and effective gid are used to determine the permissions when accessing files.

Saved set-user-ID (resuid) and saved set-group-id (resuid)

These are placeholders used for switching between real and effective IDs.

They can be retrived with getresuid() and getresgid().

Filesystem user ID (fsuid) and Filesystem group ID (fsgid) (LINUX ONLY)

These are used to determine permissions for accessing files.

They can be retrived with getfsuid() and getfsgid().