Home > OS >  How to distinguish calling a c library function from making a system call?
How to distinguish calling a c library function from making a system call?

Time:04-24

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?

CodePudding user response:

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.

CodePudding user response:

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.
  • Related