Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Linux process management related content     - C ++ 11 smart pointers (Programming)

- Linux system security configuration (Linux)

- Oracle 12C RAC optimizer_adaptive_features cause of data into overtime (Database)

- Python implementation Bursa transition model (Programming)

- Setting CentOS firewall open port (Linux)

- 10046 trace only open for a particular SQL statement (Database)

- Lazarus for Raspbian installation (Linux)

- Linux System Getting Started Learning: After starting in Ubuntu or Debian, enter the command line (Linux)

- MongoDB Learning the notes (Database)

- Virtual Judge structures under Ubuntu 14.04 (Server)

- Linux Command Study Notes (Linux)

- JIRA 3.6.2 Upgrade from older version to the new version 6.0.8 (Linux)

- On the PC goes heavy security watch your startup items (Linux)

- Grading defense against Linux server attacks (Linux)

- VMware Workstation + Ubuntu 10.04 Download the Android 2.2 source code (Linux)

- See Linux kernel, CPU, memory, and various versions of the command module and means (Linux)

- CentOS source installation GitLab Chinese Version (Server)

- Linux System Getting Started Learning: Linux command in w (Linux)

- Java data structures - the single linked list LinkedList linear table (Programming)

- Linux system started to learn: how to solve the xxx is not in the sudoers file error (Linux)

 
         
  Linux process management related content
     
  Add Date : 2018-11-21      
         
         
         
  Brief introduction

When we run the program, Linux creates a special program for the environment that contains all the resources needed to run the program, to ensure that the program can run independently, without interference from other programs. This special environment called process.
Each Linux commands and system procedures corresponding to the input command, Linux creates a new process. For example, when you use the ls command to traverse directory file, it creates a process.
In short, the process is an instance of the program.
Five-digit digital tracking system through a program of running, this number is called pid or process ID. Each process has a unique pid.
In theory, five figures is limited, when the numbers are used up, it will start again next pid, pid it will eventually repeat. However, the same two pid process can not exist, because Linux will use the pid to keep track of the program running.

Create Process

There are two ways to create a process: the foreground and background processes.

Foreground process

By default, the process created by the user are foreground processes; foreground process to read data from the keyboard, and the processing results output to the display.
We can see the process running in the foreground process. For example, use ls command to traverse the current directory of files:

$ Ls ch * .doc
ch01-1.doc ch010.doc ch02.doc ch03-2.doc
ch04-1.doc ch040.doc ch05.doc
This program runs in the foreground, it will direct the output to a monitor. If the ls command requires data (actually require), then it will wait for user input from the keyboard.
When the program is running in the foreground, since the command prompt ($) is yet to come, you can not enter other commands; even take a very long time, we must wait for the program to run in order to enter the other end of the command.

Background process

Background process and not necessarily related to the keyboard. Of course, the background process may also be waiting for keyboard input.
Advantage background process is running without waiting for the end of the program you can enter another command.
Create a background process The easiest way is to add at the end of the command &, for example:

$ Ls ch * .doc &
ch01-1.doc ch010.doc ch02.doc ch03-2.doc
ch04-1.doc ch040.doc ch05.doc ch06-2.doc
ch01-2.doc ch02-1.doc
If you need to enter the ls command (do not really need), then it will be suspended until the user bring it to the foreground and continues to run only from the keyboard input data.

View running processes

You can use the ps command to check the running state of the process, including background processes, such as:

$ Ps
PID TTY TIME CMD
18358 ttyp3 00:00:00 sh
18361 ttyp3 00:01:31 abiword
18789 ttyp3 00:00:00 ps
 Can also be combined with the -f option to see more information, f is full of abbreviations, for example:

$ Ps -f
UID PID PPID C STIME TTY TIME CMD
amrood 6738 3662 0 10:23:03 pts / 6 0:00 first_one
amrood 6739 3662 0 10:22:54 pts / 6 0:00 second_one
amrood 3662 3657 0 08:10:53 pts / 6 0:00 -ksh
amrood 6892 3662 4 10:51:50 pts / 6 0:00 ps -f
The meaning of each column is as follows:

Column Description
UID process belongs to the user ID, that is, which the user creates the process.
PID process ID.
PPID The parent process ID, the process to create a process known as the parent process.
C CPU usage.
STIME time process is created.
TTY terminal type related processes.
TIME CPU time used by the process.
CMD command to create the process.

ps command There are some other options:

Option Description
-a displays all processes for all users.
-x display endless process.
-u displays more information, similar to the -f option.
-e show all processes.
Terminate the process

When a process running in the foreground, you can kill command or key combination Ctrl + C to end the process.
If the process is running in the background, the first to get the process ID by the ps command and then use the kill command to "kill" process, such as:

$ Ps -f
UID PID PPID C STIME TTY TIME CMD
amrood 6738 3662 0 10:23:03 pts / 6 0:00 first_one
amrood 6739 3662 0 10:22:54 pts / 6 0:00 second_one
amrood 3662 3657 0 08:10:53 pts / 6 0:00 -ksh
amrood 6892 3662 4 10:51:50 pts / 6 0:00 ps -f
$ Kill 6738
Terminated
As indicated above, kill command ended first_one process.
If the process ignores the kill command, you can kill -9 to end:

$ Kill -9 6738
Terminated
1. Format:

kill [parameters] [process ID]

2. Use this command:

Sends the specified signal to the appropriate process. Model not send SIGTERM (15) to terminate the specified process. Unable to terminate the program if any of the available "-KILL" parameter, a signal is sent SIGKILL (9), will force the end of the process, use the ps command or jobs command to view the process ID. root user will affect users of the process, a non-root user can only affect their process.

3. Command parameters:

-l signal Ruoguo without signal number parameter, the "-l" parameter lists all signal names

-a When dealing with the current process, does not limit the command name and process ID of the correspondence between

-p Specifies the kill command to print only the process ID of related processes, without sending any signal

-s specifies the transmission signal

-u Specifies the user

note:

1, kill command signal number can take the option to be without. If no signal number, kill command will send a termination signal (15), this signal can be captured process so that the process can clean up before exiting and release resources. You can also send a specific signal to the process with the kill. E.g:

kill -2 123

Press Ctrl + C keys its effect is equivalent to running in the foreground when the process with PID 123. However, ordinary users can only use the kill command with no parameters or signal using up to -9 signal.

2, kill may have a process ID number as an argument. When sending a signal to kill these processes must be the owner of these processes. If you try to undo a process does not have permission revoked or revocation of a process that does not exist, you'll get an error message.

3, it can send signals to multiple processes or terminate them.

4, when the kill successfully transmitted signal, shell will be displayed on the screen of the information process terminates. Sometimes the information is not immediately displayed only when you press the Enter key to make the shell command prompt appears again, will be displayed.

5, note that signal to kill the process, which often lead to side effects such as loss of data or the terminal can not be restored to the normal state. Care must be taken when sending a signal, and only as a last resort, used only kill signal (9), because the process can not catch it first. To undo all the background jobs, you can enter kill 0. Because some commands to run in the background starts multiple processes, track and locate the PID to kill all processes it is a very troublesome thing. In this case, use the kill 0 to terminate all processes started by the current shell, it is an effective method.

Signal Description:

Only ninth signal (SIGKILL) can unconditionally terminate the process, other processes have the right to ignore the signal. The following are common signals:

HUP 1 terminal disconnection

INT 2 interrupt (with Ctrl + C)

QUIT 3 Quit (with Ctrl + \)

TERM 15 termination

KILL 9 forced termination

CONT 18 continue (with the STOP contrary, fg / bg command)

STOP 19 suspended (with Ctrl + Z)

Parent and child processes

Each Linux process will include two process ID: the current process ID (pid) and the parent process ID (ppid). You can temporarily believe that all processes have a parent process.
Most commands run by a user will be Shell as the parent process, use the ps -f command to view the current process ID and parent process ID.

Zombie process and orphaned

Under normal circumstances, the parent process will be notified by SIGCHLD signal when the child process is terminated, the parent process can do some clean-up or re-start a new process. However, in some cases, the parent process will be terminated before the child, then the child process, there is no "father", the process is called an orphan.
init process will be the parent of all orphans process. The init pid 1, is the first Linux system process is the parent of all processes.
If a process is terminated, but still use the ps command to see the process, and the state is Z, then this is a zombie process. Although zombie process was terminated, but still present in the list of processes. Usually it is difficult to kill a zombie process, you can kill them first parent process, so that they become an orphan process, init process will automatically clean up zombie process.

Permanent process

Permanent process generally is a system-level process to run as root in the background, it can handle requests from other processes.
Permanent process does not end, you can not access the / dev / tty file, if you use ps -ef to see the process, tty this column displays a question mark (?).
More specifically, the Permanent process usually runs in the background, waiting for the specified event occurs, for example, the printing process is a permanent process, it will wait for user input commands and print-related processing.

top command

top command is a very useful tool, it can dynamically display running processes, you can also specify the conditions according to the process of sorting, similar to Windows Task Manager.
top command to display a lot of information about processes, including physical memory, virtual memory, CPU usage, load average and heavy process and the like. E.g:

$ Top
Tasks and processes

Task (task) is the most abstract, is a general term that refers to an activity done by the software. A task can be either a process, it can be more than one process. In short, it refers to a series of operations to achieve a common purpose. For example, reads data into memory. This task can be achieved by one process can also be achieved by a plurality of processes. Each task has a task number numerals.
  
Process (process) is often defined as the execution of the program. A process can be seen as a stand-alone program with its comprehensive data space and code space in memory. Data and variables have only a process of its own.
jobs command can be used to view the tasks running on the system, including the tasks running in the background. This command displays the task number and its corresponding process ID. A task can be one or more processes corresponding to the number.
jobs -l command option to view the current progress of the task included ID:

$ Jobs -l
[1] + 1903 running ls ch * .doc &
$
Among them, the first column represents the task number, the second column represents the task corresponding to the process ID, the third column represents the task running state, and the fourth column shows the command to start the task.

Switching the foreground and background tasks

fg command to the background task to the foreground, the syntax is:

$ Fg% jobnumber
jobnumber serial number is obtained through jobs command background tasks, note that not pid. If only a background task, you can not specify jobnumber.
bg command to suspend background tasks continue to run to the foreground, the syntax is:

$ Bg% jobnumber
jobnumber same serial number obtained through jobs command background tasks, note that not pid. If the reception is only one task, you can not specify jobnumber.
If you want to transfer the current task to the background, you can Ctrl + z to suspend the task, and then use the bg command. Tasks to the background can empty terminal, continue to enter other commands.
     
         
         
         
  More:      
 
- 3 ways to create a lightweight, durable system of Ubuntu Linux USB disk (Linux)
- CentOS 6.6 command-line automatic completion (Linux)
- Transplant spider to MySQL 5.6 (Database)
- About ORA-02391 solution (Database)
- Ubuntu How to install Pacman (Linux)
- MySQL Basic tutorial: About varchar (N) (Database)
- Python implementation Bursa transition model (Programming)
- SVN hook code set to synchronize Web directory (Server)
- Ubuntu method for single-card dual-IP (Linux)
- Struts2 interceptor simulation (Programming)
- Why you can have JavaScript string method (Programming)
- Nagios plugin installation tutorial of Nrpe (Linux)
- Compile and install the latest version of Redis Stable (Database)
- To batch create users under Linux (Linux)
- Python kills corresponding process according to pid (Programming)
- Package the Python script file into an executable file (Programming)
- ORA-12547: TNS: lost contact error Solution (Database)
- To teach you a trick to find the real IP address (Linux)
- Ten to improve the efficiency of the Linux bash tricks (Linux)
- BGP routers want to play it by CentOS (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.