目錄
核心空間與使用者空間" >核心空間與使用者空間
為什麼要區分核心空間與使用者空間" >為什麼要區分核心空間與使用者空間
核心態與使用者狀態" >核心態與使用者狀態
How to enter kernel space from user space" >How to enter kernel space from user space
the whole frame" >the whole frame
Summarize" >Summarize
首頁 系統教程 Linux 什麼是Linux核心空間與使用者空間?

什麼是Linux核心空間與使用者空間?

Feb 05, 2024 pm 12:57 PM
linux linux教程 linux系統 linux作業系統 linux指令 shell腳本 嵌入式linux 良許 linux入門 linux學習

核心空間與使用者空間

#對於32位元作業系統而言,它的尋址空間(也稱為虛擬位址空間或線性位址空間)大小為4G(即2的32次方)。這意味著一個行程可以擁有最大4G的位址空間。

作業系統的核心是核心(kernel),它是與普通應用程式分離的,有權限存取受保護的記憶體空間和底層硬體設備。為了確保核心的安全,現代作業系統通常限制使用者進程直接操作核心。

通常,這透過將虛擬位址空間劃分為兩個部分來實現,即核心空間和使用者空間。就Linux作業系統而言,最高的1G位元組(從虛擬位址0xC0000000到0xFFFFFFFF)被核心使用,稱為核心空間。而較低的3G位元組(從虛擬位址0x00000000到0xBFFFFFFF)則由各個程序使用,稱為用戶空間。

換句話說,每個行程的4G位址空間中,最高的1G是相同的,也就是核心空間。只有剩餘的3G才是進程本身的可用空間。

可以這樣理解:「最高1G的核心空間是在所有進程之間共享的!」下圖展示了每個進程4G位址空間的分配情況(圖片來自互聯網):

什麼是Linux核心空間與使用者空間?

為什麼要區分核心空間與使用者空間

#在 CPU 的所有指令中,有些指令是非常危險的,如果錯用,會導致系統崩潰,例如清除記憶體、設定時鐘等。如果允許所有的程式都可以使用這些指令,那麼系統崩潰的機率將大大增加。

所以,CPU 將指令分為特權指令和非特權指令,對於那些危險的指令,只允許作業系統及其相關模組使用,普通應用程式只能使用那些不會造成災難的指令。

例如 Intel 的 CPU 將特權等級分成 4 個等級:Ring0~Ring3。其實 Linux 系統只使用了 Ring0 和 Ring3 兩個運行等級(Windows 系統也是一樣的)。

當進程運行在 Ring3 層級時稱為運行在用戶態,而運行在 Ring0 層級時稱為運行在核心態。

核心態與使用者狀態

#好了我們現在需要再解釋一下什麼是核心態、用戶態:「當進程運行在內核空間時就處於內核態,而進程運行在用戶空間時則處於用戶態。」

在核心態下,進程運行在核心位址空間中,此時 CPU 可以執行任何指令。運行的程式碼也不受任何的限制,可以自由地存取任何有效位址,也可以直接進行連接埠的存取。

在使用者狀態下,進程運行在使用者位址空間中,被執行的程式碼要受到CPU 的諸多檢查,它們只能存取映射其位址空間的頁表項中規定的在使用者狀態下可存取頁面的虛擬位址,且只能對任務狀態段(TSS)中I/O 許可位圖(I/O Permission Bitmap)中規定的可存取連接埠進行直接存取。

對於先前的 DOS 作業系統來說,是沒有核心空間、使用者空間以及核心態、使用者態這些概念的。可以認為所有的程式碼都是運行在內核態的,因而用戶編寫的應用程式程式碼可以很容易的讓作業系統崩潰掉。

對於 Linux 來說,透過區分核心空間和使用者空間的設計,隔離了作業系統程式碼(作業系統的程式碼要比應用程式的程式碼健壯很多)與應用程式碼。

即使是單一應用程式出現錯誤也不會影響到作業系統的穩定性,這樣其它的程式還可以正常的運作(Linux 可是個多任務系統啊!)。

「所以,區分核心空間和使用者空間本質上是要提高作業系統的穩定性及可用性。」

#

How to enter kernel space from user space

In fact, all system resource management is completed in the kernel space. For example, reading and writing disk files, allocating and recycling memory, reading and writing data from network interfaces, etc.

Our application cannot directly perform such operations. But we can accomplish such tasks through the interface provided by the kernel.

For example, if an application wants to read a file on the disk, it can initiate a "system call" to the kernel and tell the kernel: "I want to read a certain file on the disk."

In fact, a special instruction is used to allow the process to enter the kernel state (to the kernel space) from the user state. In the kernel space, the CPU can execute any instructions, including reading data from the disk. The specific process is to first read the data into the kernel space, then copy the data to the user space and switch from the kernel mode to the user mode.

At this point, the application has returned from the system call and obtained the desired data, and can happily continue execution. To put it simply, the application outsources high-tech things (reading files from disk) to the system kernel, and the system kernel does these things professionally and efficiently.

For a process, the process of entering kernel space from user space and finally returning to user space is very complicated. For example, the concept "stack" that we often come into contact with actually has a stack in the kernel mode and user mode.

When running in user space, the process uses the stack in user space, and when running in kernel space, the process uses the stack in kernel space. Therefore, each process in Linux has two stacks, one for user mode and one for kernel mode.

The following figure briefly describes the conversion between user mode and kernel mode:

什麼是Linux核心空間與使用者空間?

Since the user mode process must switch to the kernel mode in order to use the system resources, let's take a look at how many ways the process can enter from the user mode to the kernel mode.

In summary, there are three ways: system call, software interrupt and hardware interrupt. Each of these three methods involves a lot of operating system knowledge, so I will not expand on it here.

the whole frame

Next, let’s take a look at the structure of the entire Linux system from the perspective of kernel space and user space. It can be roughly divided into three parts, from bottom to top: hardware -> kernel space -> user space. As shown in the picture below (this picture comes from the Internet):

什麼是Linux核心空間與使用者空間?

On top of the hardware, the code in the kernel space controls the use of hardware resources. The code in the user space can only use the hardware resources in the system through the system call interface (System Call Interface) exposed by the kernel. . In fact, not only Linux, but also the design of Windows operating systems is similar.

In fact we can summarize the activity of each processor at any given point in time as one of the following three:

  • Runs in user space and executes user processes.
  • Runs in the kernel space, in the process context, and executes on behalf of a specific process.
  • Runs in kernel space, is in interrupt context, has nothing to do with any process, and handles a specific interrupt.

The above three points include almost all situations. For example, when the CPU is idle, the kernel runs an empty process, which is in the process context but runs in the kernel space.

Note: The interrupt service routines of Linux systems are not executed in the context of the process. They are executed in a specialized interrupt context that is independent of all processes.

The reason why there is a special execution environment is to ensure that the interrupt service program can respond to and handle the interrupt request as soon as possible, and then exit quickly.

Summarize

Most modern operating systems protect the security and stability of the operating system itself through the design of kernel space and user space. Therefore, when we read information about operating systems, we often encounter concepts such as kernel space, user space, kernel mode, and user mode. I hope this article can help you understand these basic concepts.

以上是什麼是Linux核心空間與使用者空間?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1668
14
CakePHP 教程
1426
52
Laravel 教程
1329
25
PHP教程
1273
29
C# 教程
1256
24
Linux體系結構:揭示5個基本組件 Linux體系結構:揭示5個基本組件 Apr 20, 2025 am 12:04 AM

Linux系統的五個基本組件是:1.內核,2.系統庫,3.系統實用程序,4.圖形用戶界面,5.應用程序。內核管理硬件資源,系統庫提供預編譯函數,系統實用程序用於系統管理,GUI提供可視化交互,應用程序利用這些組件實現功能。

git怎麼查看倉庫地址 git怎麼查看倉庫地址 Apr 17, 2025 pm 01:54 PM

要查看 Git 倉庫地址,請執行以下步驟:1. 打開命令行並導航到倉庫目錄;2. 運行 "git remote -v" 命令;3. 查看輸出中的倉庫名稱及其相應的地址。

notepad怎麼運行java代碼 notepad怎麼運行java代碼 Apr 16, 2025 pm 07:39 PM

雖然 Notepad 無法直接運行 Java 代碼,但可以通過借助其他工具實現:使用命令行編譯器 (javac) 編譯代碼,生成字節碼文件 (filename.class)。使用 Java 解釋器 (java) 解釋字節碼,執行代碼並輸出結果。

sublime寫好代碼後如何運行 sublime寫好代碼後如何運行 Apr 16, 2025 am 08:51 AM

在 Sublime 中運行代碼的方法有六種:通過熱鍵、菜單、構建系統、命令行、設置默認構建系統和自定義構建命令,並可通過右鍵單擊項目/文件運行單個文件/項目,構建系統可用性取決於 Sublime Text 的安裝情況。

Linux的主要目的是什麼? Linux的主要目的是什麼? Apr 16, 2025 am 12:19 AM

Linux的主要用途包括:1.服務器操作系統,2.嵌入式系統,3.桌面操作系統,4.開發和測試環境。 Linux在這些領域表現出色,提供了穩定性、安全性和高效的開發工具。

laravel安裝代碼 laravel安裝代碼 Apr 18, 2025 pm 12:30 PM

要安裝 Laravel,需依序進行以下步驟:安裝 Composer(適用於 macOS/Linux 和 Windows)安裝 Laravel 安裝器創建新項目啟動服務訪問應用程序(網址:http://127.0.0.1:8000)設置數據庫連接(如果需要)

git軟件安裝 git軟件安裝 Apr 17, 2025 am 11:57 AM

安裝 Git 軟件包括以下步驟:下載安裝包運行安裝包驗證安裝配置 Git安裝 Git Bash(僅限 Windows)

sublime快捷鍵怎麼使用 sublime快捷鍵怎麼使用 Apr 16, 2025 am 08:57 AM

Sublime Text 提供了提高开发效率的快捷键,包括常用的(保存、复制、剪切等)、编辑(缩进、格式化等)、导航(项目面板、文件浏览等)以及查找和替换快捷键。熟练使用这些快捷键可显著提升 Sublime 的使用效率。

See all articles