Do you think IIT Guwahati certified course can help you in your career?
No
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 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.
What is a Process?
Before learning process management, let’s build some foundation by discussing what a process means.
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.
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 d at the end of the process name, like systemd, syslogd, etc.
Process Management
Now, let's discuss how to manage processes in a Linux environment.
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.
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, username, NI ( nice value), and command name of the running process.
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.
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.
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.
You just need to type nice -n ‘nice value” process _name in the terminal to set the priority for a process.
Before using the nice command
After using the 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
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 add & at the end of the command to make it a background process, for example, sleep 4000 &.
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.
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.
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.
➤Then we check the process id for sleep 500 processes using the ps -a 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,
➤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.
➤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.
➤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.
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.