如何区分呼叫AC库功能与进行系统调用?

发布于 2025-01-23 23:06:45 字数 392 浏览 2 评论 0原文

有c库函数 pipe(3) 和kernel(系统呼叫)< a href =“ https://linux.die.net/man/2/pipe” rel =“ nofollow noreferrer”> pipe(2)。 两者都具有相同的签名,应该像这样使用(包括标头):

#include <unistd.h>
int fds[2];
pipe(fds);

此代码调用管道(3)或管道(2)会吗? 我如何决定要使用LIBC还是系统调用? 如果管道(3)和管道(2)相同,我怎么知道?

There is the C library function pipe(3) and the kernel (system call) pipe(2).
Both have the same signature and should be used like this (same include header):

#include <unistd.h>
int fds[2];
pipe(fds);

Will this code call pipe(3) or pipe(2)?
How can I decide whether I want to use libc or a system call?
If pipe(3) and pipe(2) are the same, how do I know that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

身边 2025-01-30 23:06:45

此代码调用管道(3)或管道(2)?

它将调用管道(3)

可以直接调用c直接调用

  • ),或
  • 在执行适合体系结构的系统调用指令之前,使用syscall(2)将正确的参数“插入”正确的寄存器中,或
  • 提供您自己的汇编包装器,该包装器将执行相同的操作或
  • 使用Inline __ ASM __做同样的事情。

Will this code call pipe(3) or pipe(2)?

It will call pipe(3).

There is no way to call the system call directly from C, you either

  • have to call libc wrapper for such system call (if one is provided), or
  • use syscall(2) to "stuff" the right arguments into the right registers before executing architecture-appropriate system call instruction, or
  • provide your own assembly wrapper which will do the same, or
  • use inline __asm__ to do the same.
慵挽 2025-01-30 23:06:45

我认为您在没有一个的地方做出了区别。您的代码将调用管道库功能,这只是PIPE系统调用围绕的包装器。这不是一个/或。第3节手册页来自POSIX程序员的手册,第2节手册页面是Linux特定的。

I think you're making a distinction where there isn't one. Your code will call the pipe library function, which is just a wrapper around the pipe system call. It's not an either/or. The section 3 manual page is from the POSIX programmer's manual, and the section 2 manual page is Linux-specific.

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