allkillers 发表于 2016-05-24 17:16

clone调用的小例子

如下例子,
期望结果:先打印child,再打印parent;
但实际结果:只打印出child。

如果在如下1111111111和222222222之间加上sleep几秒,就能打印出parent了。

这是为什么?没道理啊。
int child_fun(void*p)
{
    printf("child\n");       //1111111111111111
    return 0;                   //222222222222222
}

static int stack={0};

int main(void)
{
    clone(child_fun,stack)+1023,CLONE_VM | CLONE_THREAD | CLONE_SIGHAND ,NULL);
    sleep(1);
    printf("parent\n");
    return 0;
}

nswcfd 发表于 2016-05-27 10:13

strace跟踪一下看看

nswcfd 发表于 2016-05-27 10:15

这是在模拟thread的行为吧?

nswcfd 发表于 2016-05-27 10:21

看起来exit_group让所有线程都退出了

allkillers 发表于 2016-05-27 11:41

你说的没错,我把程序加上如下红色部分,执行之后并没有因为死循环而一直运行,也退出了。

int child_fun(void*p)
{
    printf("child\n");         //1111111111111111
    return 0;                   //222222222222222
}

static int stack={0};

int main(void)
{
    clone(child_fun,stack)+1023,CLONE_VM | CLONE_THREAD | CLONE_SIGHAND ,NULL);
    while(1)
    {
      sleep(1);
    }
    printf("parent\n");
    return 0;
}

回复 4# nswcfd


   

nswcfd 发表于 2016-05-27 12:50

pthread的线程退出使用的是syscall _exit

allkillers 发表于 2016-05-27 15:49

有几个问题:
1. 我这边strace怎么看不到调用exit时的syscall?
2. _exit的解释是这样的“Inglibc up to version 2.3, the _exit() wrapper function invoked the kernel system call of the same name.Since glibc 2.3, the wrapper function invokes exit_group(2), in order to terminate all of the threads in a process.”
3. 我调用pthread_exit怎么没看到syscall?如下代码及strace结果,我确实能看到pthread_create是clone系统调用,却没看见child的pthread_exit的系统调用。

child_fun(void*p)
{
    printf("child\n");
    pthread_exit(&i);
}

int main(void)
{
    pthread_t tid;
    pthread_create(&tid,NULL,child_fun,NULL);
    while(1)
    {
    }
    return 0;
}

strace结果:
clone(child_stack=0x7f786b0f9fb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f786b0fa9d0, tls=0x7f786b0fa700, child_tidptr=0x7f786b0fa9d0) = 21499
child

strace回复 6# nswcfd


   

nswcfd 发表于 2016-05-30 09:46

stdlib的exit通常映射为exit_group

[~]$ cat e.c
#include <unistd.h>
#include <stdlib.h>

int main()
{
        exit(1);
}

[~]$ strace ./e
...
exit_group(1)                           = ?

nswcfd 发表于 2016-05-30 09:49

在thread里调用exit的情况,也是映射为exit_group

[~]$ cat e.c
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

void *f(void *arg)
{
        exit(1);
}

int main()
{
        pthread_t th;
        pthread_create(&th, NULL, f, NULL);
        pause();
}

[~]$ strace -f ./e
...
clone(Process 14866 attached (waiting for parent)
Process 14866 resumed (parent 14865 ready)
<unfinished ...>
set_robust_list(0x7f07bca4a9e0, 0x18 <unfinished ...>
<... clone resumed> child_stack=0x7f07bca49ff0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f07bca4a9d0, tls=0x7f07bca4a700, child_tidptr=0x7f07bca4a9d0) = 14866
<... set_robust_list resumed> ) = 0
pause( <unfinished ...>
exit_group(1)               = ?
Process 14866 detached

nswcfd 发表于 2016-05-30 09:52

thread调用pthread_exit的情况,注意这时候换成了_exit(0)

[~]$ cat e.c
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

void *f(void *arg)
{
        pthread_exit(NULL);
}

int main()
{
        pthread_t th;
        pthread_create(&th, NULL, f, NULL);
        pause();
}

[~]$ strace -f ./e
...
clone(Process 14926 attached (waiting for parent)
Process 14926 resumed (parent 14925 ready)
<unfinished ...>
set_robust_list(0x7f234bf599e0, 0x18 <unfinished ...>
<... clone resumed> child_stack=0x7f234bf58ff0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f234bf599d0, tls=0x7f234bf59700, child_tidptr=0x7f234bf599d0) = 14926
<... set_robust_list resumed> ) = 0
pause( <unfinished ...>
...
_exit(0)                  = ?
Process 14926 detached
页: [1]
查看完整版本: clone调用的小例子