System Call in Linux Os
System Call in Linux Os
• In Linux, system calls are invoked using the "int 0x80" assembly
language instruction. "int 0x80" acts as an gateway to all the system
calls implemented by the Linux kernel.
• Each system call has a unique number identifying the system call.
You can find the system call numbers in the file
/usr/include/asm/unistd.h For example the fork system call has a
system call number of 2. You can find the system call numbers also
in the asm/unistd.h file in the Linux kernel source directory.
Example
• read(fd,buffer,size);
• This corresponds to a system call with three arguments. So this will be expanded by the
_syscall3 macro.
• A statement “static inline syscall3(int,read,int,fd,char *,buf,off_t,len)” has been added in the
header file for the macro expansion to take place.
• After the expansion the system call number will be in register 'zero' and the argument to the
system call will be in the general purpose registers of the processor.
• Also the macro will call the "int 0x80" instruction after loading the registers. So the kernel
mode is initiated and kernel will execute on behalf of the process initiated the system call.
• The "int 0x80" instruction will call the system call handler.
• Each system call will have a routine or program defined in the kernel. Address of each of
these routine are stored in the in array named "sys_call_table". You can find the code
corresponding to the system call handler and sys_call_table in the file “/usr/src/linux-
/arch/i386/kernel/entry.S”.
• The path name has to be filled accordingly for different kernel version. The system call
handler will call the service routine corresponding to the system call, by looking at the system
call number loaded in the register "zero". So the service routine corresponding to the read
system call will be executed. After executing the service routine the control comes back to
the system call handler and it will then give control back to the user process, also the mode
of operation is changed to user mode.
Number of system calls
• The kernel will have a limit on the number of
system calls. The value is stored in the
"NR_syscalls" variable.
#include <linux/myroutine.h>
main(){
int sum;
sum=myroutine(10,10);
printf(“%d”,sum);
}
You should get a 20 printed while you execute above program.
• Before running the program you have to compile the kernel
and boot the system with the compiled kernel.