Brief explanation of process related terms in linux.

In this article, we will look at different terms w.r.t process and understand them.

We all should know how to manage process(es) in linux.

This post is very useful for our day to day activities which revolves around the processes.

This article explains almost all terms related to a process in Linux.

Let us have a look at these terms point wise to have a better understanding.

1. What is a process ?
A process is responsible to execute a job or task(some lines of code to perform a specific function).
CPU performs the job of process execution. A process can have following states(can be seen in man page of ps command):

       D    Uninterruptible sleep (usually IO)
       R    Running or runnable (on run queue)
       S    Interruptible sleep (waiting for an event to complete)
       T    Stopped, either by a job control signal or because it is being traced.
       W    paging (not valid since the 2.6.xx kernel)
       X    dead (should never be seen)
       Z    Defunct ("zombie") process, terminated but not reaped by its parent.

2. What is child process ?
A process forked from parent process is a child process.
Child process uses code and data of the parent process.

3. How to see all process running in machine?

[root@ngelinux ~]# ps -ef | wc -l
147
[root@ngelinux ~]# ps -A | wc -l
147

4. How to check if a particular process is running or not ?

### Lets see the process with name bash
[root@ngelinux ~]# ps -A | grep bash
 4509 pts/0    00:00:00 bash

### We can also use pgrep command.
[root@ngelinux ~]# pgrep bash
4509
[root@ngelinux ~]# 

5. How to get details of a particular process ?
One of the most useful command to see all process details.

[root@ngelinux ~]# ps -FC bash
UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root      4509  4505  0  1316  1744   0 21:19 pts/0    00:00:00 -bash
[root@ngelinux ~]# 

6. How to kill all processes of specific name using single command ?

### Usin pkill command
[root@ngelinux ~]# pkill -9 bash
Connection closing...Socket close.
Connection closed by foreign host.

### Using killall command
[root@ngelinux ~]# killall -9 bash
Connection closing...Socket close.
Connection closed by foreign host.

7. How to kill one process using pid ?

### Get current shell process id
[root@ngelinux ~]# echo $$
4736

### Kill the process using PID
[root@ngelinux ~]# kill -9 4736
Connection closing...Socket close.
Connection closed by foreign host.

8. How to start process in background ?

### Get current tty
[root@ngelinux ~]# tty
/dev/pts/0
[root@ngelinux ~]# 

### echo hello every 5 seconds on your tty in background
[root@ngelinux ~]# while true; do echo "hello" > /dev/pts/0; sleep 5; done &
[1] 4851
[root@ngelinux ~]# hello

[root@ngelinux ~]# hello

9. How to list all background processes ?

[root@ngelinux ~]# jobs -l
[1]+  4851 Running                 while true; do
    echo "hello" > /dev/pts/0; sleep 5;
done &
[root@ngelinux ~]#

[1] is the job number of the current shell.
We can specify the job number instead of process IDs to kill the process to to bring it in foreground.

10. How to kill background process ?
We can send various signals including termination/stop or restart to various jobs of the current shell using job numbers instead of process numbers.

### Job numbers are presented with % like below.
### Lets send SIGTERM to job number 1 of this shell
[root@ngelinux ~]# kill -9 %1
[root@ngelinux ~]# jobs
[1]+  Killed                  while true; do
    echo "hello" > /dev/pts/0; sleep 5;
done
[root@ngelinux ~]# 

11. What is foreground process ?
By default any process started on the bash shell runs in the foreground.

12. How to move a process from background to foreground ?

### Lets start two new background jobs.
[root@ngelinux ~]# yes > /dev/null &
[1] 5087
[root@ngelinux ~]# yes > /dev/null &
[2] 5088

### Check out two background jobs initiated.
[root@ngelinux ~]# jobs
[1]-  Running                 yes > /dev/null &
[2]+  Running                 yes > /dev/null &
[root@ngelinux ~]# 

### Bring second job to foreground
[root@ngelinux ~]# fg %+2
yes > /dev/null

### Bring first job to foreground.
[root@ngelinux ~]# fg %-1
yes > /dev/null

In the above scenario, we can simply refer the jobs as fg %1 and fg %2.

The “+” sign next to the job number indicates the current job send to background.
The previous job is always marked with “-” sign.
Hence we can refer to current job as “%” or “%+”, and previous job by “%-“.

13. How to move foreground process to background process ?
To move the process to background, we need to stop it and then send it to background.

### Stop a running process using Ctrl+Z
[root@ngelinux ~]# yes > /dev/null 
^Z
[1]+  Stopped                 yes > /dev/null

### Send the process in background now and it will be started automatically.
[root@ngelinux ~]# bg %1
[1]+ yes > /dev/null &

[root@ngelinux ~]# jobs
[1]+  Running                 yes > /dev/null &
[root@ngelinux ~]# 

14. What happens after we close the current shell session ?
When we close the terminal/shell, the shell sends SIGHUP signal to all foreground process and background processes. Hence in ideal case, all running foreground and background processes get killed by SIGHUP signal until and unless started using nohup command in bakground or shell options are modified.

15. How to retain process(continuously running) after closed terminal ?
We can use nohup command and an run the process in background to keep it running in background.

### Lets run another process using nohup command.
[root@ngelinux ~]# nohup yes > /dev/null &
[2] 5469
[root@ngelinux ~]# nohup: ignoring input and redirecting stderr to stdout

### Now we can see two commands, one with nohup.
[root@ngelinux ~]# jobs
[1]-  Running                 yes > /dev/null &
[2]+  Running                 nohup yes > /dev/null &

### Logout from current shell
[root@ngelinux ~]# logout
Connection closing...Socket close.
Connection closed by foreign host.

### Now prgep with yes command, only one process is running with same process id of nohup.
[root@ngelinux ~]# pgrep yes
5469

nohup marks the process to ignore NOHUP signal and keeps it running in backgroud.
After shell logout, “yes” background process is adopted by init process.
And yes process becomes an orphan process.

16. What is orphan process?
If parent process is completed/dead, and child process are still running.
These child processes without parents are known as orphaned process.
These orphaned child processes are adopted by init process and hence their PPID is reassigned to 1.

To see PPID i.e. parent PID, we can use ps command.

[root@ngelinux ~]# ps -FC yes
UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root      5469     1 33  1017   488   0 22:04 ?        00:01:38 yes

17. How to list all available process signals in linux ?

[root@ngelinux ~]# 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	
[root@ngelinux ~]# 

18. What are two different sleep states of process in Linux ?
1. Interruptible sleep
2. UnInterruptible sleep

What is Interruptible sleep and UnInterruptible sleep?
Interruptible sleep
In Interruptible sleep, we can communicate process using signal like kill -9 or kill SIGTERM. That is why it is called as Interruptible sleep.

To find Interruptible sleep process:

[root@ngelinux ~]# ps -A -o pid,comm,stat | grep 'S'
  PID COMMAND         STAT
    1 init            Ss
    2 kthreadd        S
    3 migration/0     S
    4 ksoftirqd/0     S
    5 stopper/0       S
    6 watchdog/0      S

UnInterruptible sleep
In UnInterruptible sleep, we can not communicate with process using signal because kernel locks the process for security purpose. Hence It is called UnInterruptible sleep.

To find UnInterruptible sleep

[root@ngelinux ~]# ps -A -o pid,comm,stat | grep 'D'
  PID COMMAND         STAT
[root@ngelinux ~]# 

My system does not have any uninterrupted sleep process. In case you system has too many frequent uninterrupted sleep processes, then you need to contact your admin support team for any bug assistance with kernel/or specific program.

19. How to kill UnInterruptible sleep?
We should not kill unInterruptible sleep process. Wait for the process to finish execution or then start it again.

20. How to find that what all processes are consuming more MEMORY ?

[root@ngelinux ~]# ps -A --sort -rsz -o pid,comm,pmem,rsz | head -5
  PID COMMAND         %MEM   RSZ
 2455 mysqld           2.9 30604
 2734 Xorg             1.7 17944
 2901 gdm-simple-gree  1.3 14260
 2874 gnome-settings-  1.1 11552
[root@ngelinux ~]# 

21. How to find which processes are consuming more CPU usage ?

[root@ngelinux ~]# ps -A --sort -pcpu -o pid,comm,time,pcpu | head -5
  PID COMMAND             TIME %CPU
 5268 yes             00:10:22 48.4
 5388 yes             00:06:24 36.6
 5469 yes             00:04:28 33.1
 5703 ps              00:00:00  1.0
[root@ngelinux ~]# 

Using above ps command, we can get average cpu usage from starting of server.


If we want to check current cpu usage of process, then we can use top command.

[root@ngelinux ~]# top -bn2 | awk '/^top/{i++}i==2' | sed -n '7,14p'
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                
 5268 root      20   0  4068  488  436 R 33.2  0.0  11:04.11 yes                                    
 5469 root      20   0  4068  488  436 R 33.2  0.0   5:10.74 yes                                    
 5388 root      20   0  4068  492  436 R 32.9  0.0   7:05.81 yes                                    
 2692 ricci     19  -1  9676  920  652 S  0.3  0.1   0:00.33 ricci                                  
 5746 root      20   0  2712 1128  864 R  0.3  0.1   0:00.02 top                                    
    1 root      20   0  2900 1416 1200 S  0.0  0.1   0:02.86 init                                   
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.01 kthreadd                               
[root@ngelinux ~]# 

I have tried to cover almost every term related to process, however there is a lot more and everything can’t be covered in a single post.

Do post your comments/suggestions below.

Leave a Reply

Your email address will not be published.