以内核模式运行的进程和以 root 身份运行的进程之间的区别?
我知道在用户模式下运行的进程和在内核模式下运行的进程之间的区别(基于访问限制、对硬件的访问等)。但出于好奇,以内核模式运行的进程与以 root 身份运行的进程有什么区别?
I am aware of the difference between a process running in user mode and one running in kernel mode (based on access restrictions, access to hardware etc.). But just out of curiosity, what is the difference between a process running in kernel mode and one running as root?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
内核模式和根是两个独立的概念,彼此之间并不真正相关。以 root 身份运行进程的概念是一个 unix/linux 术语,这意味着您以系统管理员的身份登录。
您运行的任何进程,无论是作为 root 用户还是普通用户,通常都在用户模式和内核模式下运行。系统不断地在用户模式(应用程序代码运行的地方)和内核模式(内核代码运行的地方)之间切换。
某些程序(如许多设备驱动程序)始终在内核模式下运行,这意味着它们可以完全访问硬件。以root权限运行的普通应用程序仍然存在于用户模式下,并且仅在进行内核系统调用时切换到内核模式,然后再切换回用户模式。
kernel mode and root are two separate ideas that aren't really related to each other. The concept of running a process as root is a unix/linux term that means you're logged in as the administrator of the system.
Any process you run, whether as root or a normal user, generally runs in both user mode and kernel mode. The system is continually switching between user mode (where the application code runs) and kernel mode (where the kernel code runs).
Some programs, like many device drivers, always run in kernel mode, meaning they have full access to the hardware. A normal application running with root privileges still exists in user mode and only switches to kernel mode when a kernel system call is made and then switches right back to user mode.
我知道这个问题很老了,但我只花了一个小时看这样的帖子,因为这些答案不清楚,我想我知道为什么。 “用户模式”中“用户”的含义与将其用于帐户时的含义不同,在操作系统中您可以将其视为“进程用户”,但这很少得到澄清。
简化且最终错误的可视化如下:
如果您正在询问“root 是否在内核模式下运行”,那么您可能已经非常熟悉任务管理器(如 htop),它向您显示所有进程/线程(从现在开始我只说进程),这就是您将看到的(让我们以 linux 为例)
所以我显然认为“所有特权都是root,如果root不是内核模式那么它就不会出现在列表中,对吗?”
错误的!
仔细一看,内核本身并不存在。
好的,那么你可以搜索“内核进程在哪里?”然后是“内核是一个进程吗?”和 bam
答案是:内核不是进程!
我看到的关于“根与内核”的每个答案都忽略/忘记提及这一点。
内核不是一个进程,它管理进程、启动、控制 CPU 并以 root 用户身份运行第一个进程 init/systemd。之后的一切都是一个进程,包括你可能运行的任何东西,并且以用户模式运行,包括 root,它是一个用户。用户的概念是一个内核结构。用户可以执行的操作的权限由内核本身处理,而不是由CPU处理,CPU根据内核的指令处理用户模式和内核模式之间的切换。在单核单线程CPU中,进程的概念也是内核的一个构造。
也许进程模式而不是用户模式会是一个更好的术语,毕竟用户的概念是由内核应用的,你可以没有用户的概念,但仍然有内核和用户模式......
最后,我可能仍然弄错了,在这种情况下,请有人纠正我,但我仍然认为这证明了术语和解释是多么令人困惑。
I know this question is old but I just spent one hour looking at posts like this because these answers are just not clear and I think I know why. The meaning of "user" in "user mode" is not the same as when using it for accounts, which in operating systems you can think of as "process users", but this is seldom clarified.
A simplified and ultimately wrong visualization is the following:
If you are at the point of asking "does root run in kernel mode" then you are likely already very familiar with task managers (like htop) that show you all the processes/threads (I'll just say process from now on) and this is what you will see (let's take linux as an example)
So I obviously thought "everything privileged is root, if root wasn't kernel mode then it wouldn't be on the list, right?".
Wrong!
Look closely, the kernel itself is not there.
Ok, then you can search "where is the kernel process?" and then for "is the kernel a process?" and bam
there is the answer: the kernel is not a process!
Every single answer I have seen about "root vs kernel" omits/forgets to mention this.
The kernel is not a process, it manages processes, it starts, takes control of the CPU and runs the first process init/systemd as root user. Everything after that is a process, including anything you may run, and runs in user mode, including root, which is a user. The concept of users is a kernel construct. The permissions of what users can do are handled by the kernel itself not by the CPU, which handles the switch between user and kernel mode according to what the kernel instructed. In a single-core single-thread CPU the concept of process is also a construct of the kernel.
Maybe process mode instead of user mode would have been a better terminology, after all, the concept of user is applied by the kernel, you could have no concept of users and still have kernel and user mode ...
Finally, I might have still gotten this wrong, in which case, please someone correct me, but I would still argue that this is a testament about how confusing the terminology and the explanations are out there.