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

linuxfundamentalspart1

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.


πŸ‘» To-do πŸ‘»

Stuff that I found, but never read/used yet.

  • pkill, pgrep, and killall