-
创建test-fork.c 文件:
#include <unistd.h>
int main (void)
{
fork();
return 0;
}
使用静态链接编译:gcc -O0 -static -Wall test-fork.c -o test-fork
拆机:objdump -D -S test-fork > test-fork.dis
-
打开test-fork.dis文件并搜索fork:
fork();
80481f4: e8 63 55 00 00 call 804d75c <__libc_fork>
return 0;
80481f9: b8 00 00 00 00 mov $0x0,%eax
}
80481fe: c9 leave
80481ff: c3 ret
-
然后搜索__libc_fork:
0804d75c <__libc_fork>:
804d75c: 55 push %ebp
804d75d: b8 00 00 00 00 mov $0x0,%eax
804d762: 89 e5 mov %esp,%ebp
804d764: 53 push %ebx
804d765: 83 ec 04 sub $0x4,%esp
804d768: 85 c0 test %eax,%eax
804d76a: 74 12 je 804d77e <__libc_fork+0x22>
804d76c: c7 04 24 80 e0 0a 08 movl $0x80ae080,(%esp)
804d773: e8 88 28 fb f7 call 0 <_init-0x80480d4>
804d778: 83 c4 04 add $0x4,%esp
804d77b: 5b pop %ebx
804d77c: 5d pop %ebp
804d77d: c3 ret
804d77e: b8 02 00 00 00 mov $0x2,%eax
804d783: cd 80 int $0x80
804d785: 3d 00 f0 ff ff cmp $0xfffff000,%eax
804d78a: 89 c3 mov %eax,%ebx
804d78c: 77 08 ja 804d796 <__libc_fork+0x3a>
804d78e: 89 d8 mov %ebx,%eax
804d790: 83 c4 04 add $0x4,%esp
804d793: 5b pop %ebx
804d794: 5d pop %ebp
804d795: c3 ret
请注意,在此特定硬件/内核上,fork 与 系统调用编号 2
相关联
下载 Linux 内核的副本:wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.23.tar.bz2
打开linux-2.6.23/arch/x86/kernel/syscall_table_32.S文件
-
注意系统调用号 2 关联到
sys_fork:
.long sys\_fork /* 2 */
打开linux-2.6.23/arch/x86/kernel/process.c文件
-
搜索sys_fork:
asmlinkage int sys_fork(struct pt_regs regs)
{
return do_fork(SIGCHLD, regs.esp, ®s, 0, NULL, NULL);
}
请注意,do_fork() 仅使用 SIGCHLD 参数调用
打开linux-2.6.23/kernel/fork.c 文件。这里是定义do_fork() 的地方!
-
do_fork() 然后调用copy_process():
/*
* Ok, this is the main fork-routine.
*
* It copies the process, and if successful kick-starts
* it and waits for it to finish using the VM if required.
*/
long do_fork(unsigned long clone_flags,
unsigned long stack_start,
struct pt_regs *regs,
unsigned long stack_size,
int __user *parent_tidptr,
int __user *child_tidptr)
{
struct task_struct *p;
int trace = 0;
struct pid *pid = alloc_pid();
long nr;
if (!pid)
return -EAGAIN;
nr = pid->nr;
if (unlikely(current->ptrace)) {
trace = fork_traceflag (clone_flags);
if (trace)
clone_flags |= CLONE_PTRACE;
}
p = copy_process(clone_flags, stack_start, regs, stack_size, \
parent_tidptr, child_tidptr, pid);
/*
* Do this prior waking up the new thread - the thread
* pointer might get invalid after that point,
* if the thread exits quickly.
*/
if (!IS_ERR(p)) {
struct completion vfork;
if (clone_flags & CLONE_VFORK) {
p->vfork_done = &vfork;
init_completion(&vfork);
}
if ((p->ptrace & PT_PTRACED) || \
(clone_flags & CLONE_STOPPED)) {
/*
* We'll start up with an immediate SIGSTOP.
*/
sigaddset(&p->pending.signal, SIGSTOP);
set_tsk_thread_flag(p, TIF_SIGPENDING);
}
if (!(clone_flags & CLONE_STOPPED))
wake_up_new_task(p, clone_flags);
else
p->state = TASK_STOPPED;
if (unlikely (trace)) {
current->ptrace_message = nr;
ptrace_notify ((trace << 8) | SIGTRAP);
}
if (clone_flags & CLONE_VFORK) {
freezer_do_not_count();
wait_for_completion(&vfork);
freezer_count();
if (unlikely (current->ptrace & \
PT_TRACE_VFORK_DONE)) {
current->ptrace_message = nr;
ptrace_notify \
((PTRACE_EVENT_VFORK_DONE << 8) | \
SIGTRAP);
}
}
} else {
free_pid(pid);
nr = PTR_ERR(p);
}
return nr;
}
-
fork 中的大部分工作由do_fork() 处理,
在kernel/fork.c 中定义。 do_fork() 执行的操作:
- 它通过调用
alloc_pid()为孩子分配一个新的PID
- 它检查父级的
ptrace 字段(即current->ptrace)
-
它调用copy_process(),它设置进程描述符和子进程执行所需的任何其他内核数据结构
- 它通过清除或初始化
task_struct 的各个字段来区分子项和父项
-
它调用copy_flags() 来更新task_struct 的flags 字段
-
PF_SUPERPRIV(表示任务是否使用超级用户权限)和PF_NOFREEZE 标志被清除
- 设置了
PF_FORKNOEXEC 标志(表示任务是否未调用`exec())
- 它调用 `init_sigpending() 来清除挂起的信号
- 根据传递给
do_fork(),copy_process() 的参数,然后复制或共享资源
- 打开文件
- 文件系统信息
- 信号处理程序
- 地址空间
- 它调用
sched_fork(),它将剩余的时间片在父母和孩子之间分割
- 最后,它返回一个指向新子节点的指针
然后,do_fork() 添加一个待处理的SIGSTOP 信号,以防设置CLONE_STOPPED 标志或必须跟踪子进程(即PT_PTRACED 标志在p->ptrace 中设置)
-
如果没有设置CLONE_STOPPED 标志,它会调用wake_up_new_task() 函数,该函数执行以下操作:
- 调整父母和孩子的调度参数
- 如果子进程将与父进程在同一个 CPU 上运行,并且父进程和子进程不共享同一组页表(即
CLONE_VM 标志已清除),则它会通过插入它来强制子进程在父进程之前运行在父级之前进入父级的运行队列。如果子进程刷新其地址空间并在分叉后立即执行新程序,这个简单的步骤会产生更好的性能。如果我们让父级先运行,Copy On Write 机制会导致一系列不必要的页面重复。
- 否则,如果子进程不会在与父进程相同的 CPU 上运行,或者父进程和子进程共享同一组页表(即
CLONE_VM 标志集),它将孩子插入到父运行队列的最后一个位置
- 否则,如果设置了
CLONE_STOPPED 标志,它会将孩子置于TASK_STOPPED 状态
如果正在跟踪父进程,它将子进程的 PID 存储在
current 的 ptrace_message 字段并调用
ptrace_notify(),它实质上会停止当前进程并向其父进程发送SIGCHLD 信号。孩子的“祖父母”是跟踪父母的调试器; SIGCHLD 信号通知调试器当前已经派生了一个子节点,可以通过查看 current->ptrace_message 字段来检索其 PID。
如果指定了CLONE_VFORK 标志,它会将父进程插入等待队列并暂停它,直到子进程释放其内存地址空间(即,直到子进程终止或执行新程序)
- 它通过返回孩子的 PID 来终止。