linux clone()返回-1作为child_pid

我有以下程序:

#define _GNU_SOURCE #include<sched.h> #include<stdio.h> #include<stdlib.h> #include<sys/wait.h> #include<unistd.h> static char child_stack[2048]; static int child_fn() { printf("Pid: %ld\n", (long) getpid()); return 0; } int main() { pid_t child_pid = clone(child_fn, child_stack+1024, CLONE_NEWPID | CLONE_NEWNS | SIGCHLD, NULL); printf("clone()= %ld\n", (long) child_pid); waitpid(child_pid, NULL, 0); return 0; } 

我得到以下输出

 # ./clone clone()= -1 

谁能帮我理解为什么这样?

注意:我正在使用ubuntu:latest dockerdocker镜像下运行它。 还要注意提示符是#而不是$ ,所以和运行这个进程的用户有什么关系,在这种情况下可能是root

正如在评论中提到的,我使用strerror(3)来找出为什么我得到错误。 错误是Operation not permitted 。 我search了一下,发现了解决办法 – 克隆:操作不允许

所以为了解决这个问题,我所要做的就是运行我的--privileged容器--privileged标志