Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Linux?
3.
What is a Process?
3.1.
Foreground Process
3.2.
Background Process
3.3.
Types of Processes
4.
Process Management
4.1.
System Monitor
4.2.
top Command 
4.3.
ps Command
4.4.
nice Command
4.5.
Suspending a Process 
4.6.
Convert the Process into a Background Process
4.7.
Background Process into Foreground Process
4.8.
kill Command
5.
Frequently Asked Questions.
5.1.
What is PPID?
5.2.
Can we see the CPU usage of the application or process in the system?
5.3.
Can we generate a report of the process duration in Linux?
5.4.
Can we terminate a process without process id?
5.5.
How many status codes are available in Linux?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Linux - Process Management

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In this blog, we will discuss the processes in Linux and the types of processes in Linux. We will also discuss how we can manage a process in Linux. We will discuss some commands necessary for Linux users to know if they want to manage the processes in Linux.  Let’s get started.

What is Linux?

Linux is an open-source kernel that Linus Torvalds developed. A kernel is software whose role is to communicate between the application and hardware of a system. By open source, I mean its source code is publicly available, which you can contribute to if you have enough knowledge about Linux.

linux

Linux is a highly customizable kernel with various distributions, like Debian, Ubuntu, Kali, and many more. In today's world, Linux is used on multiple devices around you; for example, android devices are based on Linux.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is a Process?

Before learning process management, let’s build some foundation by discussing what a process means. 

what is a process in linux?

A process is any application, program, or command running in the terminal. Starting an application, for example when you open a browser will be labeled as a process in Linux.

Each process in a Linux system has some state; for example, a process might be in a running state, a stopped state, or a terminated state. We will learn how we can manage each of these states in Linux.

Before we know about the type of processes in a Linux system, we need to understand two things:

Foreground Process

Any current or active process running on the terminal, a user cannot execute other processes until the previous process is done, is known as a foreground process. A foreground process requires input from the user and displays the output on the screen, meaning the user must interact with the process.

Example

The sleep command can be an excellent command to demonstrate a foreground process.

sleep command

When we type sleep 360 in the terminal, we cannot interact with the terminal for 360 seconds; as you can see, we have typed several commands, and none of them is being executed because the sleep command has to execute first.

Background Process

A background process is a process that runs in the background of the system, and the user does not need to interact with the process for input to keep the process running. If a background process does require some user input, it will be stopped until it is converted into a foreground process. You can make a foreground process into a background process and execute another task as your previous process is being executed in the back of the system

Further in the article, we will learn how to implement a foreground and background process and how to convert one into another.

Types of Processes

Now, will discuss the different types of processes in the Linux environment. There are multiple processes in Linux and each has a specific role in the system, and some are connected to each other.

Parent Process
Each process has a unique process id or PID and PPID, which means Parent process ID.
A parent process is a process that creates another process from it, which is known as the child process. Most processes in Linux have parent processes, but there are exceptions, like system processes executed by the Linux kernel itself.

Child Process
Any process created through another process is known as a child process. We can check the parent of a particular process through the PPID that got assigned to the child process. 

Zombie Process
Some processes in Linux are labeled zombie processes because even after they are executed or terminated, we can still see them in our process lists. A process with z status is labeled as a zombie process.

Daemon Process
System-related background processes known as daemons are operated under root permissions and respond to service calls from other processes. All daemons will have a at the end of the process name, like systemd, syslogd, etc.

daemon process

Process Management

Now, let's discuss how to manage processes in a Linux environment. 

process management in linux

It is necessary to know about process management because Linux has a complex environment compared to other operating systems, and you may encounter some problems where you cannot shut down a task or terminate an application or process; in these cases, you must know about process management. You must know some necessary commands in Linux if you want to manage or observe the processes.

System Monitor

There is an application pre-installed in the Linux environment, which is known as a system monitor. The system monitor is like the task manager in the window, which assists you to manage the processes currently running in your system.

With the help of a system monitor, you can kill, stop, end or continue the process accordingly. You can also set the priority to a process if you wish.

system monitor
system monitor

top Command 

First, we will discuss the top command in Linux. It is a handy Linux system performance command which displays all the processes running in the system with Process ID, PPID, CPU usage, memory usage, usernameNI ( nice value), and command name of the running process.

top command
top command

ps Command

The ps stands for process status, a command in Linux that helps you display the snapshot of the active process running in the system and how long they have been running with the process IDs. if you type only the ps in the terminal, it will show the recent running process with less information.

ps command

There are multiple flags available for the ps command, and each has a specific role.

-a : Select all processes excluding session leaders and processes not associated with a terminal.
 

-u : It shows the information of the process with the currently active user.
 

-e : Shows all processes in detail,,, and with the -ef flag, you can see the additional information.

ps command

Column name with Description

UID - UID stands for user id which shows the id of the user who is running that process.
PID - PID stands for process ID which shows the id of a particular process.
PPID - PPID means parent process ID and this column shows the parent ID of the process.
C- C column shows the CPU utilization of the process.
STIME - It shows the process time.
TTY - TTY means terminal type and it displays the type of terminal process.
TIME - It Shows the time taken by the CPU.
CMD - This column shows the command that initiated the process.

nice Command

There is a command known as nice in Linux which helps to set a priority for a process. There is a nice index that ranges from -20 to 19 the lower the nice index the higher the priority of the process. By default, the nice index is 0 in most processes. You can check for the nice value of a process using the top command. The NI column shows the nice value.

top command

You just need to type nice -n ‘nice value” process _name in the terminal to set the priority for a process.

nice command

Before using the nice command

nice command


After using the nice command

nice command

Suspending a Process 

With the ctrl + z, you can suspend any currently running process, and the system will pin it into the job list with the job id. You can see the suspended process with the job command.

With ctrl+z

ctrl z command

Convert the Process into a Background Process

Now that you have suspended a process, you can convert it into a background process with the help of the bg command. You just need to type bg %job-id, and the process will run in the background, or we can just addat the end of the command to make it a background process, for example, sleep 4000 &.

& command


In image2 the current status of the sleep command is stopped, but when typing the bg %3 where 3 is the process's jobID, it is converted to a background process, and then again check the process status in jobs, we can see it is running in image4.

If we don't provide any job id or process id for the command bg by default, it will pick the latest suspended command from the jobs.
 

bg command
bg command

Background Process into Foreground Process

Converting the background process into the foreground process is very easy; you need to type fg %jobid or processid, and the process will be back into the foreground.

fg command

As you can see, the sleep command is back in the foreground, and we cannot interact with the terminal again.

kill Command

So far, we have learned how to check out the process status or convert the background process into the foreground process or vice versa. But now we will know how to terminate the process from the system, which is helpful.

You can terminate the process simply by using ctrl + c in the terminal, but it is sometimes not recommended or useful. So we will discuss a more organized way to terminate a process with the help of a kill command. To kill or terminate a process can just type the kill process_ID, and the process will be terminated from the system.
 

➤First, we will execute a sleep process and run it in the background using the bg command.

kill command

 

➤Then we check the process id for sleep 500 processes using the ps -a command.

kill command

 

➤At last, we will terminate the process using the kill command with process id 151. As you can see in the image below, it has been terminated,

kill command

 

➤Keep in mind that the kill sends the signal to the process to terminate the process. Some processes can neglect this signal and still be active in the system. Like in the image below, we can see that the process is still in jobs even after using the kill command.

kill command

 

➤You can deal with these processes by changing the signal for the kill command; There are multiple signals available in the kill command. To completely terminate the process using kill -9 process_ID, your process will be terminated without any second thought. The kill -9 means terminating the process immediately. In the below example, 92 is the process_id for sleep 600.

kill command

 

➤You can use the name of the process by using the killall or pkill command in the terminal. Typing killalll process_name or pkill process_name will terminate the process.

killall command

We hope you have understood everything about process management in Linux.

Frequently Asked Questions.

What is PPID?

PPID stands for parent process ID, which is the process id of a child's parent.

Can we see the CPU usage of the application or process in the system?

Yes, with the help of the top command in Linux, we can see the various factors, including CPU usage.

Can we generate a report of the process duration in Linux?

The ps command in Linux generates the report of the current processes.

Can we terminate a process without process id?

Yes, we can terminate a process with the process name using the killall or pkill command.

How many status codes are available in Linux?

There are multiple status codes: ' D’ = uninterruptible sleep, ‘R’ = running, ‘S’ = sleeping, ‘T’ = traced or stopped, and ‘Z’ = zombie.

Conclusion

In this blog, we discussed what a process is in Linux and the types of processes in Linux. We have also learned how to manage the processes in a Linux environment with the help of multiple commands. We have also discussed what is a foreground process and background process.

To learn more about Linux commands, check out the following articles.

To learn more about DSA, competitive coding, and many more knowledgeable topics, please look into the guided paths on Coding Ninjas Studio. Also, you can enroll in our courses and check out the mock test and problems available to you. Please check out our interview experiences and interview bundle for placement preparations.

Happy Coding!

Previous article
How to Check SD Card in Linux?
Next article
Network Troubleshooting Tools in Linux
Live masterclass