何谓system call? 很多人认为system call就是我们编程中使用的各种API,其实不然,我们在编程中调用的各种API其实是对system call的一种包装,例如:我们在程序中使用printf(),这个printf()就是libc对各种system call的封装。在linux 2.6.16.4中有两种方式实现system call,从user space 切换到kernel space1. int $0x80 对应返回使用iret2. sysenter 对应返回使用sysexit如何编写system call?在 arch/***/kernel/syscall_table.S ***为具体的体系(例如I386)添加一个新表项 ENTRY(sys_call_table) .long sys_restart_syscall /* 0 - old "setup()" system call, used for restarting */ .long sys_exit .long sys_fork .long sys_read .long sys_write .long sys_open /* 5 */ .long sys_close .long sys_waitpid .long sys_creat .long sys_link .long sys_unlink /* 10 */ .long sys_execve .long sys_chdir .long sys_time .long sys_mknod .long sys_chmod /* 15 */ …… …… .long sys_mycall /* 313*/ 例如添加sys_mycall 在 include/Asm-***/unistd.h中添加,其中的313为sys_mycall 在sys_call_table中的序号,从0开始编号 #define __NR_mycall 313 kernel/Sched.c //把实现放在Sched.c中,可以任意,最好放在功能相关文件中//=====================================================================// 返回当前pid//=====================================================================#include <……>//包含必要的头文件asmlinkage long sys_mycall(void){ return current->tgid; //返回当前pid} 修改完毕后重新编译内核。 test.c//=====================================================================// 返回当前pid//=====================================================================#include <stdio.h> #define __NR_mycall 313__syscall0(long, mycall) int main(){ long pid = 0; pid = mycall(); printf("当前进程PID为:%d",); return 0;}
系统调用
最新推荐文章于 2023-07-04 12:25:22 发布