Sunday, February 10, 2013

Linux system calls


System call is a call to kernel for specific kernel services by the process. A system call is not an ordinary function call, it's a special procedure which is required to transfer control to the kernel. The set of Linux system calls forms the most basic interface between programs and the Linux kernel. Each call presents a basic operation or capability. Most of the system calls in Linux are located in /usr/include/unistd.h and in current Linux flavor system calls are located in /usr/include directory. Let's start discussing some of the basic system calls used for file operations.

1. access:-
            This is a system call used to test the file permission. The access system call is used to determine whether the process has permission for accessing file. File permission can be read, write, execute or any combination among them. The syntax for access system call is:    
                access (path, permissions)
             
The first argument specifies the path to the file to be tested and the second argument specifies the list of file permission to be tested. The system calls returns 0 if the file has specified all the permission written in system call. If the file has not associated with the permission specified in system call then it returns -1.

2. uname:-
            The system call uname returns system statistics such as operating system, version, processor architecture (i386, i686) etc. The uname system call is located in  sys/utsname.h file. Also there is command available for getting system statistics i.e. “uname [option]”. The system call uname uses the pointer to structure utsname. Syntax for the uname system call is: 
            uname(&p)
Here &p is pointer to structure utsname.

3. create:-
            This system call is used to create new file. It returns -1 if any error occurred while creating file. The system call create takes two arguments, first is the path where the file is created and second is the permission i.e. mode (read,write,execute) to be assigned to this file. Syntax for create system call: 
           create(path,mode)

4. open:-
            The system call open is used to open or create file. This system call returns file descriptors. It returns -1 in case of errors. The number of arguments in the open system call can be two or more. First argument specifies the path of the file. If we want to open existing file then we require only two arguments. If we require to create file using open system call  then we require three arguments. Syntax for open system call is: 
           open(path,mode)