Linux Analysis Process
One: Theoretical knowledge
1: Before we get to know the Linux process, let's start by understanding how the shell script executes the command.
The shell is also a program, the biggest difference with other programs is that it is designed to execute other programs, not to engage in calculations, drawings, storage and other specific transactions. Analyzing how the shell works helps break the shell's mystique and better command the shell. Taking Ls-l *.c as an example
-
1, the shell constantly asked whether there is keyboard input, the user to enter the end of the keyboard input, the shell program with a space separator, get "ls", "-L", "*.c", three strings.
-
2, the shell found that the third string contains the "*" wildcard character, the current path (or the specified path) to search for a file that satisfies this wildcard character. There are two files for bar.c and foo.c, then expand into {"LS", "-L", "bar.c", "FOO.C"} Four strings, and save in the argv of this two-dimensional character array, and assign the value argc to 4.
-
3. The shell then searches for the path that the global variable holds, looking for a command called "LS". If present, fork () is a child process that calls the Exec () load to run the LS command. The parameters of LS's main (int argc, String * * argv) function are derived from the shell. If it does not exist, it will prompt the user "command not found".
-
4, the LS program after execution, will analyze argc and argv, "-L" indicates that this is an option switch, indicating that the user wants the details of the file, and to save this switch amount, so that the final output of the time to choose the format of information.
-
5, LS Executes the established steps, exit and return the exit code, if successful, return 0, otherwise, depending on the error of the different return different non 0 numbers.
-
6, the shell after the successful exit code, continue to wait for user input
Reference hjx5200 the documentation
As above: The command is in the process of execution,
command-to-binary---> Run----> To load the corresponding---> Draw a conclusion in this process, the recorded things will produce a process;
2. Process: The system does not finish the work that the CPU does not complete, the running Program type: Program process/service process (Eg:apache) resident in memory a program runs at least one process
In the kernel view, processes are entities, and the kernel must share various computer resources between them. For restricted resources such as memory, the kernel initially allocates a certain amount of resources to the process and adjusts the resource requirements for the process and the system as a whole during the life cycle of the process. When the program terminates, the kernel frees all such resources for reuse by other processes. Other resources, such as CPUs, network bandwidth, and so on, are renewable resources, but must be shared equally among all processes.
3. Role of Process Management:
Determine the health status of the server
View all processes in the system
Kill process
4: Memory layout of the process
Logically dividing a process into the following sections (also known as segments)
Text: Instructions for the program.
Data: Static variables used by the program.
Heap: Programs can dynamically allocate additional memory from this area.
Stack: Random number calls, returns a piece of memory, used to allocate storage space for local variables and function call link information.
5: Creating processes and executing programs
A process can use System call fork () to create a new process. The process called fork () is called the parent process, and the newly created process is called a child process. The kernel creates child processes by copying the parent process. After a child process inherits a copy of the data segment, stack segment, and heap segment from the parent process, it can modify the content without affecting the parent process's "original" content. (program text segments that are marked as read-only in memory are shared by the parent, child processes)
The child process then either executes another set of different functions in the code that is shared with the parent process, or, more commonly, uses the system call EXECVE () to load and execute a completely new program. Execve () Destroys existing text segments, data segments, stack segments, and heap segments, and replaces them with new ones, based on the code of the newly created program.
6: Process ID and parent process ID
Each process has a unique integer process identifier (PID). In addition, each process also has a parent process identifier (PPID) attribute that identifies the request kernel to create its own process.
7: Process termination and termination status
You can use one of the following two ways to terminate a process: First, the process can use the _exit () system call (or the associated exit () library function) to request an exit, and second, pass the signal to the process and "kill" it. Regardless of the way you exit, the process generates a "terminating state", a nonnegative small integer that can be detected by the parent process's wait () system call. In the case of calling _exit (), the process indicates its own termination state. If the process is "killed" by a signal, the process's terminating state is set according to the type of signal that caused the process to "die". (sometimes the argument passed into the _exit () is called the "exit state" of the process to indicate a difference from the terminating state, which refers to the parameter value passed to _exit (), or the signal to "kill" the process. )
By convention, a terminating state of 0 indicates that the process "retire", and not 0 indicates an error occurred. Most shells save the last executing program's terminating state in the shell variable $?.
Second: status of the process
1:system Monitor (Command open:) state: Status:1:sleeping (can be awakened when used)
2:running
3:stop (No automatic wake-up can be done manually, the CPU can pass the information used here to the process)
2. State switch: The system recycles the cache information resource on the CPU/uses some programs
3. System resources: Hard Disk CPU Memory
CPU-level caching (directly available)
Second-level cache
Memory (required to fetch) if the in-memory resource is reclaimed, the process shuts down
4:ps default view of processes generated at the shell
III: Some of the commands involved in the process
# # # #1. PS Command # # #
Ps
A # #关于当前环境的所有进程
X # #与当前环境无关的所有进程
F # #显示进程从属关系
E # #显示进程调用环境工具的详细信息
L # #长列表显示进程的详细信息
U # #显示进程的用户信息
Add-and-no-difference:
PS aux #查看系统中所有进程, using BSD OS format
Ps-le # View all processes in the system, using the Linux operation format
PS aux
USER PID%cpu%MEM VSZ RSS TTY STAT START time COMMAND
User: Who generated the process
PID: ID number of the process
%CPU: The percentage of CPU resources that the process consumes
%mem: The process occupies a percentage of physical memory
VSZ: The size of the virtual memory that the process occupies
RSS: The process consumes the actual memory size
TTY: at which terminal the process Tty1-tty7 represents the local console, pts/0-255 represents the virtual terminal
State:
R: Run
S: Sleep
T: Stop state
S: Contains child processes
+: In the background
PS Ax-o%cpu,%mem,user,group,comm,nice,pid # #指定显示进程的某些信息
%CPU # #显示进程cpu负载
%mem # #显示进程内存负载
User # #进程用户
Group # #进程组
Comm # #进程名称
Nice # #进程优先级
PID # #进程的id
### #2. Front and back calls to processes in the Environment # # # #
jobs # #查看被打入环境后台的进程
ctrl+z # #把占用终端的进程打入后台
fg # #把后台进程调回前台
bg # #把后台暂停的进程运行
Comm & # #让命令直接在后台运行
## #7. Top command # # #
top # #监控系统负载工具
top and PS distinguish
# #ps看到的是命令执行瞬间的进程信息, top can monitor the
# #ps只是查看进程 continuously, and top can also monitor system performance such as average load, CPU, Memory
# #top可以直接操作进程, such as changing the priority (command R) and closing process (command K)
This article is from the "12462896" blog, please be sure to keep this source http://12472896.blog.51cto.com/12462896/1950166
Linux Analysis Process