“C 系统调用”和“C 系统调用”有什么区别? 和“C 库例程”?
联机帮助页中有多个部分。 其中两个是:
2 Unix and C system calls 3 C Library routines for C programs
例如有 getmntinfo (3)
和 < code>getfsstat(2),两者看起来都做同样的事情。 什么时候应该使用哪个以及有什么区别?
There are multiple sections in the manpages. Two of them are:
2 Unix and C system calls 3 C Library routines for C programs
For example there is getmntinfo(3)
and getfsstat(2)
, both look like they do the same thing. When should one use which and what is the difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
系统调用是操作系统函数,就像 UNIX 上的
malloc()
< /a> 函数构建在sbrk()
系统调用(用于调整进程内存空间的大小)。库只是应用程序代码,不属于操作系统的一部分,并且通常可在多个操作系统上使用。 它们与您自己的程序中的函数调用基本相同。
这条线可能有点模糊,但只是将系统调用视为内核级功能。
System calls are operating system functions, like on UNIX, the
malloc()
function is built on top of thesbrk()
system call (for resizing process memory space).Libraries are just application code that's not part of the operating system and will often be available on more than one OS. They're basically the same as function calls within your own program.
The line can be a little blurry but just view system calls as kernel-level functionality.
常用函数库构建在系统调用接口之上,但应用程序可以自由使用两者。
系统调用就像有权使用内核资源的身份验证密钥。
上图来自高级 Linux 编程,有助于理解用户应用程序如何与内核交互。
Libraries of common functions are built on top of the system call interface, but applications are free to use both.
System calls are like authentication keys which have the access to use kernel resources.
Above image is from Advanced Linux programming and helps to understand how the user apps interact with kernel.
系统调用是用户级代码和内核之间的接口。 C 库例程是像任何其他库调用一样的库调用,它们只是碰巧被普遍提供(几乎普遍存在)。 许多标准库例程都是系统调用的包装器(瘦的或其他的),这确实会使界限有点模糊。
至于使用哪一种,一般来说,使用最适合您需要的一种。
System calls are the interface between user-level code and the kernel. C Library routines are library calls like any other, they just happen to be really commonly provided (pretty much universally). A lot of standard library routines are wrappers (thin or otherwise) around system calls, which does tend to blur the line a bit.
As to which one to use, as a general rule, use the one that best suits your needs.
本手册第 2 节中描述的调用都是相对较薄的包装器,围绕着对捕获到内核的系统服务的实际调用。 本手册第 3 节中描述的 C 标准库例程是客户端库函数,它们实际上可能使用也可能不使用系统调用。
此帖子有一个系统调用和捕获内核的描述(在稍微不同的上下文中),并通过一些参考解释了系统调用背后的底层机制。
The calls described in section 2 of the manual are all relatively thin wrappers around actual calls to system services that trap to the kernel. The C standard library routines described in section 3 of the manual are client-side library functions that may or may not actually use system calls.
This posting has a description of system calls and trapping to the kernel (in a slightly different context) and explains the underlying mechanism behind system calls with some references.
作为一般规则,您应该始终使用 C 库版本。 他们通常有包装器来处理深奥的事情,例如根据信号重新启动(如果您有要求)。 如果您已经与该库链接,则尤其如此。 所有规则都有被打破的理由。 使用直接调用的原因,
libc
无关; 也许有安装程序。 此类代码可以在 Android 上运行 (bionic),uClibc,以及更传统的 glibc/eglibc 系统,无论使用什么库。 此外,使用包装器动态加载以创建运行时 glibc/bionic 层,允许双 Android/Linux 二进制文件。libc
偶尔可以做到这一点。initramfs
或init
代码; 创建更小的映像或更快地启动。libc
例程。libc
中的已知错误。libc
获得。抱歉,大多数示例都是 Linux 特定的,但其原理应该适用于其他 Unix 变体。 当新功能引入内核时,最后一项非常常见。 例如,当
kqueue
或epoll
首次引入时,没有libc
支持它们。 如果系统有较旧的库,但有较新的内核并且您希望使用此功能,也可能会发生这种情况。如果您的进程尚未使用
libc
,那么系统中很可能会使用该库。 通过编写自己的变体,您可以通过提供通往同一最终目标的两条路径来取消缓存。 另外,Unix将在进程之间共享代码页。 一般来说,没有理由不使用
libc
版本。其他答案已经在 libc 和系统调用之间的差异方面做出了出色的工作。
As a general rule, you should always use the C library version. They often have wrappers that handle esoteric things like restarts on a signal (if you have requested that). This is especially true if you have already linked with the library. All rules have reasons to be broken. Reasons to use the direct calls,
libc
agnostic; Maybe with an installer. Such code could run on Android (bionic), uClibc, and more traditional glibc/eglibc systems, regardless of the library used. Also, dynamic loading with wrappers to make a run-time glibc/bionic layer allowing a dual Android/Linux binary.libc
can occasionally do.initramfs
orinit
code without a library; to create a smaller image or boot faster.initramfs
.libc
routines.libc
.libc
.Sorry, most of the examples are Linux specific, but the rationals should apply to other Unix variants. The last item is quite common when new features are introduced into a kernel. For example when
kqueue
orepoll
where first introduced, there was nolibc
to support them. This may also happen if the system has an older library, but a newer kernel and you wish to use this functionality.If your process hasn't used the
libc
, then most likely something in the system will have. By coding your own variants, you can negate the cache by providing two paths to the same end goal. Also, Unixwill share the code pages between processes. Generally there is no reason not to use the
libc
version.Other answers have already done a stellar job on the difference between
libc
and system calls.