How does a C library call kernel system calls
I know in Unix-like systems c librarys such as glibc acts as an intermediary between the kernel and the userland. So for example when implementing malloc()
how does glibc invoke system calls of the linux kernel does it use assembly?
In Linux x86 syscalls (system calls) are made by calling interrupt 0x80
. In assembly it is done with:
int $0x80
The choice of syscall is done by passing information to the CPU registers. malloc
itself is not a syscall, but malloc
algorithm usually uses sbrk
or mmap
syscalls ( brk
syscall for sbrk
).
For more information on Linux x86 syscalls, you can read this document.
EDIT: as mentioned by Jester in the comments, Intel x86 processors (after Pentium IV) now support systenter
/ sysexit
instructions that don't have the int
overhead and on these processors these instructions are used by Linux for syscalls.
Example of calling exit(0) syscall on 0x86 architecture.
movl $1, %eax #$1=number of exit syscall.
movl $0, %ebx #$0=First argument of exit syscall
int 0x80 #call the software interrupt
Every syscall has been given a number that can be found in /usr/include/asm/unistd.h. In the above example exit syscall has number 1. Then you set the argument required for the syscall. After that you call software interrupt int 0x80.
So for malloc, it will internally call brk or mmap and then it will set the required arguments and then will call int0x80.
链接地址: http://www.djcxy.com/p/90880.html上一篇: 有没有原始的Linux系统调用API / ABI文档
下一篇: C库如何调用内核系统调用