When we run the program, Linux creates a special environment for the program that contains all the resources that the program needs to run so that the program can run independently and not be interfered with by other programs. This particular environment is called a process.
Each Linux command corresponds to a program in the system and the command is entered, and Linux creates a new process. For example, when you use the LS command to traverse a file in a directory, a process is created.
In short, a process is an instance of a program.
The system runs through a five-bit digital tracker, which is called a PID or process ID. Each process has a unique PID.
Theoretically, the five-digit number is limited, and when the number is exhausted, the next PID will start again, so the PID will eventually repeat itself. However, two PID-like processes cannot exist at the same time because Linux uses PID to track the running state of a program.
Create a process
There are two ways to create a process: foreground and background processes.
Foreground process
By default, the user-created process is the foreground process, and the foreground process reads the data from the keyboard and outputs the processing results to the display.
We can see the process of the foreground process running. For example, use the LS command to traverse a file in the current directory:
$ls ch*.docch01-1.doc ch010.doc ch02.doc ch03-2.docch04-1.doc ch040.doc ch05.doc Ch06-2.docch01-2.doc Ch02-1.doc
This program runs in the foreground, and it outputs the results directly to the display. If the LS command requires data (which is not actually needed), it waits for the user to enter from the keyboard.
When the program is running in the foreground, the user cannot enter additional commands because the command prompt ($) does not appear, and you must wait for the program to finish before you can enter additional commands, even if the program needs to run for a long time.
Background process
Background processes are not necessarily related to keyboards. Of course, background processes may also wait for keyboard input.
The advantage of a background process is that you can enter additional commands without waiting for the program to finish running.
The simplest way to create a background process is to add & at the end of the command, for example:
$ls ch*.doc &ch01-1.doc ch010.doc ch02.doc ch03-2.docch04-1.doc ch040.doc ch05.doc Ch06-2.docch01-2.doc Ch02-1.doc
If the LS command needs to be entered (which is not actually required), it pauses until the user is transferred to the foreground and the data is entered from the keyboard to continue running.
To view a running process
You can use the PS command to view the running status of a process, including background processes , such as:
$psPID TTY time CMD18358 ttyp3 00:00:00 sh18361 ttyp3 00:01:31 abiword18789 ttyp3 00:00:00 PS
You can also combine the- f option to see more information, and F is the abbreviation for full, for example:
$ps-fuid PID PPID C stime TTY time cmdamrood 6738 3662 0 10:23:03 pts/6 0:00 First_oneamrood 6739 3662 0 10:22:54 pts/6 0:00 second_oneamrood 3662 3657 0 08:10:53 pts/6 0:00-kshamrood 6892 3662 4 10:51:50 p TS/6 0:00 ps-f
Each column has the following meanings:
| column |
Description |
| Uid |
ID of the user to which the process belongs, that is, which user created the process. |
| Pid |
The process ID. |
| PPID |
The parent process ID, the process that created the process is called the parent process. |
| C |
CPU usage. |
| Stime |
The time that the process was created. |
| Tty |
The terminal type associated with the process. |
| Time |
The CPU time used by the process. |
| Cmd |
The command that created the process. |
The PS command has several other options:
| Options |
Description |
| -A |
Displays all processes for all users. |
| -X |
Displays a process without a terminal. |
| -U |
Displays more information, similar to the-f option. |
| -E |
Show all processes. |
Terminating a process
When the process is running in the foreground, the kill command or CTRL + C key combination can be used to end the process.
If the process is running in the background, first get the process ID through the PS command, and then use the kill command to "kill" the process , for example:
$ps-fuid PID PPID C stime TTY time cmdamrood 6738 3662 0 10:23:03 pts/6 0:00 First_oneamrood 6739 3662 0 10:22:54 pts/6 0:00 second_oneamrood 3662 3657 0 08:10:53 pts/6 0:00-kshamrood 6892 3662 4 10:51:50 p TS/6 0:00 Ps-f$kill 6738Terminated
As shown above, the KILL command ends the First_one process.
If the process ignores the kill command, it can be ended by kill-9:
$kill-9 6738Terminated
Parent and child processes
Each Linux process consists of two process IDs: the current Process ID (PID) and the Parent process ID (ppid). You can temporarily assume that all processes have a parent process.
Most commands run by the user use the Shell as the parent process , using the ps-f command to view the current process ID and the parent process ID.
Zombie process and orphan process
Normally, when a child process is terminated, the parent process is notified through the SIGCHLD signal, and the parent process can do some cleanup work or restart a new process. In some cases, however, the parent process is terminated before the child process, so the child processes do not have a "father", which is called the orphan process .
The init process becomes the parent process for all orphan processes. Init has a PID of 1, which is the first process of the Linux system and the parent process of all processes.
If a process is terminated, but the process can still be viewed with the PS command, and the status is Z, then this is a zombie process . The zombie process is terminated, but it still exists in the process list. The general zombie process is hard to kill, you can kill their parent process first, let them become orphans process, the init process will automatically clean up the zombie process.
Resident process
The resident process is typically a system-level process that runs in the background with root privileges and can handle requests from other processes.
The resident process does not have a terminal, cannot access the/dev/tty file, and if you use PS-EF to view the process, the TTY column displays a question mark (?).
Rather, the resident process typically runs in the background, waiting for a specified event to occur , such as a print process that is a resident process, which waits for the user to enter and process the relevant command for printing.
Top command
The top command is a useful tool that can dynamically display a running process and also sort the process by the criteria you specify, similar to the Task Manager for Windows.
The top command can display a lot of information about the process, including physical memory, virtual memory, CPU usage, average load, and busy processes . For example:
$top
Here's just one, and it's best for the reader to run it yourself:
Tasks and processes
A task is the most abstract and a generic term that refers to an activity done by the software. a task can be either a process or multiple processes. In short, it refers to a series of operations that collectively achieve a purpose. For example, read the data and put the data in memory. This task can be implemented by a single process, or by multiple processes. Each task has a number representing the task number.
Process is often defined as the execution of a program. You can think of a process as a separate program that has complete data space and code space in memory. The data and variables owned by a process belong to itself only.
The Jobs command can be used to view tasks that are running in the system, including tasks that run in the background. The command can display the task number and its corresponding process ID. A task can correspond to one or more process numbers.
The-l option of the jobs command can see the process ID that the current task contains:
$jobs-l[1] + 1903 running ls Ch*.doc &$
Where the first column represents the task number, the second column represents the process ID of the task, the third column represents the running state of the task, and the fourth column represents the command to start the task.
Switching of foreground and background tasks
The FG command can transfer background tasks to the foreground with the following syntax:
$FG%jobnumber
Jobnumber is the ordinal of the background task obtained through the jobs command, note that it is not a PID. If there is only one task in the background, you can not specify Jobnumber.
The BG command can move the background paused task to the foreground to continue running , with the following syntax:
$BG%jobnumber
Jobnumber is also the serial number of the background task obtained through the jobs command, note that it is not PID. If the foreground has only one task, you can not specify Jobnumber.
If you want to move the current task to the background, you can pause the task with Ctrl + Z and then use the BG command. The task is moved to the background to empty the terminal and continue to enter other commands.
Linux Learning Series 8---process management