(转)执行fork函数的曲折过程

执行fork函数的曲折过程
        ----------学习系统过程调用
  本文是鄙人在学习中linux源码中的一些体会,希望对初学者有所帮助。
一.准备和初始化阶段
1. ~/kernel/system_call.s
_sys_fork 函数本身 entry point=0x751c
2. ~/include/linux/sys.h
_sys_call_table[] = { sys_setup, sys_exit, sys_fork, sys_read..} 地址=0x17034
_sys_call_table 将_sys_fork 函数的地址放入表中
3. ~/kernel/system_call.s
_system_call
call sys_call_table(,%%eax,4)
当eax=0x2 时,间接调用_sys_fork
4. ~/kernel/sched.c
sched_init(void)
set_system_gate(0x80,&system_call);
将system_call的地址放入IDT[0x80]中。
5. ~/include/asm/system.h
#define set_system_gate(n,addr) _set_gate(&idt[n],15,3,addr)
#define _set_gate(gate_addr,type,dpl,addr) /
__asm__ ("movw %%dx,%%ax/n/t" /
        "movw %0,%%dx/n/t" /
        "movl %%eax,%1/n/t" /
        "movl %%edx,%2" /
        : /
        : "i" ((short) (0x8000+(dpl<<13)+(type<<8))), /
        "o" (*((char *) (gate_addr))), /
        "o" (*(4+(char *) (gate_addr))), /
        "d" ((char *) (addr)),"a" (0x00080000))
二.系统调用阶段
1.~/init/main.c
       static inline _syscall0(int,fork)
2. ~/include/unistd.h
#define _syscall0(type,name) /
type name(void) /            //定义了fork()
{ /
long __res; /
__asm__ volatile ("int $0x80" /
        : "=a" (__res) /
       : "" (__NR_##name)); /  // __NR_fork =2 在~/include/unistd.h中定义
if (__res >= 0) /
        return (type) __res; /
errno = -__res; /
return -1; /
}
3.fork() 调用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值