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

I wanted to know if there are any reasons to minimize the use of the system call in the code and what is the alternative to not using the system call, we can say that use the API, but api uses the system call in turn

It's true?

+4
source share
4 answers

Since most system calls have overhead. A system call is a means of listening to the kernel of a managed gateway to receive some service.

When making a system call, some actions are performed (warning, this is a simplification):

  • You call the library (wrapper) function
  • The function places the arguments where they are expected. The function also puts the system call number in eax
  • The function calls a trap ( int 0x80 or something else)
  • CPU switches to kernel mode
  • The kernel calls some system_call procedure
  • Registers are saved to the kernel stack
  • The arguments are validated.
  • Action in progress
  • Registers are restored from the kernel stack
  • The processor returns to user mode
  • The function (finally ...) returns

And I probably forgot some steps. Doesn't that seem like a lot of work ? All you need is the bold part. The rest is overhead.

+11
source

A system call requires the system to switch from user mode to kernel mode . This leads to costly system calls.

An article to understand this better:

+1
source

Firstly, if you use a framework or API (for example, using wxWidgets instead of manually rendering Windows or the GNU C library), your code wraps between different operating systems.

Secondly, if you use the APIs, you will not have problems if the manufacturer changes the operation of the operating system under the hood, since the API (should) be the same as before.

0
source

The only reason I'm talking about is portability issues. If you use system calls, your code will only work on this operating system. And if you need to compile the same source into another OS, you will have problems, the API can be completely different.

0
source

All Articles