Distributions
› Ubuntu
› Fedora
› CentOS
中文资源站
› 网易开源镜像站
juzipeek
V2EX  ›  Linux

Linux clone 疑问

  •  
  •   juzipeek · Jun 4, 2019 · 4534 views
    This topic created in 2710 days ago, the information mentioned may be changed or developed.

    在看 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 replies  •  2019-06-05 17:05:08 +08:00
    dorentus
    dorentus
    Jun 4, 2019
    1
    说了不能啊。
    lechain
    lechain
    Jun 4, 2019 via Android
    2
    由于子进程和调用进程可能共享内存,因此子进程不可能与调用进程在同一堆栈中执行。=>共享内存会导致父子进程运行在相同的栈。
    想知道这个因果关系是如何得出来的
    不应该是 如果父子进程运行在相同的栈上,就无法共享内存吗……(虽然这个命题好像也是不对的
    zzjas98
    zzjas98
    Jun 5, 2019
    3
    每个 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
    zzjas98
    Jun 5, 2019
    4
    上面是一本正经的瞎猜,说错了请多包涵
    zzjas98
    zzjas98
    Jun 5, 2019
    5
    例子的第 4 行有个 typo,应该是“ with initial sp2 = 0xff ”
    owt5008137
    owt5008137
    Jun 5, 2019 via Android
    6
    人家说的明明是不能。
    juzipeek
    juzipeek
    OP
    Jun 5, 2019
    7
    补充说明,问题的语境是

    ```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。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1469 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 18:27 · PVG 02:27 · LAX 10:27 · JFK 13:27
    ♥ Do have faith in what you're doing.