Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the reasons to use "syscall" instead of calling the function directly?

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?

like image 698
Drakosha Avatar asked Sep 05 '12 16:09

Drakosha


People also ask

Why do we use syscall?

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.

What is difference between function call and 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.

Is syscall a function?

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.

Why syscall is used in Linux?

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).


1 Answers

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.)

like image 148
Nemo Avatar answered Nov 15 '22 19:11

Nemo