Linux process understanding [Von Neumann architecture, operating system, process concepts and basic operations]
- 1. Von Neumann architecture
- 2. Operating system
- 3. Process understanding
- 4. Process-Based Related Operations
- 5. Process PID
-
- 1.getpid() and getppid()
- 2. The fork() function creates a process
- 6. Another way to view the process
- 7. Summary
To understand the process, we must first understand two major knowledge points:
1. Von Neumann architecture (from a hardware perspective)
2. Operating system (from a software perspective)
1. Von Neumann architecture
1. Description of von Neumann architecture
2. The value of von Neumann architecture
To understand the value of the von Neumann architecture, we must first clarify two points:
1. Limitations of computers before von Neumann
But before the emergence of computers based on von Neumann architecture design
computers had two major shortcomings:
1. Low efficiency< /span> And everyone may have heard of wooden Bucket Principle At that time, computers only had CPUs, input devices, and output devices It explains a truth in detail: This is a computer storage pyramid found online Why is this happening?
2. Expensive
2. Why does memory exist in computer architecture?
Therefore, the introduction of memory makes the overall efficiency of our computers pretty good and relatively cheap, which is also conducive to the spread of computers
More and more people are using computers, so The Internet just appeared
At this point, we have introduced the prerequisite knowledge related to understanding the pre-process von Neumann architecture
Now let’s talk about the second prerequisite knowledge: operating system
2. Operating system
1.What is an operating system?
The driver here refers to:
The operating system can access and control the underlying hardware by calling the interface of the driver corresponding to the underlying hardware
Therefore, the operating system can ensure the efficiency and success rate of hardware management through drivers
2. How to manage the operating system
At this point, we understand how the operating system manages software and hardware resources.
Then the next question is:
How does the operating system manage its hardware and software resources? What is the relationship between users?
Next we will explain the relationship between the operating system and users:
3. Why do we need an operating system?
4. System call interface and user operation interface
At this point, everyone will have a deeper understanding of this picture
We have finished introducing the pre-knowledge related to the operating system before understanding the process
Now we officially enter the process of learning
3. Process understanding
1. The concept of process
We all should know the task manager in our windows system
When a program is loaded into the memory and becomes a process, the operating system will allocate a memory to each process for storage. The data of the structure object of the process facilitates the operating system to manage the process!
The following is the structure that describes the process information
At this point, the operating system has successfully described the attributes of the process with PCB objects,
Therefore, the operating system The management of processes becomes the management of PCB objects
therefore:
Process = kernel data structure (not just PCB objects) + executable program
Therefore, all control and operations on the process are only related to the PCB object of the process, and have nothing to do with the executable program of the process
As long as you want, you can put the PCB object into Manage in any data structure!
2. The unique linking method of task_struct structure
Please note:
A task_struct can be connected to a variety of data structures!!!
This is very important, because we will also introduce the run queue in the future,
This is without changing the linked list structure that task_struct has formed at the same time
The reason why you can also put it in the queue
3.Task_struct content classification (roughly talk about it)
So what is in task_struct?
Let’s talk briefly about it first
About the pc pointer or eip register here Let’s first take a look at the related operations of the process You have said so much, you have to let us see it. Process bar We will introduce these contents in detail later
Let’s introduce its function
4. Process-Based Related Operations
1. Check the process
First generate a process executable program
ps ajx | head -1 && ps -ajx | grep 可执行程序名字
Here we first execute an executable program: process
and then view the process
ps ajx | head -1 && ps -ajx | grep 可执行程序名字 | grep -v grep
2. Kill the process
Just now we said ctrl+c to exit a process
Sometimes ctrl+c is not feasible (we will see this situation below)
Now we need to use it
kill -9 进程的PID
to kill the specified process
5. Process PID
1.getpid() and getppid()
Note: The values of pid_t are all positive integers or 0
Let’s check the process ID and parent process ID of the process in the code
while :; do ps ajx | head -1 && ps ajx | grep 可执行程序名字 | grep -v grep; sleep 1;done
This shell command can check the process containing the specified executable program name in an infinite loop
Each while loop will sleep for 1 second
Press ctrl+c Exit
View the information of the corresponding process based on the process ID:
ps ajx | head -1 && ps ajx | grep 进程ID | grep -v grep
Below we execute this process multiple times and find:
2. The fork() function creates a process
1.fork() function
2.Demonstration
Let's demonstrate it below
3. Some explanations
1. Process independence
(Any) processes are independent and cannot affect each other.
Even if the parent process and the child process are as close as father and son, when our child process and the parent process are both running,
if the child process dies, the code will still exist, which will affect the parent process. The process has no impact
If the parent process hangs up, the code will still exist and has no impact on the child process
Kill the parent process, the child process is still running
However, because the parent process is killed at this time, the child process cannot exit through ctrl+c
You can only use kill -9 to kill the child process
Kill the child process, the parent process is still running,
The window on the upper right shows that the child process is defunct (that is, closed) status
indicates that the child process has been killed, and the parent process can exit normally after pressing ctrl+c
Regarding the topic of parent-child processes, we We will explain in detail when introducing zombie processes and orphan processes in the future
Here we only need to know: processes are independent and cannot affect each other
2. Several questions about the return value of the fork function
1. Why are the return values to the parent and child processes different?
Distinguish the parent and child processes through the return value of the fork function, and use if else judgment to let the parent and child processes do different things.
2.Why does the fork function return twice?
Inside the fork function, before the return statement is executed, the child process has been created
And because the code of the parent and child processes is shared
And the return statement itself also belongs to code
So the return statement will be executed by both the parent process and the child process
3. Why can id, as the same variable, represent both the parent process id and the child process id?
This involves the knowledge of copying while writing. Let’s mention it here first, and we will mention it later:
Therefore, we can conclude:
In Linux, you can use the same variable name to represent different memories
4. Create multiple child processes (observe the entire process of process creation and exit)
Next we create multiple child processes
Observe the entire process of process creation and exit
Finally, all child processes exited, leaving only The next parent process finally sleeps for 15 seconds before exiting
In this way, we can see the entire process from creation to exit
The child process here is in The defunct (that is, closed) status indicates that the process has exited
6. Another way to view the process
1./proc
Process information can be viewed through the /proc system folder
ls /proc
View information about a specific process:
ls /proc 进程id
Let's use the mycmd executable program to test it
I used to use the shell shell and permissions in Linux (including directory file permissions) , the ins and outs of sticky bits)Linked files were mentioned in this blog
Next we will also see linked files
2.exe link file
However, this process can only be executed once. After the process ends, it cannot be executed again.
When I delete the executable program mycmd, the process remains the same. Run again
but the executable program executed by this exe has been deleted
3. How to change cwd?
We can use the chdir function
Let’s try it out
It is currently under this path
I think Let it come
/home/wzs/wzsdir/systemlearndir
Under this path
Let’s create a file
to deepen our understanding of the cwd and fopen functions
Found that test.txt was indeed created in
/home/wzs/wzsdir/systemlearndir
under this path
7. Summary
In this blog we mainly introduce
1. Von Neumann architecture
2. Operating system
3. The concept of process, task_struct structure
4. Basic related operations of the process: view the process, kill the process, change the working directory of the process
5. Process PID, fork function creates process
The above is the entire content of Linux process understanding (von Neumann architecture, operating system, process concepts and basic operations). I hope it can be helpful to everyone!