Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 using system call ,one would say use API but api in turns use system call

Is it True??

like image 543
Amit Singh Tomar Avatar asked Jun 21 '11 11:06

Amit Singh Tomar


People also ask

What is the purpose of using system calls?

System call provides the services of the operating system to the user programs via Application Program Interface(API). It provides an interface between a process and operating system to allow user-level processes to request services of the operating system.

What are the advantages of system call?

The most important benefit of a system call is simplicity. You should not have to write a complex program in order to open or save a file to the disk, or print a document. Further, you don't want to have anything become compromised in the operating system, such as device drivers or other system components.

Why do we use API instead of system call?

Another difference between API and system call is their usage. An API helps to exchange data between various systems, devices and applications while a system call allows a program to access services from the kernel of the operating system.

Why is a system call more expensive than a procedure call?

Solution : A system call is expected to be significantly more expensive than a procedure call (provided that both perform very little actual computation). A system call involves the following actions, which do not occur during a simple procedure call, and thus entails a high overhead: A context switch.


2 Answers

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
like image 200
Ozair Kafray Avatar answered Dec 08 '22 16:12

Ozair Kafray


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.

like image 20
cnicutar Avatar answered Dec 08 '22 14:12

cnicutar