Linux Operating System Architecture ( 3 ) -【Linux Communication Architecture Series】

Series Article Directory

C++ skill series
Linux communication architecture series
C++ high-performance optimization programming series
Deep understanding of software architecture design series
Advanced C++ concurrent thread programming

Looking forward to your attention! ! !
insert image description here

现在的一切都是为将来的梦想编织翅膀,让梦想在现实中展翅高飞。
Now everything is for the future of dream weaving wings, let the dream fly in reality.

If you want to program well, you must have some understanding of the Linux operating system architecture. Generally speaking, the traditional Linux operating system architecture is shown in the figure:
insert image description here

Figure 1 Architecture diagram of traditional Linux operating system

In general, the structure of the Linux operating system is divided into user mode and kernel mode.

1. 操作系统/内核- Software

It is used to control the hardware resources of the computer and provide the environment in which the application programs run. It is called the kernel because it is relatively small and is at the heart of the entire architecture.

The programs we write 要么运行在用户态,要么运行在内核态generally run in the user mode. When the program needs to execute some special codes, the program may switch to the kernel mode. This switching is controlled by the operating system and does not require human intervention .

To understand user mode and kernel mode from another angle, user mode can be understood as the activity space of the outermost application program in Figure 1. However, the running of the application program we develop often requires access to some resources (such as accessing the disk for reading and writing files, applying for memory for calling malloc, and accessing external devices such as fax machines and printers in the program). To be able to access these resources, the kernel must provide an interface for application programs to access, that is, the system call in Figure 1 (kernel external interface).

2. 系统调用In fact, they are some library functions, which can be called when writing code

In general, the operating system will provide 200~300 library functions for us to call. These library functions are highly encapsulated inside the system. We don't need to care about the details, just call them.

3. shellIt is a command line interpreter, which is one of the main ways for users to interact with the operating system kernel in the Linux system

When using Xshell to connect to the Ubuntu Linux virtual machine, use the ps command to check, and you can see a bash process ( this bash process is automatically started, when you use Xshell to connect to the virtual machine, the system starts a bash. This bash is login/sshd is opened with the fork function. )

Bash is the abbreviation of borne again shell (reassembled shell), which is a kind of shell. Linux uses bash as the shell by default.

In layman's terms, bash is also an executable program. The main functions of this executable program are:把用户输入的命令翻译给操作系统,所以bash相当于一个命令解释器。

bash can also be created manually since it is an executable program.

Try to use whereis to find the location of bash, execute it directly, and then use exit to exit the current bash, as shown in the figure:
insert image description here

Figure 2 Manually execute bash and use exit to exit the bashs

If you enter exit for the second time, Xshell will exit from the currently used bash and will disconnect from the Ubuntu Linux virtual machine (because the first exit is the bash executed by itself, and the second exit exit is login/ The bash process created by the sshd process with the function fork).

Observe the position of the shell in Figure 1, it is just sandwiched between the system call and the application program, it plays the role of separating the system call and the application program, and it also feels like glue (sticking the system call and the application program together).

The kernel part of the operating system includes process management, file system, device driver, network and other resource management. Between the kernel and the user space, calls to various resource functions are realized through the system call (system call) interface.

4. Switch between 用户态and内核态

The program written either runs in the user state or in the kernel state, and one of the two states must exist. Processes running in user mode can perform any operations and access resources are greatly restricted (user mode permissions are extremely small), while processes running in kernel mode can perform any operations and use resources. No restrictions (kernel mode permissions are relatively large).

During the execution of a program, most of the time is in the user state, only when the services provided by the kernel are needed, it is switched to the kernel state, and after the task processing of the kernel state is completed, it is switched back to the user state. For example, the memory allocation function malloc in the C function library uses the sbrk system call to allocate memory internally. When malloc calls sbrk, it involves a switch from user mode to kernel mode. Similar functions include printf using the write system call to output strings, and so on. This state transition is done by the operating system without our intervention.

(1) User mode permissions are small, kernel mode permissions are large, and large permissions mean that you can do some dangerous things (such as processing memory and processing clocks). If all user programs can do these things, misuse may often occur. Cause the system to be in a dangerous state or even crash. Therefore, under normal circumstances, the program is in the user state. Only when some dangerous things need to be done, the system provides some calling interfaces to allow the program to switch to the kernel state.

(2) These calling interfaces are provided by the system and managed uniformly. The operating system has limited resources. If many programs access these resources at the same time, once an access conflict occurs or the resources are exhausted, the system may crash. Therefore, the system provides these interfaces to reduce limited resource access and conflicts in use.

For example, if you want to watch a movie now, if 100 people want to buy tickets, you can't let 100 people buy tickets at the same time, otherwise everyone will huddle together and no one will be able to get the tickets. Therefore, a ticket window (interface) is set up, and there is a person who specializes in selling tickets in the ticket window, and these 100 ticket buyers need to buy one by one.

什么时候会从用户态跳到内核态?

  • 系统调用. As malloc above.
  • 异常事件. If a signal is received and a signal processing function is written in the code, the system may need to jump to the kernel state to do some preparations for calling the signal processing function.
  • 外围设备的中断. After the peripheral device completes the user's requested operation, it will send an interrupt signal to the CPU. At this time, the CPU will suspend the execution of the next instruction to be executed, and then execute the processing program corresponding to the interrupt signal. If the previously executed instruction is in the user mode Next, a transition from user mode to kernel mode occurs.

Guess you like

Origin blog.csdn.net/weixin_30197685/article/details/131318596