Javatpoint Logo
Javatpoint Logo

Difference between System Call and Library Call

In this article, you will learn the difference between the System Call and Library Call. But before discussing the differences, you must know about the System Call and Library Call.

What is a System Call?

It is a method by which a process communicates with the OS. It is a programmatic approach that enables a computer program to request a service from the operating system's kernel. System call provides the operating system's services to user programs via an API (Application Programming Interface). The kernel system's single-entry points are system calls.

There are two modes available in a computer system: the kernel mode and the user mode. In the computer system, various types of programs or apps are executed on a computer system. Programs have direct access to hardware resources and memory in kernel mode, which is more privileged than user mode. In user mode, programs are unable to access memory and hardware resources directly. Most programs run in the user mode. A program sends a system call request to the kernel when it needs a hardware resource and memory. Following that, the mode changes from user mode to kernel mode. When the work is finished, the mode switches back to user mode from kernel mode. As a result, this mode transition is also known as context switching.

There are mainly two system calls in the UNIX system are as follows:

  1. Fork()
  2. Exec()

fork()

It is used to start a new process while keeping the existing one running. When a specific process calls fork(), it makes a copy of a process. As a result, there are two processes. The primary process is the parent process, while the second is the child process.

exec()

It is used to start a new process and replace the old running process. As a result, after calling exec(), only the new process exists.

What is a Library Call?

It is a request to a programming library to use a certain function. A library file contains code, data, and object files that are stored into a single file for use. A library must be imported before performing a library call, and the library call may rely on a system call.

In C programming, the user may use library functions by adding the header files in the program. The preprocessor directive (#include) helps to add the header files. Various functions for executing input and output operations are included in the stdio.h header file. If you want to open a file, use the fopen() function, and if you want to close the file, use the fclose() function. The printf() aids in transmitting formatted output to a standard output device, whereas the scanf() aids in reading formatted input from a standard input device.

Moreover, the "math.h" header file also offers the functions to perform the mathematical computations. The "time.h" header file contains functions for calculating time and date. The "string.h" header file contains functions for manipulating strings.

Main Differences between the System Call and Library Call

System Call vs Library Call

Here, you will learn the main differences between the System Call and Library Call. Various differences between the System Call and Library Call are as follows:

  1. A system call is a kernel function that allows you to enter kernel mode and access a hardware resource. On the other hand, a library call is a function provided by a programming library to complete a task.
  2. The mode switches from user to kernel mode in the system call function. On the other hand, there is no mode switching in the library call function.
  3. The system call takes longer to execute than the library call. On the other hand, the library call is faster to execute than the system call.
  4. All functions are part of the kernel in the system call. On the other hand, all library functions in a library call are part of the programming language's standard library file.
  5. It is a program's request to access a process in kernel mode. On the other hand, a library call is a program's request to access a library function provided in a programming library.

Head-to-head Comparison between the System Call and Library Call

Here, you will learn the head-to-head comparison between the System Call and Library Call. The main differences between the System Call and the Library Call are as follows:

System Call Library Call
All functions are part of the kernel in the system call. All library functions in a library call are part of the programming language's standard library file.
It is the kernel's entry point. So, it is not linked to the program. It is linked to your program.
The system call takes longer to execute than the library call. The execution speed of the library call is faster than the system call.
There is mode switching, in which the mode changes from the user mode to kernel mode. There is no switching mode.
It is a kernel function that allows you to enter kernel mode and access a hardware resource. It is a function that the programming library provides to perform a task.
It is executed in kernel mode. It is executed in user mode.
It is a program's request to access a process in kernel mode. It is a request by a program to use a library function in a programming library.
It is not portable. It is portable.
These are offered by the system and implemented by the system kernel. These are added to the ANSI C standard library.
Examples of the system calls are:
  1. Fork()
  2. Exec()
Examples of the library calls are:
  1. Fopen()
  2. Fclose()
  3. Printf()
  4. Scanf()

Conclusion

A system call is executed in kernel space, whereas a library call is executed in user space. A library call is a function given by programming libraries. In contrast, a system call is a kernel function that allows you to enter kernel mode and access a hardware resource. Library calls may rely on the system calls to finish the task. Some instances of system calls are cd(), fork(). Some examples of library calls are fopen(), fprintf ().







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA