为什么我应该尽量减少代码中系统调用的使用?
我想知道是否有任何理由尽量减少代码中系统调用的使用,以及不使用系统调用的替代方案是什么,有人会说使用 API,但 api 反过来使用系统调用,
这是真的吗?
I wanted to know is there any reason to minimize use of system call in code and what is the alternate of not using system call ,one would say use API but api in turns use system call
Is it True??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为大多数系统调用都有固有的开销。系统调用是一种进入内核的方法,是获取某些服务的受控网关。
执行系统调用时,会采取一些操作(警告,这是一种简化):
eax
中int 0x80
或其他)>system_call
例程我可能忘记了一些步骤。这听起来是不是工作量很大?您想要的只是粗体部分。其余的都是开销。
Because most system calls have an inherent overhead. A system call is a means of tapping into the kernel, a controlled gateway towards obtaining some service.
When performing a system call, some actions are taken (warning, it's a simplification):
eax
int 0x80
or whatever)system_call
routineAnd I probably forgot some of the steps. Doesn't this sound like a lot of work ? All you wanted is the bold part. The rest is overhead.
系统调用要求系统从用户模式切换到内核模式。这使得系统调用变得昂贵。
一篇文章可以更好地理解这一点:
A system call requires that the system switches from User mode to Kernel mode. This makes system calls expensive.
An article to understand this better:
首先,如果您使用框架或 API(例如,通过使用 wxWidgets 而不是手动渲染窗口,或使用 GNU C 库),您的代码可以在不同操作系统之间移植。
其次,如果您使用 API,如果制造商更改了操作系统在底层的工作方式,那么您不会遇到问题,因为 API(应该)与以前相同。
First, if you use framework or APIs (e.g. by using wxWidgets instead of rendering the windows manually, or the GNU C library) your code is portable between different operating systems.
Second, you if you're using APIs you won't have problems if the manufacturer changes how the operating system works under the hood, as the APIs (should) be the same as before.
我现在想到的唯一原因是可移植性问题。如果您使用系统调用,您的代码将仅在该操作系统上运行。而如果你需要将相同的源代码编译到另一个操作系统,你就会遇到麻烦,API可能完全不同。
The only reason that cames to my mind right now is portability issues. If you use system calls, your code will only run on that Operating System. And if you need to compile the same source to another OS, you will be in trouble, the API may be completely different.