"fopen is much faster [then fopen
] due to buffering, but under the hood it calls open..."
In general, by definition if function1()
implementation includes calling function2()
, then calling function2()
directly, and if using the same option set as when called by function1()
, will always have a shorter execution time. If you are seeing the opposite with fopen()
and open()
, then it suggests the option set used when you are calling open()
directly would have to be different than when it is called within fopen()
. But the implementation of the internal do_sys_open()
has the same number of arguments open()
, so speed differential for that reason is not possible. You should question your bench-marking technique.
Regarding how return values is returned to the user...
Linux system calls are defined using variations of SYSCALL_DEFINEn. The following example implementation of open()
illustrates this, and shows that in the encapsulation of the function do_sys_open()
, one of the arguments include const char __user *
in both the macro
and the function, allowing it to track from which user the call was initiated:
long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
{
struct open_flags op;
int fd = build_open_flags(flags, mode, &op);
struct filename *tmp;
if (fd)
return fd;
tmp = getname(filename);
if (IS_ERR(tmp))
return PTR_ERR(tmp);
fd = get_unused_fd_flags(flags);
if (fd >= 0) {
struct file *f = do_filp_open(dfd, tmp, &op);
if (IS_ERR(f)) {
put_unused_fd(fd);
fd = PTR_ERR(f);
} else {
fsnotify_open(f);
fd_install(fd, f);
}
}
putname(tmp);
return fd;
}
SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
{
if (force_o_largefile())
flags |= O_LARGEFILE;
return do_sys_open(AT_FDCWD, filename, flags, mode);
}
open
by itself. He callsopen
,read
andwrite
, out of whichopen
probably forms a tiny, insignificant part of the overhead. It is called only once, while the other calls are made in a loop! Most of the time would be spent in read and write. – th33lf