Linux-Von Neumann architecture, operating system, process, PCB, process status, zombie process, orphan process

1. Von Neumann architecture

Most of our common computers (eg: notebooks) and uncommon computers (eg: servers) in life follow the characteristics of the von Neumann system
Insert picture description here

• Data is stored in "memory" • All data in the
computer is binary

Understanding of the relationship between the various parts of von Neumann's system

Insert picture description here
The relationship between CPU, register, and memory is like the relationship between ancient emperors, eunuchs, and ministers. The
   function of CPU is equivalent to the emperor, the function of registers is equivalent to the eunuch, the function of memory is equivalent to the minister, and the minister will feedback (memory). The memorial (data) is handed over to the eunuch (register), and then the memorial (data) is passed to the emperor (CPU) for review (processing) by the eunuch (register), and then the eunuch (CPU) has finished reviewing the memorial (data processing) The memorial (data) is handed over (transferred) to the eunuch (register) and feedback (returned) to the minister (memory)

2. Operating System

2.1 Basic concepts

Any computer system contains a basic set of programs, called the operating system (OS), in general terms, the operating system includes:

• Kernel (process management, memory management, file management, driver management)
• Other programs (eg: function library, shell program)
 
      operating system = operating system kernel + a bunch of applications

2.2 The purpose of designing the OS

The operating system is software, and the operating system is designed to manage the software and hardware resources of the computer.

• Interact with hardware, manage all software and hardware resources
• Provide a good execution environment for user programs (applications)

So how does the OS manage it?

     Management = description + organization
 
• description: custom data type eg: struct structure
• organization: use linked list or other efficient data structure

The difference between system call interface (function) and library function

• System call interface (function): functions provided by the operating system for programmers. Programmers call these functions to better use the resources managed by the operating system.
• Library functions: programmers appropriately encapsulate some system call functions to form Library
eg: strcpy strlen malloc free

3. Process

3.1 Basic concepts

What is a program?

A program is a file compiled from source code, and the program is static

What is a process?

The process is an example of the program running, and it is dynamic (a process is an instance generated when the program is running ); from the perspective of the kernel, the process is the entity through which the operating system allocates resources

3.2 View process information

ps aux View the process information of the current operating system and
Insert picture description here
let the following code run
Insert picture description here
Insert picture description here
through the pipe filter to find the process information generated by the currently running program
ps aux | grep ./main
Insert picture description here
At this time, you will see two process information of ./main

Why will two ./main process information be generated
• 第一行是正在运行程序产生的进程信息
• 第二行是因为在执行ps aux | grep ./main命令时,
在命令结束之前,将grep产生的进程信息也保存下来最终返回

4. Describe the process-PCB

4.1 Basic concepts

The process information is placed in a data structure called the process control block (PCB) , which can be understood as a collection of process attributes; the PCB under the Linux operating system is task_struct, which is equivalent to the structure

• The structure describing the process in Linux is called task_struct
• task_struct is a data structure of the Linux kernel, which is loaded into RAM (memory) and contains process information

4.2 Concurrency and Parallelism

Parallel : multiple processes have different CPUs at the same time to perform operations, called parallel
eg: eight lanes, each lane can have one vehicle at the same time
Concurrent : multiple processes at the same time can only be owned by one process Perform calculations, called concurrent
eg: Only one car can pass through a single lane at the same time

4.3 struct content classification

• Process identifier (PID): uniquely identifies a process in the operating system, used to distinguish other processes

• Process status: used to indicate the task status of the process, exit code, push out signal, etc.

Program counter : save the address of the next instruction that the process is about to run

Context data : save the contents of the register

• Memory pointer: points to the program address space (points to the heap area, data segment, code segment, etc.)

• I/O status information: save the information of the file opened by the current process
==> /proc: save all the process information maintained by the current operating system, each process is a folder
Insert picture description here
View the process number of the current process and Insert picture description here
cd to the file Folder Insert picture description here
cd to the fd folder under this folder to view its corresponding I/O stream
Insert picture description here

• Accounting information: the length of time the cpu is used and the amount of memory occupied

• Priority: relative to the priority of other programs

4.3 Process status

From the big point of view, the process has three states: ready state, running state, blocking state

From the perspective of subdivision, the process has 7 states:

• R state: running state -> contains two states of ready and running (waiting and running two states)
 
• S state: interruptible sleep state -> the process is waiting for the event to complete
 
• D state: uninterruptible sleep state
 
• T state: Paused state
-> ctrl+z makes a process paused, the process still exists after suspending
-> the process does not exist after ctrl+c is suspended
 
• t state: tracking state -> will appear during gdb debugging
 
• X state: dead state
 
• Z state: zombie state (later talked about zombie process together)

fg可以使Ctrl+z暂停的程序回复运行

4.4 Process creation

4.4.1 getpid() function

gitpid() function: used to get the pid number of the current process, who calls to get who

4.4.2 gitppid() function

getppid() function: Get the pid number of the parent process of the current process, who calls to get whose parent process

The test is as follows

The code is as follows Insert picture description here
View the process information and output comparison of the current executable programInsert picture description here

4.4.3 fork() function

Fork() function: allows programmers to create a child process
 
pid_t fork(void) in the code
 
   • no parameters
 
   • pid_t: essentially shaping
 
   • Return value: if the child process fails to be created, -1 is returned; if the child process is created successfully, For the parent process, return the number >0 (in fact, return the PID number of the child process); for the child process, return the number == 0
 
   • Principle: Fork creates the PCB of the child process to copy the PCB of the parent process
Insert picture description here
 
   • The parent and child processes are mutual independent

ps -ef | grep ./main can view the parent process of the child process

Insert picture description here

5. Zombie Process & Zombie State

The production of zombie processes:

The child process exits before the parent process. When the child process exits, it will tell the parent process, but the parent process has not had time to reclaim the resources of the child process. As a result, the PCB of the child process is not released, and a zombie process is generated. Zombie state

The test is as follows:

Create a test.c with the
 
following code. The parent process never exits (with an infinite loop set inside), and the child process exits after executing the code. Insert picture description here
Use the command ps aux | grep test to view the process information of the test. As shown in the figure below, the process with PID 2337 is A zombie processInsert picture description here

The harm of the zombie process:

The PCB of the child process cannot be released, and it is thought of a memory leak

Solution:
kill命令
• kill + PID :杀死进程
• kill -9 + PID:强制杀死进程
但以上命令并不能杀死僵尸进程

• Restarting the operating system is costly and not recommended.
• Killing the parent process of the zombie process is relatively costly and not recommended.
Process waiting-process control (described later), the recommended way

6. Orphan Process

The production of orphan processes:

Parent before the child process exits , the child process will be the adoption process No. 1 , when the child process exits, the child process will be recovered by the process exit No. 1 resource; refer to this process as orphaned
 
Note: Process state is not Orphan status

What is process 1

The first process generated when the operating system is started is process No. 1

The test is as follows:

The child process of the following code never exits (infinite loop is set inside), and the parent process exits after executing the code Insert picture description here
. As shown in the figure below, the process with PID 7082 is an orphan process
Insert picture description here

6.1 Question 1:

Does the created child process run from the code after the fork or from the code before the fork?

Insert picture description here
When the parent process executes fork, the program counter saves the next statement (judgment statement), fork child process, the child process copies the parent process PCB is also saved in the program counter to the next judgment statement, so the child process starts from the judgment statement , The child process is run from the code after the fork

6.2 Question 2:

Does the parent process run first or the child process chooses to run?

This is uncertain and depends on the operating system's kernel scheduling (who gets the CPU resources first).
If there are two CPUs, the parent and child processes may run at the same time.
If there is only one CPU, it depends on who gets the CPU resources first and runs first.

Guess you like

Origin blog.csdn.net/weixin_50886514/article/details/115114170