gpt4 book ai didi

C 系统编程 clone() 标志位值和操作

转载 作者:行者123 更新时间:2023-11-30 19:09:50 27 4
gpt4 key购买 nike

我对 C 系统编程非常陌生,我查看了clone() 的手册页,它有一堆标志。它还为您提供了一个示例:

pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]);

第一个问题,我浏览了clone()的手册页,它没有给你任何标志的数值,但它确实提到“标志也可以与零个或多个以下常数”。在哪里可以找到这些标志的数值。

其次,这句话背后的逻辑是什么

CLONE_NEWUTS | SIGCHLD

这里到底发生了什么?我知道按位或,但它对这些标志有什么作用以及对这两个标志进行或运算的结果是什么?

最佳答案

第一个问题:

查看系统的“sched.h”中是否有 CLONE_NEWUTS(可能是 0x04000000),以及“signal.h”中是否有 SIGCHLD(可能是 20)。如果您查看“sched.h”,您可能会发现信号掩码 (CSIGNAL) 设置为 0x000000FF,这引出了问题 2...

第二个问题:

来自 Linux 手册页 (man7.org):

If CLONE_NEWUTS is set, then create the process in a new UTS namespace, whose identifiers are initialized by duplicating the identifiers from the UTS namespace of the calling process. If this flag is not set, then (as with fork(2)) the process is created in the same UTS namespace as the calling process. This flag is intended for the implementation of containers.

还有

The low byte of flags contains the number of the termination signal sent to the parent when the child dies. If this signal is specified as anything other than SIGCHLD, then the parent process must specify the __WALL or __WCLONE options when waiting for the child with wait(2). If no signal is specified, then the parent process is not signaled when the child terminates.

我建议这样做 - 在新的 UTS 命名空间中创建克隆进程,并在终止后用 SIGCHLD 向父进程发出信号。

关于C 系统编程 clone() 标志位值和操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42406628/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com