系统调用

何谓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;}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值