为什么我们不能在内核开发中使用C标准库函数? [英] Why can't we use C standard library functions in kernel development?

查看:214
本文介绍了为什么我们不能在内核开发中使用C标准库函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习内核开发,对此有一个小疑问.将其与c库链接后,为什么不能在内核开发中使用c函数?为什么内核从不与c库链接,却对某些标准c函数(例如printk()而不是printf())具有自己的实现.如果内核是用c编写并在c编译器的帮助下进行编译的,那为什么我们不能使用c库中的标准函数呢?

I just got started with learning kernel development and had a small doubt. Why can't we use c functions in kernel development after linking it with the c library? Why is it that the kernel is never linked with a c library but has its own implementation of some standard c functions like printk() instead of printf(). IF the kernel is written in c and compiled with the help of a c compiler then why can't we use the standard function from the c library?

推荐答案

因为您熟悉的GNU C库是为用户模式而不是内核模式实现的.内核无法访问用户空间API(可能会调用Linux内核的 syscall ).

Because the GNU C Library which you are familiar with is implemented for user mode, not kernel mode. The kernel cannot access a userspace API (which might invoke a syscall to the Linux kernel).

来自内核新手常见问题解答

我可以在内核中使用库函数吗?

Can I use library functions in the kernel ?

通常可供用户空间程序员使用的系统库(例如glibc,libreadline,libproplist等)对内核程序员不可用.加载进程时,加载器将自动将所有依赖库加载到进程的地址空间中.内核程序员无法使用此机制:忘记ISO C库,唯一可用的是内核中已经实现(并导出)的内容以及可以自己实现的内容.

System libraries (such as glibc, libreadline, libproplist, whatever) that are typically available to userspace programmers are unavailable to kernel programmers. When a process is being loaded the loader will automatically load any dependent libraries into the address space of the process. None of this mechanism is available to kernel programmers: forget about ISO C libraries, the only things available is what is already implemented (and exported) in the kernel and what you can implement yourself.

请注意,可以转换"库以在内核中工作;但是,它们不合适,过程繁琐且容易出错,并且堆栈处理可能存在重大问题(内核限于少量堆栈空间,而用户空间程序没有此限制)导致随机内存损坏.

Note that it is possible to "convert" libraries to work in the kernel; however, they won't fit well, the process is tedious and error-prone, and there might be significant problems with stack handling (the kernel is limited to a small amount of stack space, while userspace programs don't have this limitation) causing random memory corruption.

许多常用功能已经在内核中实现,有时是轻量级"版本,其功能不如其用户级功能强大.从头开始编写自己的版本之前,请确保grep您可能使用的所有功能的标头.一些最常用的文件位于include/linux/string.h.

Many of the commonly requested functions have already been implemented in the kernel, sometimes in "lightweight" versions that aren't as featureful as their userland counterparts. Be sure to grep the headers for any functions you might be able to use before writing your own version from scratch. Some of the most commonly used ones are in include/linux/string.h.

每当您觉得需要一个库函数时,就应该考虑您的设计,并问自己是否可以将部分或全部代码移入用户空间.

Whenever you feel you need a library function, you should consider your design, and ask yourself if you could move some or all the code into user-space instead.

如果您需要使用标准库中的函数,则由于简单原因-没有标准C库,您必须重新实现该功能.

If you need to use functions from standard library, you have to re-implement that functionality because of a simple reason - there is no standard C library.

C库基本上是在Linux内核(或其他操作系统的内核)的顶部实现的.

C library is basically implemented on the top of the Linux kernel (or other operating system's kernel).

例如,C库的mkdir(3)函数基本上不过是Linux内核系统调用mkdir(2)的包装器.

For instance, C library's mkdir(3) function is basically nothing more than a wrapper for Linux kernel's system call mkdir(2).

http://linux.die.net/man/3/mkdir http://linux.die.net/man/2/mkdir

这篇关于为什么我们不能在内核开发中使用C标准库函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆