Jump to content

Linux processes

From Wikibooks, open books for an open world



Processes

[edit | edit source]

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 forkkernel_clone id creates a new process by duplicating the process invoking it.

man 2 _exitdo_exit id terminates the calling process "immediately". Any open file descriptors belonging to the process are closed.

man 2 waitkernel_waitid id suspends the execution of the calling process until one of its children processes terminates.

man 2 execvedo_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 getpidtask_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 gettidtask_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

[edit | edit source]

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 tgkilldo_tkill id sends a signal to a thread
man 2 process_vm_readvprocess_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 mknoddo_mknodat id S_IFIFO id
Express Data Path PF_XDP id
Unix domain socket PF_UNIX id
Memory-mapped files man 2 mmapksys_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