Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are function calls like read() , write() actual system calls in linux?

Tags:

c++

c

linux

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 ?

like image 516
Tushar Poddar Avatar asked Feb 28 '16 07:02

Tushar Poddar


2 Answers

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.

like image 165
wRAR Avatar answered Nov 17 '22 04:11

wRAR


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

like image 1
arash kordi Avatar answered Nov 17 '22 04:11

arash kordi