Detailed process and probing process

The concept of process

In short, a process is a program that is being executed

As I said before, the first step of program execution in Windows is to double-click the program Linux is ./. When the system receives this command, it will load the program code from the disk into the memory and allocate space for the program.

When the program is loaded into memory and builds its own PCB , it is a process.

What is a PCB

The concept of PCB: a data structure containing various information and attributes of the process , such as process ID, user ID, group ID, process status, priority, I/O status information and program data, etc.
insert image description here
PCB is the name (process control block) in general textbooks . The entity called PCB under the Linux operating system is: task_struct

The role of task_struct

task_struct is a data structure of the Linux kernel , it will be loaded into RAM (memory) and contains process information

It contains roughly:

1. 标示符: 描述本进程的唯一标示符,用来区别其他进程。
2. 状态: 任务状态,退出代码,退出信号等。
3. 优先级: 相对于其他进程的优先级。
4. 程序计数器: 程序中即将被执行的下一条指令的地址。
5. 内存指针: 包括程序代码和进程相关数据的指针,还有和其他进程共享的内存块
   的指针
6. 上下文数据: 进程执行时处理器的寄存器中的数据,可以理解为存储的是程序
    已经做了什么,下一步该做那些
8. I/ O状态信息: 包括显示的I/O请求,分配给进程的I/ O设备和被进程使用
   的文件列表。
9. 记账信息: 可能包括处理器时间总和,使用的时钟数总和,时间限制,记账号
   等。
   其他信息等

how to execute process

First of all, we need to understand that all your commands and operations you can perform under Linux are related to permissions.

When any event is triggered, the system will define it as a process and give the process a unique ID called PID. At the same time, according to the relationship between the user who triggered the process and related attributes, the process PID will be given corresponding permissions.

Running the program is also subject to permission constraints , so task_struct must store the necessary attribute information

Under normal circumstances, task_struct will be arranged in memory like a linked list, one by one
, but we need to know that everything must be prioritized, especially when the operating system executes the process.

Therefore , there is a priority attribute in the task_struct corresponding to the process. It uses the PRI value to represent the priority. The smaller the PRI value, the higher the priority . Therefore, in the figure below, the PRI of 60 will be preferred over the PRI of 80. Get the resource
insert image description here
priority of the CPU: it is the order in which the CPU allocates resources

There are only two PRI values ​​in the above figure for your convenience to understand. In fact, the PRI value is determined according to the process attribute (NI value will also affect the process priority and will be added later)

The necessity of priority: There are many system processes, but only a small amount of CPU resources, or even one, so there is competition between processes. In order to efficiently complete tasks and compete for related resources more reasonably , they have priorities.

process profiling

Having said so much, it’s time to see the process. Under
Linux, you can use the ps command to view the process
. Command: ps
Function: View the process belonging to the current user.
insert image description here
In the above picture, you can see the process of the current ps command and bash after entering ps and pressing Enter. From this , it can be concluded that the command is just a shortcut to a certain program, and a process will also be created when it is executed.

what is bash

Each user will create a bash process when logging in, and the bash processes between users have no influence on each other.

1. bash是一个命令处理器, 运行在文本窗口中, 并能执行用户直接输入的命令.
2. bash还能从文件中读取Linux命令, 称之为脚本.
3. bash支持通配符, 管道, 命令替换, 条件判断等逻辑控制语句 

Summary: bash can be called a command line interpreter, and the processes we execute on the command line are all derived from it.

Use of the ps command (view process)

First understand what each item in the process information list means
insert image description here

F :内核分配给进程的系统标志
UID : 启动该进程的用户
PID : 进程ID
PPID : 父进程ID(如果该进程是其他进程启动的)
PRI : 进程优先级(值越小,优先级越大)
NI :优先级的修正值
VSZ : 进程占用虚拟内存空间的大小
RSS :进程在未被交换出时占用的物理内存大小
WCHAN :进程休眠的内核函数地址
STST : 代表当前进程状态(R是可运行正在等待;O是正在运行;S代表休眠;Z代表僵化,已终止但找不到其父进程 ;T代表停止)

The above briefly demonstrates the ps command, but it only displays two very short lines of content, which is obviously not what we want. The following
demonstrates the BSD-style
command of ps command: ps la
Function: use long format to display all processes associated with any terminal
insert image description here
It can be seen that several processes are displayed, among which there are two bash processes, because there are now two accounts logging in here.

The COMMAND in the right column is the program name. The ./my_exe program in the figure is just a program currently running on another account. Its PID is 19795, and the PPID indicates the parent process. The ./my_exe program PPID is 18505, and 18505 happens to be bash
. PID.

So come to the second conclusion: the commands executed on the Linux command line, the processes generated are all derived from the bash process, that is to say, the PPID (parent process) of the processes generated on the command line are all bash

The following figure shows the command options of ps, since they are all query options, I will not demonstrate them one by one

options describe
T Show all processes associated with the current terminal
a Show all processes associated with any terminal
g Show all processes including the controlling process
r Show only running processes
x Show all processes, including those not assigned any terminal
U userlist Display processes owned by a user ID in the userlist list
p pidlist Display the process whose PID is in the pidlist list
t ttylist Show processes associated with a terminal in the ttylist list
the format In addition to the standard columns, also output the columns specified by format
x Display data in register format
z Include security information in output
j Show Job Information
l Display in long format
the format display only the columns specified by format
s Display in signal format
u Display in user-based format
v Display in virtual memory format
N namelist Defines the value to be displayed in the wCHAN output column
o order Define the display order of the columns of information
s Summarize numerical statistics (such as CPU and memory usage) of child processes into the parent process
c Show the real command name (the name of the program that started the process)
e Show the environment variables used by the command
f Display processes in a hierarchical format, showing which processes started which processes
h do not display header information
k black specify the column used to sort the output
n Use numeric values ​​to display user ID, group ID, and wCHAN information
w Generate wide output for wider terminal screens
H show threads as processes
m Show threads after processes
L list all format specifiers
v Show version of ps command

create process

Method: A process can use the system call fork() function to create a new process.

The process that calls fork() is called the parent process, and the newly created process is called the child process
as follows: Create a child process in main
insert image description here
The result of the journey is as
insert image description here
follows After using fork() to create the child process, bbbbbb... It was printed twice, but aaaa... the output is normal and only printed once
to explore why we use two functions to help us watch the results
Function: getpid()
Function: Return the current process PID

Function: getppid()
Function: Return the current process PPID (parent process)
and re-edit the code as follows:

#include<unistd.h>  
#include<stdio.h>  
 int main()  
  {
    
      
	  printf("aaaaaaaaaaaaaaaaaaa\n");  
      fork(); //创建一个子进程  
      printf("我的PID :%d  我的PPID :%d \n",getpid(),getppid());                                  
      return 0;  
   }  

After executing the program, the result is as follows.
insert image description here
It can be seen that the first one that prints out its own PID 6389 is the parent process , because the second one that prints out its own PPID is 6389, so PID 6390 is the child process

But it also shows that the code executed by the parent and child processes is the same.

Exploring the parent-child process

Check out the fork's description:

1:如果子进程开启成功则给父进程返回子进程的PID,否则返回-1
2:会给子进程返回0

From this I have the following procedure:

  1 #include<unistd.h>
  2 #include<stdio.h>
  
  3 int main()
  4 {
    
    
  5   int n = 5;
  6   int i  = 1;
  7   printf("aaaaaaaaaaaaaaaaaaa\n");
  8  int pp =  fork(); //创建一个子进程
  9  if(pp == 0)//子进程程序
 10  {
    
    
 11     while(n--)
 12     {
    
    
 13       printf("我是子进程,i = %d 我的PID :%d  我的PPID :%d \n",i ,getpid(),getppid());
 14       sleep(1); i =99;//把值改掉
 15     }
 16  }
 17  else
 18  {
    
                                                                                                
 19     while(n--)
 20     {
    
                 
 21       printf("我是父进程,i = %d 我的PID :%d  我的PPID :%d \n",i ,getpid(),getppid()); sleep(1);
 22     }     
 23  }
 24   return 0;
 25 }

As shown in the figure above, the pp variable will receive the return value of fork() (currently, the process creation failure is not considered), and the codes of the parent process and the child process have been described above , but the fork() received by the pp variable in the parent process The return value is the PID of its child process, and the return value of fork() received by the pp variable in the child process is 0

Therefore, in the above code, the code executed by the child process is the same as that of the parent process (the child process is executed from the position of the fork() function), but the pp variable is different
,
insert image description here
as
insert image description here
shown in the figure below. The pp variable is different, so it is distinguished by if and else to print out the corresponding

But in line 14 of the program code, the value of i is changed to 99 in the child process, but the value of i in the parent process has not been changed

Summary: The kernel creates a child process by copying the parent process. After the child process inherits a copy of the data segment, stack segment, and heap segment from the parent process, it can modify these contents without affecting the content of the parent process (in memory The program text segment marked as read-only is shared by the parent and child processes);

Guess you like

Origin blog.csdn.net/ZhuGeBin26/article/details/129331797