'How to distinguish calling a c library function from making a system call?
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?
Solution 1:[1]
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.
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Employed Russian |
| Solution 2 | Joseph Sible-Reinstate Monica |
