文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>执行fork函数的曲折过程

执行fork函数的曲折过程

时间:2006-11-15  来源:qfox

执行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() 调用
相关阅读 更多 +
排行榜 更多 +
轻松扫描王

轻松扫描王

游戏工具 下载
时光行者

时光行者

休闲益智 下载
合并与服务

合并与服务

休闲益智 下载