In order to better understand the difference between different call types, let us set some context for discussion. We are referring here to modern operating system (OS) design concepts. So, what are the main goals of having an OS in the first place ? Well, the OS achieves two primary goals for users. First, Abstraction meaning the OS hides the underlying hardware details. Second, Isolation so that running processes do not interfere with each other. In other words, the OS enforces a security policy to control access to system resources so that users do not unintentionally break the rules.
Figure 1. Linux kernel System Call Interface
Now, the question is how does the OS implement that policy ? The answer is - as you may have guessed - System Calls. The OS provides an interface (API) for users to - indirectly - access system resources (See Figure 1). Since the OS is the trusted party to perform such actions, there are two main advantages. Users cannot unintentionally break the security policy. Also, the OS spares them the hassle of writing complex low level code. This separation between user actions and privileged OS actions introduces the concept of user space and kernel space. A call that runs in kernel space is a system call. System calls take care of low level services such as device, process, memory and file system management.
When user program invokes a system call, a context switch between user program and the kernel is performed. This means user program stops executing and its state is saved (pushed to the stack). The kernel is swapped in to complete the protected task. Results are returned back to the calling user application and the kernel is then swapped out. That is what a system call is in plain English. Let us now see the other call types.
Procedure Call vs Function Call vs Library Call
The difference between a function and a procedure is not of a big deal in our discussion. A function returns one output value while a procedure returns zero or more values. In either case, we are referring to non privileged user space calls where a program calls another block of code within the same user application. Library call on the other hand, is no different. Common code is usually shared between applications using a library but we are still referring to user space as opposed to the restricted kernel space. Long story short, a non system call is basically a function call. It is a non privileged call that does not require extra handling such as context switching mentioned earlier. Function calls do not interact with low level system resources directly but throw a system call. Let us now summarize.
Difference between api and system call
API (Application Programming Interface) is a general term that can refer to different things depending on the context. For example, allowing a software application to exchange information with a web service is done via an API exposed by the server. With that said, a system call (explained earlier) implements a specific type of API so that user level programs can access the kernel services.
Function, procedure and library calls all refer to the same concept which is calling a block of code in user space. On the other hand, system call refers to calling an operating system service running in privileged kernel space. This separation is intentionally designed to achieve to two major goals: security and convenience.
If you think this article was useful, please use the comments section below for questions, corrections or feedback. Thanks for reading.