Process control - jasper-zanjani/dotfiles GitHub Wiki
Process control at atq atrm bg fg jobs kill killall lsns nice nohup pgrep pidof pkill ps pstree renice strace top unshare
A process consists of an address space and a set of data structures within the kernel. An address space consists of a set of pages in memory allocated to the process. (ULSAH: 90)
A thread is an execution context within a process. (ULSAH: 91)
- a process runs in its own user address space, a protected space which can't be disturbed by other users
- all processes on a Linux system are child processes of a common parent: the
init
process which is executed by the kernel at boot time (PID 1) - every Linux process inherits the environment (PATH variable, etc) and other attributes of its parent process
Every process has a parent; a process can spawn children in a process that is actually made of two separate system calls.
-
FORK
copy process invoking it asfork()
-
EXEC
parent then overwrites this copy with the program that has to be executed which replaces or overlays the text and data areas asexec()
system call -
WAIT
parent waits forSIGTERM
signal which the child will send upon completionwait()
system call -
TEXT SEGMENT
: executable code -
DATA SEGMENT
: variables and arrays the program uses during execution -
USER SEGMENT
: process attributes
-
process ID (PID)
-
real user ID of user who created it (stored in /etc/passwd)
-
real group ID
-
priority
-
Internal commands (
cd
,echo
, etc. and variable assignments) do not spawn child processes -
Shell scripts are executed by spawning a sub-shell, which becomes the script's parent
-
External commands are spawned as children of the parent as described above
Priorities range from 0-19 in csh
(10 is default); higher values run at lower priority
nice
View priorities of jobs
ps -l
Run cmd
at a higher priority
nice -5 cmd &
Control group (cgroups) is a Linux kernel feature that isolates a collection of processes.
- allow you to allocate resources (CPU time, system memory, network bandwidth, or combinations thereof) among user-defined groups of processes
- like processes, cgroups are hierarchical and inherit attributes from parents, but they from separate trees branching off from
subsystems
(also:resource controller
, or justcontroller
), each of which represent a single system resouce-
blkio
sets limits on input/output access from block devices -
cpu
which provides access to the CPU - many more...
-
- different from
namespaces
- good for limiting the resources available to a container
-
systemd
uses cgroups - first heard about in Linux Unplugged 289, in the context of Fedora supporting v2 whereas most userspace applications support v1
Process IDs in the same namespace can have access to one another, whereas those in different namespaces cannot. Spawning a process in a new namespace prevents it from seeing the host's context, so an interactive shell like zsh
spawned in its own namespace will report its PID as 1
, even though the host will assign its own PID. This is the concept behind containers. [55]
Run $CMD
at a nice value of (positive) 10
nice -10 $CMD
nice -n 10
nice $CMD
Run a program in a namespace unshared from its parent process. [ref][https://opensource.com/article/19/10/namespaces-and-containers-linux]
sudo unshare --fork --pid --mount-proc zsh