Linux Advanced - Process Management

Linux Advanced - Process Management

I will use this space to discuss details about how process management works on Linux systems.

What is a Process?

A running instance of a program or a program in execution is known as a process.

When a command is executed it creates a process. Every process will be assigned a process ID or PID.

For example when you run date command on a Linux system you invoke a process.

What is Process Management?

Monitoring, tuning, and controlling instances of running applications is known as Process Management.

There are many tools (native and third party) available to manage processes on Linux systems. We will learn about them in detail below.

Different ways to run a process

Fundamentally there exists two ways one process can be run on a Linux system.

Foreground processes

Also known as interactive processes, gets initialized through a terminal session by taking its input from the keyboard and sending output back to the same terminal.

These processes will not start automatically as part of the system functions/services and they do not allow any process to run on the terminal until the process is killed or terminated by itself because the prompt would not be available.

For example ls command.

root@lco-linux-master:~# ls -l *.txt

ls.png

Here we are listing all the text files available in our current directory. The ls process runs in the foreground and the output gets directed to the terminal screen.

Background Processes

Also known as non interactive processes, are not connected to a terminal or keyboard (stdin) and usually do not need any kind of user input. They run on their own.

When a background process is running we can always initiate other processes within the same terminal from which they are initiated.

If you just add an & sign to any foreground process it will become a background process.

For example:

root@lco-linux-master:~# sleep 500 &
[1] 4676

root@lco-linux-master:~# jobs
[1]+  Running                 sleep 500 &

bg_process.png

Here we are running sleep command for 500 seconds which helps in delaying the execution and sending that to background.

To check all the background jobs run the jobs command as follows.

root@lco-linux-master:~# jobs
[1]+  Running                 sleep 500 &

jobs.png

To bring back a background process to foreground run fg command as follows.

root@lco-linux-master:~# fg

fg.png

There is another way to send a process to background. Run the application/command , then press ctrl + z and finally run the bg command. Pressing ctrl + z in between the execution of the command stops it.

root@lco-linux-master:~# sleep 500
^Z
[1]+  Stopped                 sleep 500

root@lco-linux-master:~# bg
[1]+ sleep 500 &

bg2.png

What if you have more than one background processes running on the system and wants to bring back one of them to foreground.

two_bgs.png

Here you can see two processes are running in background with some numbers assigned to them. You can bring them back to foreground by using those numbers with the fg command.

For example let me bring back process with job id 2 to foreground.

root@lco-linux-master:~# fg 2

fg2.png

Listing Running Processes

All the running processes can be listed by running ps (process status) command.

root@lco-linux-master:~# ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
root      4339  4234  0 16:21 pts/1    00:00:00 -bash
root      4936  4339  0 17:09 pts/1    00:00:00 sleep 1000
root      4937  4339  0 17:09 pts/1    00:00:00 ps -f

ps_ef.png

Let us understand all the parameters of this command.

ps_explaination.png

There are few important command arguments which you can use along with ps command.

-a -> Shows information about all users.

ps_a.png

-x -> Shows information about processes without terminals.

ps_x.png

-e -> Displays extended information.

ps_e.png

-u -> Shows additional information like -f option.

ps_u.png

Stopping or Killing Running Processes

There are several ways to stop or kill a process in Linux.

By pressing ctrl+c

If a process is running in foreground you can stop or kill that process by pressing ctrl+c (the default interrupt character).

For example:

ctrlc.png

By using kill command

In order to kill or terminate a process running in background use the kill command to kill the process as follows. One should be aware of the process PID to kill it.

root@lco-linux-master:~# kill -9 5218

kill.png

What is kill command?

kill is a native Linux command which sends a signal to specified processes or process groups, causing them to act according to the signal.

Following are the available kill signals.

kill_signals.png

all kill signals begin with "SIG"; this means SIGnal.

Particularly useful signals include HUP, INT, KILL, STOP, CONT, and 0. Alternate signals may be specified in three ways: -9, -SIGKILL or -KILL.

signals.png

Signals can be specified in three different ways.

signal_ways.png

Types of Processes in Linux

There are five types of Processes which exists on a Linux system.

Let us understand each one if them.

Parent process

In Linux all the processes have a parent process. When the user creates a process in that case the kernel process becomes the parent of that process.

Every Linux process has two ID's assigned to it, the Process ID (pid) and the Parent process ID (ppid).

For example:

root@lco-linux-master:~# sleep 500 &
[1] 18188

root@lco-linux-master:~# ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
root      3025  2932  0 May09 pts/0    00:00:00 -bash
root     18188  3025  0 13:58 pts/0    00:00:00 sleep 500
root     18189  3025  0 13:58 pts/0    00:00:00 ps -f

root@lco-linux-master:~# pidof sleep
18188

root@lco-linux-master:~# pstree 3025
bash─┬─pstree
     └─sleep

parent_process.png

Here we have run a background sleep process. By running pidof command followed by process name we can find its pid and by running ps -f command we can find both pid and ppid` of a process.

pstree command is used to display a tree of processes and we can clearly see bash process if the parent process which we ran over terminal via keyboard input (stdin).

Child Processes

The processes which gets created by another process aka it's parent process known as child process.

In our above example, the sleep process with PID 18188 is a child process of the bash process with PID 3025.

Orphan Processes

Usually whenever a child process is killed, the parent process is updated via a SIGCHLD signal which means now the parent process can do some other task or restart a new child as needed.

But in some cases the parent gets killed before the child process then the child process becomes an orphan process. All the orphan processes then have Init process (PID 0) as their parent.

Zombie processes

At times there are processes which are already dead but still shows up in process list are called Zombie processes. These processes can be found while doing ps listing, the process with a Z state are zombie processes. They don't consume any CPU resources.

zombie.png

Daemon process

The system related background running processes are called Daemon Processes. When you see a process running with a ? mark in sixth column (TTY field), that's a daemon process.

daemon_process.png

Process states

Each process running on a Linux system goes through different states in its life cycle.

The below diagram is self explanatory.

process-states-3.png

The top command

To view the dynamic real-time view of the running processes and system resource usage Linux systems natively has a command called top.

It is the best place to debug your system resources, the process states, system load etc..

When you run the top command an interactive command mode session gets opened. Top portion of it reports the processes statistics and the resource usage. The lower portion lists the currently running processes.

top.png

You can press the Up or Down Arrows, Home, End, and Page Up or Down keys to move up and down and access all the processes.

Let us understand the metrics shown here.

The first line ->

It includes the time, how long your computer has been running, the number of people logged in, and what the load average has been for the past one, five, and 15 minutes.

The second line ->

It shows the number of tasks/processes and their states: running, stopped, sleeping, or zombie.

The third line ->

It displays the following central processing unit (CPU) values:

top-cpu.png

The fourth line ->

It shows the memory consumption details such as total amount of physical memory, and how much is free, used, and buffered or cached.

The fifth line ->

It shows the swap memory details, and how much is free, used, and available.

The process list columns are as follows:

top-process_column.png

The process status can be any of the following:

D: Uninterruptible sleep

R: Running

S: Sleeping

T: Traced (stopped state)

Z: Zombie

A brief intro to nice values :

Nice values are user-space values that we can use to control the priority of a process and priority PR is the process's actual priority that is used by Linux kernel. The nice value range is -20 to +19 where -20 is highest, 0 default and +19 is lowest.

That's all for this detailed guide covering all major aspects of Process Management on Linux systems.

Hope you like the article. Stay Tuned for more.

Thank you. Happy learning!

Did you find this article valuable?

Support Learn Code Online by becoming a sponsor. Any amount is appreciated!