Reputation: 179
I have been writing programs in C/C++ that make use of the Linux API and make system calls like fork(),read(),write() etc. Now, I am beginning to wonder if these library functions are actually system calls, or are they some kind of wrapper functions.
What really happens when a program makes a call to write() ? How does this function interact with the kernel ? If this is a wrapper then why do we need it ?
Upvotes: 8
Views: 1476
Reputation: 2558
using this simple code :
int main()
{
int f = open("/tmp/test.txt", O_CREAT | O_RDWR, 0666);
write(f, "hello world", 11);
close(f);
return 0;
}
you can use strace
to find system calls used in the binary file :
gcc test.c -o test
strace ./test
the result is something like this :
.
.
.
open("/tmp/test.txt", O_RDWR|O_CREAT, 0666) = 3
write(3, "hello world", 11) = 11
close(3) = 0
exit_group(0) = ?
as for fork()
, it's actually a wrapper around clone()
system call
Upvotes: 1
Reputation: 25693
All such functions are real userspace functions in libc.so
that your binary is linked against. But most of them are just tiny wrappers for syscalls which are the interface between the userspace and the kernel (see also syscall(2)
).
Note that functions that are purely userspace (like fmod(3)
) or do some things in userspace in addition to calling the kernel (like execl(3)
) have their manpages in the section 3 while functions that just call the kernel (like read(2)
) have them in the section 2.
Upvotes: 6