开发者

Why should I Minimize the use of system call in my code?

I wanted to know is there any reason to minimize use of system call in code and what is the alternate of not开发者_StackOverflow社区 using system call ,one would say use API but api in turns use system call

Is it True??


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

  • You invoke a library (wrapper) function
  • The function puts the arguments where they are expected. Also the function puts the number of the system call in eax
  • The function calls a trap (int 0x80 or whatever)
  • The processor is switched to kernel mode
  • The kernel invokes some system_call routine
  • The registers are saved onto the kernel stack
  • The arguments are checked to be valid
  • The action is performed
  • The registers are restored from the kernel stack
  • The processor is returned to user mode
  • The function (finally...) returns

And 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:

  1. Understanding User and Kernel Mode - Jeff Atwood


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜