System Programming: Process Management in Linux

System Programming: Process Management in Linux

Unix/Linux Process Management: ps, kill, top, df, free, nice Commands

image.png

In Linux, unlike windows, all commands are run on terminal/shell. All administrative tasks can be accomplished through terminal/shell. This includes package installation, navigation, file manipulation, and user management. Process management in Linux is nothing but manipulating (resume, stop or kill) a command which is already in progress, about to start or already killed.

What is a Process?

An instance of a program is called a Process. In simple terms, any command that you give to your Linux machine starts a new process. The process is a program in execution. The process is created when a command is to be executed so, it can be called a running instance of a program in execution. Tuning or controlling a process is called Process Management.

When you launch Office to write some article:

image.png

Corresponding process is created:

5034 ubuntu    20    0  225m  49m S  0.3  10.0    0:07.22  soffice.bin

Having multiple processes for the same program is possible.

Any process can be run in two ways:

  • Foreground Processes: They run on the screen and need input from the user. For example Office Programs.

  • Background Processes: They run in the background and usually do not need user input. For example Antivirus. Adding ‘&’ to a foreground command makes it a background process.

Running a Foreground Process

By default, All the processes are run in the foreground. When a process is run in foreground, no other process can be run on the same terminal until the process is finished or killed. When issuing this type of process, the system receives input from the keyboard(stdin) and gives output to the screen(stdout).

To start a foreground process, you can either run it from the dashboard, or you can run it from the terminal.

When using the Terminal, you will have to wait, until the foreground process runs.

image.png

Running a Background process

A background process runs on its own without input from the keyboard(stdin) and waits for input from the keyboard. While the process runs in the background, other processes can be run in the foreground.

If you start a foreground program/process from the terminal, then you cannot work on the terminal, till the program is up and running.

Particular, data-intensive tasks take lots of processing power and may even take hours to complete. You do not want your terminal to be held up for such a long time.

To avoid such a situation, you can run the program and send it to the background so that terminal remains available to you. Let me explain how to do this:

Start the program and press ctrl + z

silva@oluwaseunda-silva:~$ banshee
[Info 16:08:36.688] Running Banshee 2.2.1: [Ubuntu 20.04 14:51:26 UTC]
^Z
[1]+  Stopped                    banshee

Type bg to send the process to the background

silva@oluwaseunda-silva:~$ bg

Types of Process

There are five types of Process in Linux

1. Parent Process

The process created by the user on the terminal. All processes have a parent process, If it was created directly by user then the parent process will be the kernel process.

image.png

2. Child process

The process created by another process (by its parent process). All child processes have a parent process.

The example is given above, the process having PID 87822(last row) is a child process of the process having PID 87821.

3. Orphan process

Sometimes when the parent gets executed before its own child process then the child process becomes an orphan process. The orphan process have “Init” process (PID 0) as their PPID (parent process ID)

4. Zombie process

The processes which are already dead but shows up in process status is called Zombie process. Zombie processes have Zero CPU consumption.

5. Daemon process

These are system-related processes that run in the background. A Daemon process can be recognized if it has “?” in its TTY field (6th column)

For ease of understanding, let us now discuss about basic commands used for process management in Linux one by one.

FG

You can use the command fg to continue a program which was stopped and bring it to the foreground.

The simple syntax for this utility is:

fg jobname

Example

  1. Launch banshee music player
  2. Stop it with the ctrl + z command
  3. Continue it with the fg utility.
silva@oluwaseunda-silva:~$ banshee
^Z
[1]+  Stopped                    banshee
silva@oluwaseunda-silva:~$ fg banshee
banshee
[Info 00:36:19.400] Running Banshee 2.2.0: [Ubuntu oneiric 
(linux-gnu, i686) @ 2022-09-15 17:40:16 UTC]

Let’s look at other important commands to manage processes.

TOP

This utility tells the user about all the running processes on the Linux machine.

silva@oluwaseunda-silva:~$ top

image.png

Press q on the keyboard to move out of the process display.

PS

This command stands for ‘Process Status’. It is similar to the “Task Manager” that pop-ups in a Windows Machine when we use Ctrl + Alt + Del. This command is similar to ‘top’ command but the information displayed is different.

To check all the processes running under a user, we use the command:

ps ux

image.png

To list all process in the background use ps –f and to know more info on process use ps -ef

Here’s an example of a list of the process using ps –ef

silva@oluwaseunda-silva:~$ ps -ef

image.png

We can also check the process status of a single process, use the syntax

ps PID

image.png

KILL

This command terminates running processes on a Linux machine.

To use these utilities we need to know the PID (process id) of the process we want to kill

Syntax

kill PID

To find the PID of a process simply type

pidof {Process_name}

Let us try it with an example.

silva@oluwaseunda-silva:~$ pidof Photoshop.exe
1525
silva@oluwaseunda-silva:~$ kill 1525

NICE

Linux can run a lot of processes at a time, which can slow down the speed of some high priority processes and result in poor performance.

To avoid this, you can tell your machine to prioritize processes as per your requirements.

This priority is called Niceness in Linux, and it has a value between -20 to 19. The lower the Niceness index, the higher would be a priority given to that task.

The default value of all the processes is 0.

To start a process with a niceness value other than the default value use the following syntax

nice -n {Nice_value} {process_name}
silva@oluwaseunda-silva:~$ nice -n 19 banshee

If there is some process already running on the system, then you can renice its value using syntax:

silva@oluwaseunda-silva:~$ renice {nice_value} -p PID

To change Niceness, you can use the top command to determine the PID (process id) and its Nice value. Later use the renice command to change the value.

Let us understand this by an example.

Checking the niceness value (column 4) of the process banshee (run the top command):

PID   USER    PR  NI   VIRT   RES  SHR  S  %CPU  %MEM  TIME+    COMMAND
3293  home    20  0    277m   64m  35m  S  96.4  6.4   9:56.72  banshee

Renicing the value to -20:

silva@oluwaseunda-silva:~$ sudo renice -20 -p 3293
3293 (process ID) old priority 0, new priority -20
silva@oluwaseunda-silva:~$ _

The value changed to -20:

3293  home    0  -20    277m   64m  35m  S  95.2  6.4   1:32.95 banshee

DF

This utility reports the free disk space (Hard Disk) on all the file systems.

image.png

If you want the above information in a readable format, then use the command

df -h

image.png

FREE

This command shows the free and used memory (RAM) on the Linux system.

image.png

You can use the arguments

free -m - to display output in MB

free -g - to display output in GB

SUMMARY

Any running program or a command given to a Linux system is called a process.

A process could run in foreground or background

The priority index of a process is called Nice in Linux. Its default value is 0, and it can vary between -20 to 19.

The lower the Niceness index, the higher would be priority given to that task.

The following table describes the basic commands used for Process Management in Linux environment.

COMMAND      DESCRIPTION
bg           To send a process to the background
fg           To run a stopped process in the foreground
top          Details on all Active Processes
ps           Give the status of processes running for a user
ps PID       Gives the status of a particular process
pidof        Gives the Process ID (PID) of a process
kill PID     Kills a process
nice         Starts a process with a given priority
renice       Changes priority of an already running process
df           Gives free hard disk space on your system
free         Gives free RAM on your system

Linux process management is a fundamental concept to know if you work on Linux or you will be working on Linux.

That's a wow!

Yes, that's a WOW!

I hope you liked this article. Please click on the like button below, and feel free to let me know in the comments what you think.

Next, I will write about Linux regular expression as well as Input/Output redirection in Linux.

I also talk about #shellscripting, #pythonprogramming #linux, #cprogramming, and #softwareengineering. Please consider following me on Twitter and LinkedIn, and stay tuned for upcoming contents. ✒️.

Thank you for reading, I will see you in the next one.