使用“系统调用"的原因是什么?而不是直接调用函数? [英] What are the reasons to use "syscall" instead of calling the function directly?
问题描述
syscall 允许在Linux中进行间接系统调用.使用它的原因是什么-为什么它比直接调用该函数更好?
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?
推荐答案
有时内核会添加系统调用,而C库需要一段时间才能支持它们.
Sometimes the kernel adds system calls and it takes a while for the C library to support them.
或者也许您正在使用旧的Linux发行版进行编译,但希望在较新的Linux发行版上运行.
Or maybe you are compiling on an old Linux distribution, but want to run on a newer one.
示例代码:
// 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);
但是,通常,如果编译环境中的C库满足您的需要,则使用syscall
没有任何优势. (一方面,它的可移植性比使用Linux特定的接口还要差,因为系统调用号因CPU而异.)
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.)
这篇关于使用“系统调用"的原因是什么?而不是直接调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!