Introduction and identifier of Linux-process

what is a process

In our daily life, we use Windows a little more, and when we use Windows, we can open our task manager and see various processes running.

 So, is the process equal to our program?

Many times, when we read books, it will be written that a process refers to a program that has been running in the computer memory.

This statement is actually not comprehensive.

If the operating system wants to manage the process, it must first collect the running data of the program and then manage it. Therefore, the operating system needs to have a module that can collect and query all running data well. This module is It is called PCB (process control block) process control module.

With PCB, all process management tasks are not directly related to the program corresponding to the process. The management of the process by the operating system is strongly related to the PCB, and only needs to operate on the PCB.

And the process is the process = PCB + your program data and code.

What are PCBs? What's in it?

Process information is placed in a data structure called a process control block, which can be understood as a collection of process attributes.

In the Linux system, its PCB is called struct task_struct{...}, which can be understood as a structure in essence, and the data and status of the process are stored inside the structure, whether you want to search for a process or To delete, you must first check its task_struct before you can operate on this process.

So specifically, what information does this structure store?

There are many things stored in it, including various information and the current state of the process. We only introduce some important ones.

Identifier

Identifier: The unique identifier describing this process is used to distinguish other processes.

The identifier of the process is called PID, and PID is the unique identifier of the process, which is the unique code name of the process.

In order to facilitate the observation of the PID of the process, we first write a random program and let it run.

The above getpid() and getppid() functions are to obtain its PID and PPID respectively, and the header file unistd.h needs to be included

On the left, our infinite loop program keeps running.

On the right side, we use the ps axj | head -1 && ps axj | grep myproc command to view our process information, and the PID is the unique identifier of our process.

The PID of each process is also stored in the PCB.

And PPID is the PID of its parent process.

Here we have introduced a new thing, what is the parent process? Is there a corresponding child process? Yes.

 We can use the fork() function to create a child process. If fork creates a child process successfully, it will return the PID of the child process to the parent process, and return a 0 to the child process. If the creation fails, it will return a number <0. .

Use the fork function to create another flow, implement a code, two processes, one is called the parent process, and the other is called the child process, and the child process and the parent process can have different running results.

run code

 use the ps command

At this point we can see that there are two processes with the same name, and we can take a closer look at their PID and PPID

The above process with PID 23700 is the parent process of the process with PID 23701.

But who is the parent process 15604 of the process with PID 23700?

We search for process 15604 with ps and grep commands

 Here we can see that it is our bash (command line interpreter).

If you continue to trace the source upward, you will be able to trace the source to our operating system. You can come down and try it yourself.

Summarize

1. Know that the process is composed of the data and code of the PCB and the process. (emphasis)

2. Know to use the fork function to create another stream, implement a code, two processes, one is called the parent process, the other is called the child process, and the child process and the parent process can have different running results.

3. Learned how to use the ps command to view the relevant information of the process.

Guess you like

Origin blog.csdn.net/fengjunziya/article/details/130478463