What is Linux System Calls and Library Functions?

by Himanshu Arora on July 4, 2012

Computer software are developed to either automate some tasks or solve some problems. Either way, a software achieves the goal with the help of the logic that the developer of that software writes. Every logic requires some services like computing the length of a string, opening a file etc. Standard services are catered by some functions or calls that are provided for this purpose only.

Like for calculating string length, there exists a standard function like strlen(), for opening a file, there exists functions like open() and fopen(). We call these functions as standard functions as any application can use them.

These standard functions can be classified into two major categories :

  1. Library function calls.
  2. System function calls.

In this article, we will try to discuss the concept behind the system and library calls in form of various points and wherever required, I will provide the difference between the two.

1. Library functions Vs System calls

The functions which are a part of standard C library are known as Library functions. For example the standard string manipulation functions like strcmp(), strlen() etc are all library functions.

The functions which change the execution mode of the program from user mode to kernel mode are known as system calls. These calls are required in case some services are required by the program from kernel. For example, if we want to change the date and time of the system or if we want to create a network socket then these services can only be provided by kernel and hence these cases require system calls. For example, socket() is a system call.

2. Why do we need system calls?

System calls acts as entry point to OS kernel. There are certain tasks that can only be done if a process is running in kernel mode. Examples of these tasks can be interacting with hardware etc. So if a process wants to do such kind of task then it would require itself to be running in kernel mode which is made possible by system calls.

3. Types of library functions

Library functions can be of two types :

  • Functions which do not call any system call.
  • Functions that make a system call.

There are library functions that do not make any system call. For example, the string manipulation functions like strlen() etc fall under this category. Also, there are library functions that further make system calls, for example the fopen() function which a standard library function but internally uses the open() sytem call.

4. Interaction between components

The following diagram to depict how Library functions, system calls, application code interact with each other.


The diagram above makes it clear that the application code can interact with Library functions or system calls. Also, a library function can also call system function from within. But only system calls have access to kernel which further can access computer hardware.

5. fopen() vs open()

Some of us may argue that why do we have two functions for the same operation ie opening a file?

Well, the answer to this is the fact that fopen() is a library function which provides buffered I/O services for opening a file while open() is a system call that provides non-buffered I/O services. Though open() function is also available for applications to use but application should avoid using it directly.

In general, if a library function corresponding to a system call exists, then applications should use the library function because :

  • Library functions are portable which means an application using standard library functions will run on all systems. While on the other hand an application relying on the corresponding system call may not run on every system as system call interface may vary from system to system.
  • Sometimes the corresponding library function makes the load to system call lesser resulting in non-frequent switches from user mode to kernel mode. For example if there is an application that reads data from file very frequently, then using fread() instead of read() would provide buffered I/O which means that not every call to fread() would result in a call to system call read(). The fread() may read larger chunk of data(than required by the user) in one go and hence subsequent fread() will not require a call to system function read().

6. Is malloc() a system call?

This is one of the very popular misconception that people have. Lets make it clear that malloc() is not a system call. The function call malloc() is a library function call that further uses the brk() or sbrk() system call for memory allocation.

7. System calls : Switching execution modes

Traditionally, the mechanism of raising an interrupt of ‘int $0×80′ to kernel was used. After trapping the interrupt, kernel processes it and changes the execution mode from user to kernel mode. Today, the systenter/sysexit instructions are used for switching the execution mode.

8. Some other differences

Besides all the above, here are a few more differences between a system and library call :

  • A library function is linked to the user program and executes in user space while a system call is not linked to a user program and executes in kernel space.
  • A library function execution time is counted in user level time while a system call execution time is counted as a part of system time.
  • Library functions can be debugged easily using a debugger while System calls cannot be debugged as they are executed by the kernel.

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 17 comments… read them below or add one }

1 Jalal Hajigholamali July 4, 2012 at 8:51 am

Hi,

Very very nice and useful paper…

2 Pierre B. July 5, 2012 at 1:08 am

Thanks, i will be enjoying this article at my lunch break (in 3 hours …)
Thanks a lot TGS.

3 bob July 5, 2012 at 7:18 am

Excellent article as usual…
BTW what is the “s” in “sbreak” mean? I sometimes need to know what the word is so that I can remember it later.

4 uvreticent July 5, 2012 at 9:48 am

Nice One ,subtlities of C exposed!!!!!!!!

5 Rajesh Kumar V July 7, 2012 at 8:16 am

every beginner must read this .good Article.

Big Thanks.

Rgds,
Rajesh

6 Rupali Sharma July 16, 2012 at 9:50 am

Excellent article…
Really nicely explained system calls and library calls. :)
One can use ‘strace’ to determine system calls being used in a c program.

7 jay October 8, 2012 at 9:32 am

please i want to know the critical difference between a system call in linux and windows…
thank you very much

8 Himanshu January 6, 2013 at 7:20 am

Excellent article . was very useful .
Thanks :)

9 aneesh February 26, 2013 at 5:11 am

relay use full.excellent .thank u

10 Fazlan March 8, 2013 at 11:12 am

very well done! Thanx

11 sooraj s.p July 31, 2013 at 10:04 am

very helpful.

12 jack August 12, 2013 at 1:02 am

thanks..

13 Manu Juma August 15, 2013 at 6:35 pm

Very Good Explanation.

14 shankar N September 15, 2013 at 7:59 am

good one………….

15 Fongoh March 26, 2014 at 5:26 am

Thanks. That reall helped me in my quest.
I will like to know more of those library routines that tend to make system calls.
Links or direct responses will be welcomed.
Thanks again

16 SB April 17, 2014 at 11:50 am

Very simple but excellent article.

17 G,M August 11, 2014 at 12:39 am

Thanks for that

Leave a Comment

Previous post:

Next post: