Professional Documents
Culture Documents
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.
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.
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().
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.
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 /.
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.
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
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
Contains device files. These include terminal devices, usb, or any device attached to the system. For example: /dev/tty1, /dev/usbmon0
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
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);
Directory that contains temporary files created by system and users. Files under this directory are deleted when system is rebooted.
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
Home directories for all users to store their personal files. For example: /home/john, /home/nikita
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
Contains library files that supports the binaries located under /bin and /sbin Library filenames are either ld* or lib*.so.*
opt stands for optional. Contains add-on applications from individual vendors. add-on applications should be installed under either /opt/ or /opt/ sub-directory.
Temporary mount directory for removable devices. For examples, /media/cdrom for CD-ROM; /media/floppy for floppy drives; /media/cdrecorder for CD writer
srv stands for service. Contains server specific services related data. For example, /srv/cvs contains CVS related data.
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.
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
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
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
# 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.
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
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
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
# # # # #
# 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]