Linux processes
Process is a running user space program. Kernel starts the first process /sbin/init in function run_init_process idusing 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.
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.
⚲ API
⚙️ 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
📖 References