Everything you need to know about Linux processes

Summary: process refers to an executing program; an instance of a program that is running. It consists of program instructions, and data read from files, other programs, or input from the system user. Types of Processes There are two main types of processes in Linux: Foreground processes (also known as interactive processes) - These processes are initialized and controlled by a terminal session.

process refers to the program being executed; an instance of the program being run. It consists of program instructions, and data read from files, other programs, or input from the system user. Types of Processes There are two main types of processes in

Linux: foreground processes (also known as interactive processes) - these processes are initialized and controlled by a terminal session. In other words, there needs to be a user connected to the system to start such processes; they are not started automatically as part of a system function/service. Background processes (also known as non-interactive/automatic processes) - These processes are not connected to a terminal; they do not require any user input. What is a daemon A daemon is a special type of background process that starts at system boot and runs as a service all the time; they don't die. They are started spontaneously as system tasks (run as services). However, they can be controlled by the user through the init process. Linux Process State Linux Process State Creating a Process in Linux A new process is created when an existing process makes a full copy of itself in memory. The child process will have the same environment as the parent process, only the process ID is different. There are two general ways of creating processes in Linx: using the system() function - this method is relatively simple, but inefficient and has obvious security implications.
















Use the fork() and exec() functions - this trick is more advanced but provides better flexibility, speed, and security.
How does Linux identify processes?
Since Linux is a multi-user system, meaning that different users can run a wide variety of programs on the system, the kernel must uniquely identify each instance of a program running.

A program is identified by its process ID (PID) and the process ID (PPID) of its parent process, so processes can be classified as:

parent processes - these are processes that create other processes at runtime.
Child Processes - These are processes that are created by other processes at runtime.
init process
The init process is the parent process of all processes in the system, and it is the first program to run after starting the Linux system; it manages all other processes on the system. It is started by the kernel itself, so in theory it has no parent process.

The process ID of the init process is always 1. It is the adoptive parent of all orphan processes. (It will adopt all orphaned processes).

Find Process ID
You can use the pidof command to find the process ID of a process:

# pidof systemd
# pidof top
# pidof httpd
Find Linux Process ID
Find Linux Process ID

To find the process ID of the current shell and the process ID of its parent, run :

$ echo $$
$ echo $PPID
Find Linux Parent Process ID
Find Linux Parent Process ID

Starting a Process in Linux
Every time you run a command or program (eg cloudcmd - CloudCommander), it starts a process on the system. You can start a foreground (interactive) process as follows, it will be connected to the terminal and the user can send input to it:

# cloudcmd
start linux interactive process
start linux interactive process

linux background task
to be in background (non-interactive) Start a process, using the & symbol, at which point the process will not read input from the user until it is brought to the foreground.

# cloudcmd &
# jobs
start a linux process
in the background start a linux process in the background

You can also use Ctrl + Z to suspend the execution of a program and send it to the background, it will send a SIGSTOP signal to the process, thus suspending its execution; it will Become idle:

# tar -cf backup.tar /backups/* ### Press Ctrl+Z
# jobs
To continue running the suspended command above in the background, use the bg command:

# bg
To send the background process to the foreground, Use the fg command with the ID of the task, something like:

# jobs
# fg %1
Linux daemon tasks
Linux daemon tasks

You may also want to read: How to Launch Linux Commands in the Background and Detach Processes in Terminal

The state of a process in Linux
During execution, a process transitions from one state to another depending on its environment. In Linux, a process has the following possible states:

Running - At this point it is running (it is the current process in the system) or ready to run (it is waiting for CPU units to be allocated).
Waiting - In this state, the process is waiting for an event or system resource. Additionally, the kernel also differentiates between two different types of waiting processes; interruptible waiting processes - which can be interrupted by a signal, and uninterruptible waiting processes - which are waiting for a hardware condition and cannot be interrupted by any event/signal.
Stopped - In this state, the process has been stopped, usually due to a signal. For example, the process being debugged.
Zombie - The process has died, it has stopped but the process table still has its entry in the process table.
How to View Active Processes in Linux
There are many Linux tools that can be used to view/list running processes in the system, two traditionally well known are the ps and top commands:

1. The ps command
it displays the active processes of the selected system information, as shown in the following figure:

# ps
# ps -e | head
List Linux Active Processes
List Linux Active Processes

2. top - System Monitoring Tool
top is a powerful tool that can give you a dynamic real-time view of the running system , as shown in the screenshot below:

# top
List Linux Running Programs
List Linux Running Programs

Read this article for more top use cases: 12 examples of top command in Linux

3. glances - system monitoring tool
glances is a relatively new system monitoring tool, It has some more advanced features:

# glances
Glances Linux Process Monitoring
Glances - Linux Process Monitoring

For a complete usage guide, read: Glances - an advanced real-time system monitoring tool for Linux

There are many more that you can use to list active processes Other useful Linux system monitoring tools, open the link below to learn more about them:

20 Command Line Tools to Monitor Linux Performance
13 Useful Linux Monitoring Tools
How to Control Processes in
Linux Linux also has some commands for controlling processes , such as kill, pkill, pgrep, and killall, here are some basic examples of how to use them:

$ pgrep -u tecmint top
$ kill 2308
$ pgrep -u tecmint top
$ pgrep -u tecmint glances
$ pkill glances
$ pgrep -u tecmint glances
Control Linux processes
Controlling Linux Processes

To learn more about how to use these commands to kill/kill active processes in Linux, follow the link:

Kill, Pkill and Killall Commands for Killing Linux Processes Guide
How
to Find and Kill Processes in Linux You can use them to kill unresponsive programs in Linux when your system freezes.

Sending Signals
to Processes The basic way to control processes in Linux is to send them signals. You can send many signals to a process, run the following command to see all signals:

$ kill -l
list all linux signals
list all linux signals

To send a signal to a process, use the kill, pkill or pgrep command. But programs can only respond to these signals if they are programmed to recognize them.

Most of the signals are used internally by the system, or when the programmer is writing code. Here are some useful signals for system users:

SIGHUP 1 - This signal is sent to a process when the terminal controlling it is closed.
SIGINT 2 - The terminal controlling the process sends this signal to the process when the user interrupts it with Ctrl+C.
SIGQUIT 3 - This signal is sent to the process when the user sends the exit signal Ctrl+D.
SIGKILL 9 - This signal will immediately interrupt (kill) the process, and the process will not perform cleanup operations.
SIGTERM 15 - This is a program termination signal (kill sends this signal by default).
SIGTSTP 20 - Its controlling terminal sends this signal to the process asking it to stop (terminal stop); triggered by the user pressing Ctrl+Z.
Here's an example of the kill command that kills a Firefox application by its PID when it's dead:

$ pidof firefox
$ kill 9 2687
or
$ kill -KILL 2687
or
$ kill -SIGKILL 2687
Kill the application by its name, like below Use pkill or killall like this:

$ pkill firefox
$ killall firefox
Change Linux process priority
On a Linux system, all active processes have a priority and a nice value. A process with a higher priority than a point-priority process generally gets more CPU time.

However, a system user with root privileges can influence (change) the priority using the nice and renice commands.

In the output of the top command, NI displays the nice value of a process:

$ top
lists Linux running processes
List Linux running processes

Use the nice command to set the nice value for a process. Remember that a normal user can set nice values ​​from 0 to 20 for processes he owns.

Only the root user can use negative nice values.

To reset the priority of a process, use the renice command like this:

$ renice +8 2687
$ renice +8 2103
Read our other helpful articles on how to manage and control Linux processes.

Linux Process Management: Start, Stop, and Intermediate Processes Find the Top 15 Processes
Using the 'top' Command in Batch Mode Find the Processes with the Highest
Memory and CPU Utilization
in Linux How to Find Process Name Using Process ID in
Linux For: 2017-04-27

This article is from Yunqi Community Partner "Linux China"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326397006&siteId=291194637