There's syscall which allows indirect system calls in Linux. What are the reasons to use it - and why is it better than direct call to the function?
System calls are usually made when a process in user mode requires access to a resource. Then it requests the kernel to provide the resource via a system calls. If a file system requires the creation or deletion of files. Reading and writing from files also require a system call.
The main difference between system call and function call is that a system call is a request for the kernel to access a resource while a function call is a request made by a program to perform a specific task.
syscall() is a small library function that invokes the system call whose assembly language interface has the specified number with the specified arguments. Employing syscall() is useful, for example, when invoking a system call that has no wrapper function in the C library.
A system call is a function that allows a process to communicate with the Linux kernel. It's just a programmatic way for a computer program to order a facility from the operating system's kernel. System calls expose the operating system's resources to user programs through an API (Application Programming Interface).
Sometimes the kernel adds system calls and it takes a while for the C library to support them.
Or maybe you are compiling on an old Linux distribution, but want to run on a newer one.
Example code:
// syscall 277 is sync_file_range() on x86_64 Linux. The header
// files lack it on scc-suse10 where we compile, but the
// performance benefits are substantial, so we just call it
// directly. FIXME someday.
#define SYNC_FILE_RANGE_WRITE 2
syscall(277, fd, done, n, SYNC_FILE_RANGE_WRITE);
But in general, there is no advantage to using syscall
if the C library in your compilation environment has what you need. (For one thing, it is even less portable than using a Linux-specific interface, since the system call numbers vary by CPU.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With