Process management in Linux¶
What is a process?¶
- At least one process is created when program starts
- Program can use several processes
- Process can create child processes
- Each process has its own memory area and other resources that other processes cannot access
- Processes are run simultaneously on modern systems with multiple processor cores
- Only one process can be running with one processor core at once
- Scheduler shares processor time for each running process
- From user's perspective this seems as processes (=programs) are running simultaneously
General information about processes¶
- Each process has its own unique identifier, which is used to separate it from other processes in the system
- This identifier is called PID (Process ID)
- Process information is kept in /proc directory (process information pseudo-file system)
- Process has three main states:
- Running → process is executed or waiting to be executed
- Stopped → process is interrupted, but is ready to be executed
- Blocked (Uninterruptible/Interruptible) → process is waiting some event (for example I/O event, file lock release etc.)
- Zombie → process has ended, but its structure is still in process table
- Other processes are descended from so called systemd init process
- Systemd init is the first process started with the system and is started by kernel during the system bootup
- Process can start or create a new process
- Created process is called a child process
- Child process creator is parent process
- New process is created with fork call
-
Commands for managing processes:
- top, ps → Monitor process memory consumption, running state and processor time
- nice, renice → Change process priority (processor time usage)
- kill → Shutdown a process
-
Like mentioned earlier, each process has its own unique PID number
- In addition to PID, shell gives job number (JOB) to command when executed from CLI
- Almost every command in Linux creates a process when executed
- Exceptions to this are for example cd, logout and exit commands
- Bash process is created when user logs in to the system (when user's default shell is bash)
Threads¶
- Process can further be divided into threads
- Each process consists of at least one thread
- Threads are not isolated as effectively as processes
- All threads within the same process share the same memory area
- Threads in different processes use different memory areas
- Term multithreading is used when process consists of multiple threads and these threads can then be running on several processors/processor cores simultaneously
- Below is a presentation of different variations of threads in processes
source: Stallings W. Operating Systems: Internals and Design Principles
Process types¶
- Processes can be devided into following three types:
- Foreground
- Process gets input from user
- It will run directly and user will wait to be finished
- Background
- User is able to run other commands while process runs in the background
- Process is started to the background by adding & character after the command
- Services
- Service is called daemon in Linux
- Will start automatically during system startup or when user runs it (/sbin/usr/sbin directory)
- Foreground
PS (Report Process Status)¶
- User and terminal processes can be listed with ps command
testuser@ubuntu-pc:~$ ps
PID TTY TIME CMD
2024 tty1 00:00:00 bash
2041 tty1 00:00:00 ps
- By adding a option for ps command, all processes controlled by different terminals are listed
testuser@ubuntu-pc:~$ ps a
PID TTY STAT TIME COMMAND
1131 tty1 Ss 0:00 /bin/login -p --
2024 tty1 S 0:00 -bash
2044 tty1 R+ 0:00 ssh localhost
2133 pts/0 Ss 0:00 -bash
2145 pts/0 R+ 0:00 ps a
- All processes controlled by this terminal can be listed by using the -au option
- This option also provides information about the user, CPU and memory
testuser@ubuntu-PC:~$ ps au
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1074 0.0 0.0 78868 3776 tty1 Ss 05:20 0:00 /bin/login -p --
testuser 1887 0.0 0.1 21468 5144 tty1 S 05:20 0:00 -bash
root 1904 0.0 0.1 62464 4244 tty1 S 05:20 0:00 sudo su
root 1906 0.0 0.0 61756 3816 tty1 S 05:20 0:00 su
root 1907 0.0 0.0 20288 3732 tty1 S+ 05:20 0:00 bash
testuser 2030 0.1 0.1 21492 5132 pts/0 Ss 05:23 0:00 -bash
testuser 2042 0.0 0.0 38372 3584 pts/0 R+ 05:24 0:00 ps au
- This listing can further by expanded by adding an x option (the whole option will be aux)
- This option will also list processes not controlled by terminal (network daemons, service daemons etc.)
- lax option will list also the PRI and NI values of processes like shown in the example below
testuser@ubuntu-PC:~$ ps lax
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
4 0 1 0 20 0 77796 8848 - Ss ? 0:01 /sbin/init maybe-ubiquity
1 0 2 0 20 0 0 0 - S ? 0:00 [kthreadd]
1 0 4 2 0 -20 0 0 - I< ? 0:00 [kworker/0:0H]
1 0 5 2 20 0 0 0 - I ? 0:00 [kworker/u4:0]
1 0 6 2 0 -20 0 0 - I< ? 0:00 [mm_percpu_wq]
1 0 7 2 20 0 0 0 - S ? 0:00 [ksoftirqd/0]
1 0 8 2 20 0 0 0 - I ? 0:00 [rcu_sched]
1 0 9 2 20 0 0 0 - I ? 0:00 [rcu_bh]
1 0 10 2 -100 - 0 0 - S ? 0:00 [migration/0]
5 0 11 2 -100 - 0 0 - S ? 0:00 [watchdog/0]
- Below is a figure describing fields from the output of ps command
PSTREE (Display a Tree of Processes)¶
- pstree command prints the process list in tree-like format
- This command is useful since user gets the overall view of process inheritance by a quick look
- The top process (systemd) is the process from which every other process is inherited
testuser@ubuntu-PC:~$ pstree
systemd─┬─accounts-daemon───2*[{accounts-daemon}]
├─atd
├─cron
├─dbus-daemon
├─irqbalance───{irqbalance}
├─login───bash───sudo───su───bash
├─lvmetad
├─lxcfs───2*[{lxcfs}]
├─networkd-dispat───{networkd-dispat}
├─polkitd───2*[{polkitd}]
├─rsyslogd───3*[{rsyslogd}]
├─snapd───11*[{snapd}]
├─sshd───sshd───sshd───bash───pstree
├─systemd───(sd-pam)
├─systemd-journal
├─systemd-logind
├─systemd-network
├─systemd-resolve
├─systemd-timesyn───{systemd-timesyn}
├─systemd-udevd
└─unattended-upgr───{unattended-upgr}
- Output of the pstree command can be customized for example with the following options:
- -p → Show process PID numbers
- -n → Sort processes by process PID numbers (smallest PID will be shown at the top of the tree)
Kill (Terminate a Process)¶
- Process can be ended with kill command
- Command syntax: **kill
- List of available signals can be retrieved with l option like in the example below
testuser@ubuntu-PC:~$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
- Signal means the program interruption
-
Each signal has the default operation configured, for example
- Ending the process
- Signal will not be noted
-
Example 1: End process with a PID of 4065 using the greatest ending signal (9)
testuser@ubuntu-pc:~$ kill -9 4065
- Example 2: Kill all processes (Important: Do not run as a root user!)
testuser@ubuntu-pc:~$ kill -9 -1
TOP (Display Top CPU Processes)¶
- top program is an interactive tool for process management
- top program can be started with top command
testuser@ubuntu-pc:~$ top
- Running processes are listed and sorted by processor usage (%CPU) by default (process consuming processor time for the most is shown at top)
top -06:42:23 up 1:22, 2 users, load average: 0.00, 0.00, 0.00
Tasks: 102 total, 1 running, 57 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.1 us, 0.2 sy, 0.0 ni, 99.4 id, 0.2 wa, 0.0 hi, 0.1 si, 0.0 st
KiB Mem : 4039536 total, 3367340 free, 132816 used, 539380 buff/cache
KiB Swap: 4038652 total, 4038652 free, 0 used. 3674048 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2452 testuser 20 0 42796 3932 3276 R 5.6 0.1 0:00.04 top
1 root 20 0 77796 8848 6596 S 0.0 0.2 0:01.60 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/0:0H
6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 mm_percpu_wq
7 root 20 0 0 0 0 S 0.0 0.0 0:00.12 ksoftirqd/0
- Below is the table including some of the more common top program shortcuts
Shortcut | Operation |
---|---|
h or ? | Print the help screen |
k | Kill the process |
i | Hide zombie and stopped processes |
n | Amount of processes to be displayed |
q | Exit the program |
r | Change process priority (re-nice) |