Systems Programming :: System Calls

Saturday, April 3, 2010 Posted by Kanishka Dilshan
Operating system divides the system memory(RAM) into two parts.
  • Kernel Space 
  • User Space
The memory area reserved for kernel is used by device drivers and kernel extensions only.
The user space is the reserved memory area for user mode applications. User space can be swapped out to disk when necessary. Normally the kernel space is not swapped out to disk.

The kernel provides important services for the user mode applications.
  • Opening files
  • Reading files
  • Writing to files
  • Making network connections
  • Making new processes
System calls are used to make use of these services.
Relation between kernel space and the user space
Note:  I have used open() system call for this demontration.

void main()
{
    printf("This is a user mode application\n");
    ......................................
    ......................................
    ......................................
    int FD=open("MyFile.txt",0);
    ......................................
    ......................................
}
                                             User Space
_______________________________________________________
                                          
                                           Kernel Space
int open(char *_fileName, int mode)
{
        ......................................
        ......................................
        push    dword mode
        push    dword flags
        push    dword _fileName
        mov eax, 5
        add esp, byte 12
        ......................................
        ......................................
        ......................................
}


Difference between Library Functions and System Calls
  • System calls
           Executed by the OS
           Generally performs an single specific operation
  • Library Functions
           Executed in the user program
           May call system calls
           May perform more than one task
           More reliable than system calls

The relationship between system calls and library functions can be depicted as follows.

Library Functions System Calls
fopen() create(),open(),close()
fclose() read(),write()
system() chmod(),chown()
malloc() fork()
getc() unlink()

Refer following web site for more C library functions.
Labels:

Post a Comment