“C 系统调用”和“C 系统调用”有什么区别? 和“C 库例程”?

发布于 2024-07-14 03:58:29 字数 455 浏览 8 评论 0原文

联机帮助页中有多个部分。 其中两个是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

哆兒滾 2024-07-21 03:58:29

系统调用是操作系统函数,就像 UNIX 上的 malloc()< /a> 函数构建在 sbrk() 系统调用(用于调整进程内存空间的大小)。

库只是应用程序代码,不属于操作系统的一部分,并且通常可在多个操作系统上使用。 它们与您自己的程序中的函数调用基本相同。

这条线可能有点模糊,但只是将系统调用视为内核级功能。

System calls are operating system functions, like on UNIX, the malloc() function is built on top of the sbrk() 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.

︶葆Ⅱㄣ 2024-07-21 03:58:29

常用函数库构建在系统调用接口之上,但应用程序可以自由使用两者。

系统调用就像有权使用内核资源的身份验证密钥。

在此处输入图像描述

上图来自高级 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.

enter image description here

Above image is from Advanced Linux programming and helps to understand how the user apps interact with kernel.

败给现实 2024-07-21 03:58:29

系统调用是用户级代码和内核之间的接口。 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.

2024-07-21 03:58:29

本手册第 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.

不…忘初心 2024-07-21 03:58:29

作为一般规则,您应该始终使用 C 库版本。 他们通常有包装器来处理深奥的事情,例如根据信号重新启动(如果您有要求)。 如果您已经与该库链接,则尤其如此。 所有规则都有被打破的理由。 使用直接调用的原因,

  1. 您希望与 libc 无关; 也许有安装程序。 此类代码可以在 Android 上运行 (bionic),uClibc,以及更传统的 glibc/eglibc 系统,无论使用什么库。 此外,使用包装器动态加载以创建运行时 glibc/bionic 层,允许双 Android/Linux 二进制文件。
  2. 您需要极致的性能。 尽管这种情况可能很少见,而且很可能被误导。 重新思考问题可能会带来更好的性能优势,并且调用系统通常会带来性能优势,libc 偶尔可以做到这一点。
  3. 您正在编写一些没有库的 initramfsinit 代码; 创建更小的映像或更快地启动。
  4. 您正在测试新的内核/平台,并且不想让成熟的文件系统使生活变得复杂; 与 initramfs 非常相似。
  5. 您希望在程序启动时非常快速地执行某些操作,但最终希望使用 libc 例程。
  6. 避免 libc 中的已知错误。
  7. 该功能无法通过 libc 获得。

抱歉,大多数示例都是 Linux 特定的,但其原理应该适用于其他 Unix 变体。 当新功能引入内核时,最后一项非常常见。 例如,当 kqueueepoll 首次引入时,没有 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,

  1. You want to be 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.
  2. You need extreme performance. Although this is probably rare and most likely misguided. Probably rethinking the problem will give better performance benefits and not calling the system is often a performance win, which the libc can occasionally do.
  3. You are writing some initramfs or init code without a library; to create a smaller image or boot faster.
  4. You are testing a new kernel/platform and don't want to complicate life with a full blown file system; very similar to the initramfs.
  5. You wish to do something very quickly on program startup, but eventually want to use the libc routines.
  6. To avoid a known bug in the libc.
  7. The functionality is not available through 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 or epoll where first introduced, there was no libc 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, Unix
will 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文