在内核和用户空间

时间:2010-05-08 14:33:58

标签: kernel

现在我知道应该避免开发一个进入内核空间的应用程序 - 它很难调试,复杂等等.... 有了这个,将应用程序从用户空间移动到内核有什么好处?毕竟,如果没有正面,就永远不会做......有什么?

3 个答案:

答案 0 :(得分:9)

一些可能的优势:

  • 系统调用可能更快(即更低的延迟),因为CPU无需从应用程序模式切换进入内核模式。 (这不一定是正确的,因为CPU可能比简单的“用户空间”和“内核空间”更精细。例如,Intel x86 CPU具有包含4个不同权限级别的环模型。) 1 )

  • 您可以通过内存和I / O端口直接访问系统硬件

  • 您可以禁止任务切换,如果您需要做一些不被打断的事情

  • 您可以规避操作系统强制执行的安全机制(例如,读取/修改其他进程的内存)。 (如果恶意软件作为内核模式设备驱动程序安装,则可以利用此功能。)

(当然,正如您所知,存在许多缺点和安全风险。应用程序空间和内核空间之间的区别是有充分理由的。)


1) 参见例如文章Making system calls from kernel space from Linux mag

  

例如,高性能Web服务器可能希望驻留在内核中以提高吞吐量和降低延迟。但是,还有一个安全权衡[...]

答案 1 :(得分:3)

你的程序中有一个小错误的机会可以乱写整个内存并破坏整个系统及其所有进程和功能。如果幸运的话,系统会崩溃。

答案 2 :(得分:2)

Some of the reasons that come to my mind when searching for options i.e kernel mode vs user mode:

1) When dedicated processing is required and we want to use the utilities built in the OS. Eg: If we were to design an IO server. Here the latencies are to the tune of 1-5 ms. One cannot wait for context switches due to kernel - user mode tradeoffs. But if one has to rely on TCP IP framework given by kernel. It has to be implemented in Kernel mode closely tying the Network /TCP/IP framework and your intended transport framework.

2) When you want to completely own the scheduling framework. While this is intuitively available using various system calls and pthread frameworks. However, If your product/threads completely owns the processor, then there are cases of deadlocks or livelock you may want to recover from. In such scenarios you would need a framework that account for the time taken by each thread. This cannot be done from user lan and hence support from kernel scheduler/schedule subsystem is required.

3) When you want to over load access to memory, again in environments where resources are dedicated for a particular operations. It makes sense to overlay the physical memory/kernel memory for virtual threads.

4) When you want to virtualize disk access either to add redundancy or improve read/write performance.

There could be many more reasons but the central root cause:

1)Whenever you want to cut down layers for performance you move to kernel. Since kernel adds virtualisation framework for fairly sharing the resources (cpu, ram, network, disk).

2) Whenever you want to use the kernel infrastructure to use that are difficult to port to user lan (Tcp/ip or shceduler).