# The Linux Kernel/Processes
## Processes

Process is a running user space program. Kernel starts the first process /sbin/init in function run_init_process$_{id}$using kernel_execve$_{id}$. Processes occupy system resources, like memory, CPU time. System calls sys_fork$_{id}$ and sys_execve$_{id}$ are used to create new processes from user space. The process exit with an sys_exit$_{id}$ system call.
Linux inherits from Unix its basic process management system calls (⚲ API ↪ ⚙️ implementations):
man 2 fork ↪ kernel_clone$_{id}$ creates a new process by duplicating the process invoking it.
man 2 _exit ↪ do_exit$_{id}$ terminates the calling process "immediately". Any open file descriptors belonging to the process are closed.
man 2 wait ↪ kernel_waitid$_{id}$ suspends the execution of the calling process until one of its children processes terminates.
man 2 execve ↪ do_execve$_{id}$ runs an executable file in the context of current process, replacing the previous executable. This system call is used by family of functions of libc man 3 exec
Linux enhances the traditional Unix process API with its own system calls man 2 clone. Clone creates a child process that may share parts of its execution context with the parent. It is often used to implement threads (though programmers will typically use a higher-level interface such as man 7 pthreads, implemented on top of clone).
PID - Process identifier defined as pid_t$_{id}$ is unique sequential number. man 1 ps -A lists current processes. Syscall man 2 getpid ↪ task_tgid_vnr$_{id}$ return PID of the current process which internally is called TGID - thread group id. A process can contain many threads. man 2 gettid ↪ task_pid_vnr$_{id}$ returns thread id. Which internal historically is called PID. ⚠️ Warning: confusion. User space PID ≠ kernel space PID. man 1 ps -AF lists current processes and thread as LWP. For a single thread process all these IDs are equal.
⚲ API
    unistd.h
    sys/types.h
    sys/wait.h
⚙️ Internals
    task_struct$_{id}$
    pid_type$_{id}$
    kernel/fork.c$_{src}$
       syscalls:
       man 2 set_tid_address – set pointer to thread ID
       man 2 fork – create a child process
       man 2 vfork – create a child process and block parent
       man 2 clone – create a child process
       man 2 unshare – disassociate parts of the process execution context
    kernel/sys.c$_{src}$
       syscalls:
       man 2 prctl – operations on a process or thread
    kernel/pid.c$_{src}$
       syscalls:
       man 2 pidfd_open – obtain a file descriptor that refers to a process
       man 2 pidfd_getfd – obtain a duplicate of another process's file descriptor
       syscalls:
       man 2 pidfd_open – obtain a file descriptor that refers to a process
       man 2 pidfd_getfd – obtain a duplicate of another process's file descriptor
    kernel/exit.c$_{src}$
       syscalls:
       man 2 exit – terminate the calling process
       man 2 exit_group – exit all threads in a process
       man 2 waitid – wait for process to change state
       man 2 waitpid – wait for process to change state
    fs/exec.c$_{src}$
📖 References
    fork (system call)
    exit (system call)
    wait (system call)
    exec (system call)

### Inter-process communication

Inter-process communication (IPC) refers specifically to the mechanisms an operating system provides to allow processes it manages to share data. Methods for achieving IPC are divided into categories which vary based on software requirements, such as performance and modularity requirements, and system circumstances. Linux inherited from Unix the following IPC mechanisms:
Signals (⚲ API ↪ ⚙️ implementations):
    man 2 kill sends signal to a process
    man 2 tgkill ↪ do_tkill$_{id}$ sends a signal to a thread
    man 2 process_vm_readv ↪ process_vm_rw$_{id}$ - zero-copy data transfer between process address spaces
🔧 TODO: man 2 sigaction man 2 signal man 2 sigaltstack man 2 sigpending man 2 sigprocmask man 2 sigsuspend man 2 sigwaitinfo man 2 sigtimedwait
kernel/signal.c$_{src}$
    Anonymous pipes and named pipes (FIFOs) man 2 mknod ↪ do_mknodat$_{id}$ S_IFIFO$_{id}$
    Express Data Path PF_XDP$_{id}$
    Unix domain socket PF_UNIX$_{id}$
    Memory-mapped files man 2 mmap ⤑ ksys_mmap_pgoff$_{id}$
    Sys V IPC:
       Message queues
       Semaphores
       Shared memory: man 2 shmget, man 2 shmctl, man 2 shmat, man 2 shmdt
📖 References
    Inter-process communication
    man 7 sysvipc