Labels

Monday 19 August 2019

How do library function is faster than System call?

Abstract:
In this article, we are going to see what is a library function and what is a system call. Also we go through the basic implementation for system call. Summary section explains about the conclusion about the summary.

What is a Library Function?

Library functions in c Language are inbuilt functions, which are grouped together and placed in a common place called library. Each library function in c performs specific operation. We can make use of these library functions to get the predefined output, instead of writing our own code to get those outputs. These library functions are created by the persons who designed and created C compilers. All C standard library functions are declared in many header files and which are saved as file_name.h. 

What is a system call?

At a high level system calls are "Services" offered by the kernel to user applications and they resemble library APIs in that they are described as a function call with a name, parameters and return value.
../_images/ditaa-48e96bb39e921ced06e8c3fe0d79112d7dd0c62b.png 
However, on a close look, we can see that system calls are actually not function calls, but specific assembly instructions (architecture and kernel specific) that do the following.
  • setup information to identify the system call and its parameters
  • trigger a kernel mode switch
  • retrieve the result of the system call
 In Linux, system calls are identified by numbers and the parameters for system calls are machine word sized (32 or 64 bit). There can be a maximum of 6 system call parameters. Both the system call number and the parameters are stored in certain registers.
 
In Summary, this is what happens during a system call:
  • The application is setting up the system call number and parameters and it issues a trap instruction
  • The execution mode switches from user to kernel; the CPU switches to a kernel stack; the user stack and the return address to user space is saved on the kernel stack
  • The kernel entry point saves registers on the kernel stack
  • The system call dispatcher identifies the system call function and runs it
  • The user space registers are stored and execution is switched back to user (eg: calling IRET)
  • The user space application resumes

Case study:

Library functions run in user space; in that aspect, they are no different than your own functions, They are executed by a simple far jump to the function entry point and there's no involvement of the OS.
 
Other hand, System calls run in kernel space, And system call is not a simple far jump. It requires generating a software interrupt , which will switch context from user process to kernel routine and then switch back. The context switching is what makes the syscall slower than common library/User function call.

Now, let's take a brief look at system library. In general, system libraries that are installed with the system, these may or may not make syscalls.  As per the process point of view, there is no difference in making/calling a system call whether a function is provided by the user binary or library or (shared) library installed with system; These are just libraries loaded into memory and linked to the user program. The addresses are resolved upon library loading.

Conclusion:

There is no difference in making system call from your code or making it transitively via library function. The library function is always faster than the system call, as the system call involves context switching and takes more CPU cycles (More Instructions).