The objective of this post is to explain what a System Call and Library Function is? How are they related to each other? Which of them is efficient?
What are System Calls?
The interface to the kernel is a layer of software called ‘System Calls’. These calls are defined by the underlying operating system and may not be fully portable.
The system calls are also termed as the entry points to the kernel and are not linked to application programs. The application code that is heavily dependent on system calls tend to be less portable across operating systems.
Since the system calls are part of the OS, the application programs need to context switch from user space to kernel space (also termed as ‘privileged mode’ or ‘superuser mode’) whenever a system call is invoked. This translates into an additional start-up overhead when compared with a library function.
As an example, consider the system call that is used to create a process. The system called used by Linux differs greatly in comparison to the one supported by Microsoft Windows®.
What are Libraries and Library functions?
Libraries are a set of common functions that can be used by application programs. These common functions are termed as ‘Library functions’. In most cases, the library functions adhere to certain standards. The ANSI C standard library is a good example. Hence, they are portable, which is a clear advantage over system calls.
In contrast to system call, the library functions are linked to application programs. Library functions always execute in user space (also termed as ‘user mode‘). Hence, they cannot interact directly with the hardware.
Library functions in-turn may utilize system calls for performing certain tasks which can only be carried out only in ‘kernel mode’.
The time consumed to execute a library function which does not in-turn invoke a system call is always lesser than the time consumed to execute a system call because the application program need not context switch between user space and kernel space.
Note: Applications are free to use either a system call or a library function to accomplish a task.
The image below illustrates the relationship between a Library Function, a System Call, the Kernel and the hardware.
- Advanced Programming in UNIX Environment, Second Edition (by W. Richard Stevens and Stephen A. Rago) – http://www.apuebook.com/index.html