Linux clone 疑问 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
juzipeek
V2EX    Linux

Linux clone 疑问

  • &nbp;
  •   juzipeek 2019-06-04 16:20:49 +08:00 3737 次点击
    这是一个创建于 2376 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在看 man page ( man clone )时有这样一句:

    Since the child and calling process may share memory, it is not possible for the child process to execute in the same stack as the calling process. 

    中文翻译是:由于子进程和调用进程可能共享内存,因此子进程不可能与调用进程在同一堆栈中执行。

    为什么会由于共享内存( share memory )会导致父子进程运行在相同的栈上?

    7 条回复    2019-06-05 17:05:08 +08:00
    dorentus
        1
    dorentus  
       2019-06-04 16:27:41 +08:00
    说了不能啊。
    lechain
        2
    lechain  
       2019-06-04 17:55:45 +08:00 via Android
    由于子进程和调用进程可能共享内存,因此子进程不可能与调用进程在同一堆栈中执行。=>共享内存会导致父子进程运行在相同的栈。
    想知道这个因果关系是如何得出来的
    不应该是 如果父子进程运行在相同的栈上,就无法共享内存吗……(虽然这个命题好像也是不对的
    zzjas98
        3
    zzjas98  
       2019-06-05 01:01:17 +08:00
    每个 process 有自己的 stack pointer ( SP )。如果两个 process 共享 memory space 还共享 stack 的话,互相有可能会 overwrite 掉对方的 stack。

    e.g.
    Process 1 has initial sp1 = 0xff
    Process 1 advances sp1 to 0xf0
    Process 1 calls clone
    Process 2 is created with initial sp = 0xff
    Context switch to Process 2
    Process 2 advances sp2 to 0xa0
    Context switch to Process 1
    Process 1 continues executing with sp1 = 0xf0
    --> Content from 0xff to 0xf0 are overwritten by Process 2

    所以:由于子进程和调用进程可能共享内存 => 因此子进程不可能与调用进程在同一堆栈中执行


    这里并没有后半个因果关系: “由于子进程和调用进程可能共享内存,因此子进程不可能与调用进程在同一堆栈中执行。=> 共享内存会导致父子进程运行在相同的栈”
    zzjas98
        4
    zzjas98  
       2019-06-05 01:01:34 +08:00
    上面是一本正经的瞎猜,说错了请多包涵
    zzjas98
        5
    zzjas98  
       2019-06-05 01:02:56 +08:00
    例子的第 4 行有个 typo,应该是“ with initial sp2 = 0xff ”
    owt5008137
        6
    owt5008137  
       2019-06-05 09:16:06 +08:00 via Android
    div class="reply_content">人家说的明明是不能。
    juzipeek
        7
    juzipeek  
    OP
       2019-06-05 17:05:08 +08:00
    补充说明,问题的语境是

    ```c
    #define _GNU_SOURCE
    #include <sched.h>

    int clone(int (*fn)(void *), void *child_stack,
    int flags, void *arg, ...
    /* pid_t *ptid, void *newtls, pid_t *ctid */ );
    ```

    ... ...

    The child_stack argument specifies the location of the stack used by the child process. Since the child and calling process may share memory, it is not possible for the child process to execute in the same stack as the calling process. The calling process must therefore set up memory space for the child stack and pass a pointer to this space to clone(). Stacks grow downward on all processors that run Linux (except the HP PA processors), so child_stack usually points to the topmost address of the memory space set up for the child stack.

    ... ...

    现在觉的“ Since the child and calling process may share memory, it is not possible for the child process to execute in the same stack as the calling process. ”这句话没有营养,关键在后面,调用 clone 要指定 child_stack。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5021 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 08:48 PVG 16:48 LAX 00:48 JFK 03:48
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86