当前位置: 技术问答>linux安装问题,高手赐教!有难度!
iis7站长之家
问个有难度的问题,2.6内核fork调用的怎么是clone
来源: 互联网 发布时间:2016-08-14
本文导语: 本帖最后由 HYPERMUSIC 于 2010-03-26 17:54:32 编辑 程序里if(fork() == 0){} 但是跟踪看到的是创建子进程调用的是clone clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xb7f3a708) 好像就这几个参数,那最后...
但是跟踪看到的是创建子进程调用的是clone
clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xb7f3a708)
好像就这几个参数,那最后都调用do_fork的话,这样以后fork的子进程不是要跟父进程共享用户空间里的东西了吗?那copy_on_write又是怎么体现出来的呢?
问题比较菜啊,但是今天看到这个了问题就想弄明白它
|
知道你的问题了,我也很郁闷
最后还是解决了,以前我说的不对,加了 CLONE_VM 就不是COW 了
glibc中fork pthread_create 都是调clone ,至于为什么要这样就不知道了,就算不用sys_fork,也不能轻易删除系统调用,历史原因吧
man 2 clone
If CLONE_VM is not set, the child process runs in a separate copy of the memory space of the calling process at the time of clone(). Memory writes or file mappings/unmappings performed by one of the processes do not affect the
other, as with fork(2).
这样的话
clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xb7f3a708)
也就足够满足sys_fork的功能了
写时拷贝的实现在fork.c 中的
static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
{
struct mm_struct * mm, *oldmm;
int retval;
tsk->min_flt = tsk->maj_flt = 0;
tsk->nvcsw = tsk->nivcsw = 0;
tsk->mm = NULL;
tsk->active_mm = NULL;
/*
* Are we cloning a kernel thread?
*
* We need to steal a active VM for that..
*/
oldmm = current->mm;
if (!oldmm)
return 0;
if (clone_flags & CLONE_VM) {
atomic_inc(&oldmm->mm_users);
mm = oldmm;
goto good_mm;
}
retval = -ENOMEM;
mm = dup_mm(tsk);
if (!mm)
goto fail_nomem;
good_mm:
/* Initializing for Swap token stuff */
mm->token_priority = 0;
mm->last_interval = 0;
tsk->mm = mm;
tsk->active_mm = mm;
return 0;
fail_nomem:
return retval;
}
dump_mm 会把子进程的用户空间页表设置为只读,这样子进程需要写的时候会产生异常,由
do_page_fault 来分配新的物理页,既是COW
最后还是解决了,以前我说的不对,加了 CLONE_VM 就不是COW 了
glibc中fork pthread_create 都是调clone ,至于为什么要这样就不知道了,就算不用sys_fork,也不能轻易删除系统调用,历史原因吧
man 2 clone
If CLONE_VM is not set, the child process runs in a separate copy of the memory space of the calling process at the time of clone(). Memory writes or file mappings/unmappings performed by one of the processes do not affect the
other, as with fork(2).
这样的话
clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xb7f3a708)
也就足够满足sys_fork的功能了
写时拷贝的实现在fork.c 中的
static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
{
struct mm_struct * mm, *oldmm;
int retval;
tsk->min_flt = tsk->maj_flt = 0;
tsk->nvcsw = tsk->nivcsw = 0;
tsk->mm = NULL;
tsk->active_mm = NULL;
/*
* Are we cloning a kernel thread?
*
* We need to steal a active VM for that..
*/
oldmm = current->mm;
if (!oldmm)
return 0;
if (clone_flags & CLONE_VM) {
atomic_inc(&oldmm->mm_users);
mm = oldmm;
goto good_mm;
}
retval = -ENOMEM;
mm = dup_mm(tsk);
if (!mm)
goto fail_nomem;
good_mm:
/* Initializing for Swap token stuff */
mm->token_priority = 0;
mm->last_interval = 0;
tsk->mm = mm;
tsk->active_mm = mm;
return 0;
fail_nomem:
return retval;
}
dump_mm 会把子进程的用户空间页表设置为只读,这样子进程需要写的时候会产生异常,由
do_page_fault 来分配新的物理页,既是COW