Processes
A process represents a running program or application. It includes information about the context of execution:
- π a pid (process id, unique)
- π a ppid (process id of the parent, -1 if the parent is dead)
- π a code to execute (current instruction, next instruction...)
- πͺΈ an environment (file descriptors, parameters, permissions...)
- π§ͺ some data (variables, environment variables, stack...)
- ...
Processes are stored in /proc/
, in a folder named after their PID.
β‘οΈ A daemon is a process that never ends. By convention, their name usually ends with d
such as systemd
.
β‘οΈ If a parent process dies, their children won't die, and will be assigned $-1$ as their PPID.
π You can get the current process PID using from the variable $$
.
Process States
Processes can be in various states, including:
- Running π£οΈ: actively executed on the CPU
- Sleeping π€: waiting for an event or a resource (ex: I/O)
- Stopped π€: halted or paused
- ...
When a process finishes its task, it will terminate itself. We can forcefully terminate a process using the signal CTRL+C
.
When a process is terminated, it returns an exit code:
- If the process successfully executed the task β
:
0
- Otherwise, if it failed β:
not 0
(usually from 1 to 255)
We can use $?
to query the exit code of the previous process:
$ /bin/false
$ echo $?
1
$ /bin/true && echo $?
0
Foreground and background
A foreground process is a process receiving input from the user, while a background process is running but not receiving input.
There can only be one foreground process at a time inside a session. We use background processes to run long tasks that do not require user input. Background processes:
- π΅ can't read and may not be able to write output on the terminal
- π aren't receiving signals, except CTRL-Z (suspend)
To run a command in the background, use &
:
$ sleep 10 &
Process-related commands
Usage π: list all running processes, and their memory usage. This is the same as Windows task manager.
Example π₯:
$ top
Usage π: list processes according to some criteria.
Example π₯:
See all processes
$ ps -A # only user processes
$ ps -e # all
$ ps -ef # add more information
$ ps aux # add even more information
$ ps axjf # view process tree
Every process "bash"
$ ps -C bash
List processes by PID (-s
for SID)
$ ps -p 12563
$ ps -p 12560,12563
$ ps -p {12590..12600}
List processes for a given user (-g
for a group)
$ ps -u username
List processes by terminal identifier, or a path to the terminal
$ ps -t pts/0
$ ps -t /dev/pts/0
Change the output
$ ps -j # basic
$ ps -l # long
$ ps -o pid,ppid
$ ps -o pid,ppid,pgid,tpgid,sid
Usage π: bring processes from the background to the foreground.
Example π₯:
$ some_command &
[1] 89
$ fg
$ fg 1 # same
$ fg %1 # same
Usage π: bring processes, that were not started using &
, to the background.
Example π₯:
$ some_command
# CTRL-Z
[1]+ Stopped
$ bg
$ bg 1 # same
$ bg %1 # same
Usage π: list running background processes. Not available in every shell.
Example π₯:
$ sleep 50&
[1] 36
$ jobs
[1]+ Running sleep 50 &
Usage π: kill a process/send a signal.
Example π₯:
All are sending "SIGTERM" (soft kill, allow cleanup)
$ kill pid
$ kill 15 pid
$ kill -s TERM pid
$ kill -SIGTERM pid
Other signals:
-
-9
/ SIGKILL: kill without cleanup -
-19
/ SIGSTOP: suspend - ...
You can use -l
to see the code for a given signal
$ kill -l SIGSTOP
19
Random Notes
Process scheduling
The scheduler job is to to maximize the use of available resources while ensuring that each process receives a fair share of the CPU time.
To the user, it looks like applications are running in parallel, but its pseudo-parallelism. As the scheduler lets each process use the CPU a little bit, they are all running a little, and we won't notice that they are not executed at the same time.
π Some scheduling algorithm: round-robin, priority-based...
Signals
Signals can be used for communication between processes or between a user and a process. The most well-known process is CTRL+C
. It forcefully terminates a foreground process.
Sessions
Every process is attached to a session identifiable with the SID (Session identifier) attribute. Most sessions are attached to a terminal (/dev/tty
).
Sessions are partitioned into groups of processes. A signal sent to a group is dispatched to every process of the group.
If a session dies, then the signal SIGHUP is sent to every process.
Namespaces
Containerisation platforms make use of "namespaces." They are a Linux kernel feature used to isolate resources (processes, files, memory, etc.). We cannot access resources of another namespace.
π» To-do π»
Stuff that I found, but never read/used yet.
-
pkill
,pgrep
, andkillall