Processes and jobs
Jump to navigation
Jump to search
Processes
- Process: instance of 1 or more related tasks (threads) executing on computer. Can be independent of or dependent on other processes. Kernel allocates share of resources (memory, CPU, peripheral devices) to processes
Types of processes
Type | Description | Example |
---|---|---|
Interactive | Need to be started by user | bash, firefox |
Batch | Automatic - scheduled & disconnected from terminal Queued and work on first-in, first-out (FIFO) basis |
updatedb |
Daemon | Run continuously. May launch during system startup | httpd, xinetd, sshd |
Thread | Runs under umbrella of a main process, sharing memory & other resources, but scheduled & run by system on individual basis Individual thread can end without ending entire process New threads can be created at any time. Many programs now multithreaded |
gnome-terminal, firefox |
Kernel thread | Kernel tasks that users can't start or terminate Perform actions like moving a thread from 1 CPU to another, making sure I/O operations to disk are completed |
kswapd, migration, ksoftirqd |
Process scheduling and states
- When process in running state: either currently executing instructions on a CPU or waiting for a share time slice) from CPU in which to run.
- Critical kernel routing aka scheduler constantly shifts processes in and out of CPU, sharing time according to relative priority, how much time needed & how much already granted to a task
- Run queue for each CPU: each running process is in one of these queues
State | Description |
---|---|
Running | Process currently being executed by CPU |
Sleeping | Process waiting for input from another process. Sits in a wait queue |
Waiting | Process has received input, ready to execute & waiting for attention from CPU |
Stopped | Process halted. Will not run again till a signal received |
Zombie | Process dead. Takes up no resources. Retained until parent process allows it to disappear |
Process and thread IDs
- OS gives each process a unique process ID (PID). Tracks state, CPU & memory usage, precisely where resources allocated in memory etc.
- New PIDs usually assigned in ascending order as processes spawned so PID = 1 is usually for init (initialization) or systemd process.
ID type | Description |
---|---|
Process ID (PID) | Unique process ID number |
Parent process ID (PPID) | Process (parent) that started this process |
Thread ID (TID) | Same as PID for single-threaded process For multi-threaded, each thread shares same PID but has unique TID |
User and group IDs
- OS identifies different users' processes with Real User ID (RUID) assigned to each user
- Users can be grouped: Real Group ID (RGID)
- Effective owner given by Effective UID (EUID) (for user) and EGID (for group)
- ps auxw - shows user identity of running processes
The ps command
ps -ef
displays all processes- -e for every process
- -f for full details
ps -eLf
also shows theads- Can also use
ps aux
,ps GXO <list of properties to show>
- Look for a running process
ps -ef | grep -i <search criteria>
ps -U root
shows processes run by root, -G to list processes by owning group
pstree
pstree
shows running processes in a tree format, showing relationships between child & parent processes. Threads displayed in curly braces
Terminate a running process
kill -SIGKILL <pid>
orkill <pid>
- Have various different options such as SIGKILL, SIGTERM etc. See more info in
man kill
- Have various different options such as SIGKILL, SIGTERM etc. See more info in
- Or use top, see below
Terminate a running process graphically using xkill
- Kills connections to X Server (GUI). Good for e.g. applications that have hung, or undesired windows displayed
xkill
on its own brings up a pointer - select window to kill. Can also kill by X Identifierxkill -id <X resource number>
pidof and pgrep
- Get PID of specific process
pidof cupsd
pgrep -U root
shows all processes owned by root, use -G to show for a particular group and use -l at the end to also show names of the processes
Process priority
- Nice values: -20 'highest' priority, 19 'lowest'
- 'Higher' priority means gets attention first from CPU, lower means has to wait for CPU attention
- Most system-started processes have value 0 priority
- Child processes inherit parent process' nice value
ps -l
displays nice values, in top shown in the NI column- Renice a process:
nice -n <value> <pid of process>
- Use
nice -2 top
to increase niceness of top (make it lower priority) ornice --10 top
to give it a lower value/higher priority
Signals
- Show available signals with
kill -l
orkill -L
- Can use the name or the number
- Format:
kill -<number>|-s <name>|--signal <name>
- Owning users can kill own processes, root can kill all processes
- Can kill multiple at once:
kill <signal value> <pid1> <pid2> <pidN>
orpkill <signal value> <name>
orkillall <name>
- If you don't specify signal value, it uses default 15
- Most commonly used kill signal values:
Signal number | Signal name | Description |
---|---|---|
1 | SIGHUP | Causes process to disconnect from a closed terminal it was tied to Also used to instruct running daemon to re-read config |
2 | SIGINT | Issues ^C (Control + C) to interrupt execution of process |
9 | SIGKILL | Kills process abruptly |
15 | SIGTERM | Sends soft termination signal to stop process in 'orderly fashion', but process can ignore it. Default |
nohup signal
- To execute command (or program launched at command line) immune to hangup signals and will continue running in the background even if you log out:
nohup <command or program or script> &
- Can also use
screen
command
More info on signals
Kill user sessions
- Use
w
to find active sessions & corresponding pts then e.g.pkill -9 -t pts/1
- Kill all sessions of specific user
killall -u john
- https://linuxiac.com/how-to-terminate-user-session-in-linux/
Background and foreground processes (jobs)
- Foreground jobs (i.e. commands run from shell): only 1 can run at a time
- Can also run jobs (commands) in the background, usin the shell, so that can run other commands at the same time
- E.g.
updatedb &
- View running jobs
jobs
bg
(background) andfg
(foreground) commands - for graphical applications e.g.firefox bg
. This will result in messages related to the running process being generated in the terminal screen so can be useful for troubleshooting- Cancel a job by using
disown
orkill %1
where %1 is job 1, see job number usingjobs
- E.g.
- Background jobs run in a lower priority
- Control + Z suspends foreground job (equivalent to SIGTSTP signal), use
fg
to bring it back, Control + C cancels it (SIGINT). Can also usesuspend
- Jobs are connected to the terminal window they are started from, will not show in a new or separate terminal
- Example:
pidstat -dhlrsu 2 >> pidstat.txt & disown