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

标签 c linux

我对 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/

相关文章:

从 Visual Basic 6 调用 C DLL : Double data type not working

c - 如何测量 ARM Cortex-A8 处理器中的程序执行时间?

c - 接收所有 IPv6 数据包

c - 在 C 中使用指针时防止内存覆盖

c - 使用 opendir() 获得的目录流中的条目顺序

java - 无法在 Android Studio 中清理项目

linux - 允许特定的 linux 用户绑定(bind)到端口 443

android - 添加到 JNI 固定数组引用表失败(1024 个条目)

c - 构建 Linux cp 命令的版本

linux - bash命令检查目录是否可执行