You are on page 1of 21

What is Linux System Calls and Library Functions?

by HI MAN SH U AR O R A 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 $080 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 Directory Structure (File System Structure) Explained with Examples

by RA MESH N AT AR AJAN on SEPTEMBER 8, 2010

Have you wondered why certain programs are located under /bin, or /sbin, or /usr/bin, or /usr/sbin? For example, less command is located under /usr/bin directory. Why not /bin, or /sbin, or /usr/sbin? What is the different between all these directories? In this article, let us review the Linux filesystem structures and understand the meaning of individual high-level directories.

1. / Root

Every single file and directory starts from the root directory. Only root user has write privilege under this directory. Please note that /root is root users home directory, which is not same as /.

2. /bin User Binaries


Contains binary executables. Common linux commands you need to use in single-user modes are located under this directory. Commands used by all the users of the system are located here. For example: ps, ls, ping, grep, cp.

3. /sbin System Binaries


Just like /bin, /sbin also contains binary executables. But, the linux commands located under this directory are used typically by system aministrator, for system maintenance purpose. For example: iptables, reboot, fdisk, ifconfig, swapon

4. /etc Configuration Files


Contains configuration files required by all programs. This also contains startup and shutdown shell scripts used to start/stop individual programs. For example: /etc/resolv.conf, /etc/logrotate.conf

5. /dev Device Files


Contains device files. These include terminal devices, usb, or any device attached to the system. For example: /dev/tty1, /dev/usbmon0

6. /proc Process Information


Contains information about system process. This is a pseudo filesystem contains information about running process. For example: /proc/{pid} directory contains information about the process with that particular pid. This is a virtual filesystem with text information about system resources. For example: /proc/uptime

7. /var Variable Files


var stands for variable files. Content of the files that are expected to grow can be found under this directory. This includes system log files (/var/log); packages and database files (/var/lib); emails (/var/mail); print queues (/var/spool); lock files (/var/lock); temp files needed across reboots (/var/tmp);

8. /tmp Temporary Files

Directory that contains temporary files created by system and users. Files under this directory are deleted when system is rebooted.

9. /usr User Programs

Contains binaries, libraries, documentation, and sourcecode for second level programs. /usr/bin contains binary files for user programs. If you cant find a user binary under /bin, look under /usr/bin. For example: at, awk, cc, less, scp /usr/sbin contains binary files for system administrators. If you cant find a system binary under /sbin, look under /usr/sbin. For example: atd, cron, sshd, useradd, userdel /usr/lib contains libraries for /usr/bin and /usr/sbin /usr/local contains users programs that you install from source. For example, when you install apache from source, it goes under /usr/local/apache2

10. /home Home Directories


Home directories for all users to store their personal files. For example: /home/john, /home/nikita

11. /boot Boot Loader Files


Contains boot loader related files. Kernel initrd, vmlinux, grub files are located under /boot For example: initrd.img-2.6.32-24-generic, vmlinuz2.6.32-24-generic

12. /lib System Libraries

Contains library files that supports the binaries located under /bin and /sbin Library filenames are either ld* or lib*.so.*

For example: ld-2.11.1.so, libncurses.so.5.7

13. /opt Optional add-on Applications


opt stands for optional. Contains add-on applications from individual vendors. add-on applications should be installed under either /opt/ or /opt/ sub-directory.

14. /mnt Mount Directory

Temporary mount directory where sysadmins can mount filesystems.

15. /media Removable Media Devices


Temporary mount directory for removable devices. For examples, /media/cdrom for CD-ROM; /media/floppy for floppy drives; /media/cdrecorder for CD writer

16. /srv Service Data


srv stands for service. Contains server specific services related data. For example, /srv/cvs contains CVS related data.

5 Examples To Master Linux Command Line History


by RA MESH N AT AR AJAN on AUGUST 11, 2008

When you are using Linux command line frequently, using the history effectively can be a major productivity boost. In fact, once you have mastered the 15 examples that Ive provided here, youll find using command line more enjoyable and fun.

1. Display timestamp using HISTTIMEFORMAT


Typically when you type history from command line, it displays the command# and the command. For auditing purpose, it may be beneficial to display the timepstamp along with the command as shown below.
# # 1 2 3 4 export HISTTIMEFORMAT='%F %T ' history | more 2008-08-05 19:02:39 service network restart 2008-08-05 19:02:39 exit 2008-08-05 19:02:39 id 2008-08-05 19:02:39 cat /etc/redhat-release

2. Search the history using Control+R


I strongly believe, this may be your most frequently used feature of history. When youve already executed a very long command, you can simply search history using a keyword and

re-execute the same command without having to type it fully. PressControl+Randtypethe keyword . In the following example, I searched for red, which displayed the previous command cat /etc/redhatrelease in the history that contained the word red.
# [Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt] (reverse-i-search)`red': cat /etc/redhat-release [Note: Press enter when you see your command, which will execute the command from the history] # cat /etc/redhat-release Fedora release 9 (Sulphur)

Sometimes you want to edit a command from history before executing it. For e.g. you can search for httpd , which will display servicehttpdstop from the command history, select this command and changethe stopto start and re-execute it again as shown below.
# [Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt] (reverse-i-search)`httpd': service httpd stop [Note: Press either left arrow or right arrow key when you see your command, which will display the command for you to edit, before executing it] # service httpd start

3. Repeat previous command quickly using 4 different methods

Sometime you may end up repeating the previous commands for various reasons. Following are the 4 different ways to repeat the last executed command. 1. Use the up arrow to view the previous command and press enter to execute it. 2. Type !! and press enter from the command line 3. Type !-1 and press enter from the command line. 4. Press Control+Pwill display the previous command, press enter to execute it

4. Execute a specific command from history


In the following example, If you want to repeat the command #4, you can do !4 as shown below.
# 1 2 3 4 history | more service network restart exit id cat /etc/redhat-release

# !4 cat /etc/redhat-release Fedora release 9 (Sulphur)

5. Execute previous command that starts with a specific word


Type ! followed by the starting few letters of the command that you would like to re-execute. In the following example, typing !

ps and enter, executed the previous command starting with ps, which is ps aux | grep yp.
# !ps ps aux | grep yp root 16947 0.0 0.1 36516 1264 ? ypbind root 17503 0.0 0.0 4124 740 pts/0 grep yp

Sl 13:10 0:00 S+ 19:19 0:00

6. Control the total number of lines in the history using HISTSIZE


Append the following two lines to the .bash_profile and relogin to the bash shell again to see the change. In this example, only 450 command will be stored in the bash history.
# vi ~/.bash_profile HISTSIZE=450 HISTFILESIZE=450

7. Change the history file name using HISTFILE


By default, history is stored in ~/.bash_historyfile. Add the following line to the .bash_profile and relogin to the bash shell, to store the history command in .commandline_warrior file instead of .bash_history file. Im yet to figure out a practical use for this. I can see this getting used when you want to track commands executed from different terminals using different history file name.

# vi ~/.bash_profile HISTFILE=/root/.commandline_warrior

If you have a good reason to change the name of the history file, please share it with me, as Im interested in finding out how you are using this feature.

8. Eliminate the continuous repeated entry from history using HISTCONTROL


In the following example pwd was typed three times, when you do history, you can see all the 3 continuous occurrences of it. To eliminate duplicates, set HISTCONTROL to ignoredups as shown below.
# pwd # pwd # pwd # history | tail -4 44 pwd 45 pwd 46 pwd [Note that there are three pwd commands in history, after executing pwd 3 times as shown above] 47 history | tail -4 # export HISTCONTROL=ignoredups # pwd # pwd # pwd # history | tail -3 56 export HISTCONTROL=ignoredups

57 pwd [Note that there is only one pwd command in the history, even after executing pwd 3 times as shown above] 58 history | tail -4

9. Erase duplicates across the whole history using HISTCONTROL


The ignoredups shown above removes duplicates only if they are consecutive commands. To eliminate duplicates across the whole history, set the HISTCONTROL to erasedups as shown below.
# export HISTCONTROL=erasedups # pwd # service httpd stop # history | tail -3 38 pwd 39 service httpd stop 40 history | tail -3 # ls -ltr # service httpd stop # history | tail -6 35 export HISTCONTROL=erasedups 36 pwd 37 history | tail -3 38 ls -ltr 39 service httpd stop [Note that the previous service httpd stop after pwd got erased] 40 history | tail -6

10. Force history not to remember a particular command using HISTCONTROL


When you execute a command, you can instruct history to ignore the command by setting HISTCONTROL to ignorespace AND typing a space in front of the command as shown below. I can see lot of junior sysadmins getting excited about this, as they can hide a command from the history. It is good to understand how ignorespace works. But, as a best practice, dont hide purposefully anything from history.
# export HISTCONTROL=ignorespace # ls -ltr # pwd # service httpd stop [Note that there is a space at the beginning of service, to ignore this command from history] # history | tail -3 67 ls -ltr 68 pwd 69 history | tail -3

11. Clear all the previous history using option -c


Sometime you may want to clear all the previous history, but want to keep the history moving forward.
# history -c

12. Subtitute words from history commands

When you are searching through history, you may want to execute a different command but use the same parameter from the command that youve just searched. In the example below, the !!:$ next to the vi command gets the argument from the previous command to the current command.
# ls anaconda-ks.cfg anaconda-ks.cfg # vi !!:$ vi anaconda-ks.cfg

In the example below, the !^ next to the vi command gets the first argument from the previous command (i.e cp command) to the current command (i.e vi command).
# cp anaconda-ks.cfg anaconda-ks.cfg.bak anaconda-ks.cfg # vi !^ vi anaconda-ks.cfg

13. Substitute a specific argument for a specific command.


In the example below, !cp:2 searches for the previous command in history that starts with cp and takes the second argument of cp and substitutes it for the ls -l command as shown below.
# cp ~/longname.txt /really/a/very/long/path/longfilename.txt # ls -l !cp:2 ls -l /really/a/very/long/path/long-filename.txt

In the example below, !cp:$ searches for the previous command in history that starts with cp and takes the last argument (in this case, which is also the second argument as shown above) of cp and substitutes it for the ls -l command as shown below.
# ls -l !cp:$ ls -l /really/a/very/long/path/long-filename.txt

14. Disable the usage of history using HISTSIZE


If you want to disable history all together and dont want bash shell to remember the commands youve typed, set the HISTSIZE to 0 as shown below.
# export HISTSIZE=0 # history # [Note that history did not display anything]

15. Ignore specific commands from the history using HISTIGNORE


Sometimes you may not want to clutter your history with basic commands such as pwd and ls. Use HISTIGNORE to specify all the commands that you want to ignore from the history. Please note that adding ls to the HISTIGNORE ignores only ls and not ls -l. So, you have to provide the exact command that you would like to ignore from the history.

# # # # #

export HISTIGNORE="pwd:ls:ls -ltr:" pwd ls ls -ltr service httpd stop

# history | tail -3 79 export HISTIGNORE="pwd:ls:ls -ltr:" 80 service httpd stop 81 history [Note that history did not record pwd, ls and ls -ltr]

You might also like