clone(2) System Calls Manual clone(2) clone, __clone2, clone3 - (libc -lc) /* glibc */ #define _GNU_SOURCE #include int clone(typeof(int (void *_Nullable)) *fn, void *stack, int flags, void *_Nullable arg, ... /* pid_t *_Nullable parent_tid, void *_Nullable tls, pid_t *_Nullable child_tid */ ); /* ()clone VERSIONS. */ #include /* struct clone_args */ #include /* CLONE_* */ #include /* SYS_* */ #include long syscall(SYS_clone3, struct clone_args *cl_args, size_t size); : glibc clone3() syscall(2). ("") fork(2). fork(2) . . namespaces(7) . "" " ". CLONE_PARENT CLONE_THREAD . : o glibc clone() . . o clone3() . " " . ()clone clone() fn. ( fork(2) fork(2)). arg fn. fn(arg) . fn . exit(2) . stack . . clone(). ( HP PA) stack . clone() . clone() . clone3() clone3() clone() . (API) : . fork(2) clone3() . 0 PID . cl_args clone3() : struct clone_args { u64 flags; /* */ u64 pidfd; /* PID (int *) */ u64 child_tid; /* TID (pid_t *) */ u64 parent_tid; /* TID (pid_t *) */ u64 exit_signal; /* */ u64 stack; /* */ u64 stack_size; /* */ u64 tls; /* TLS */ u64 set_tid; /* pid_t ( 5.5) */ u64 set_tid_size; /* set_tid ( 5.5) */ u64 cgroup; /* (cgroup) ( 5.7) */ }; size clone3() . ( size clone_args). cl_args.stack cl_args.stack_size . CLONE_VM ( ) . NULL 0 ( ). cl_args . ()clone ()clone3 clone() clone3() clone_args . clone(). clone() clone_args clone3(): clone() clone3() cl_args flags & ~0xff flags parent_tid pidfd CLONE_PIDFD child_tid child_tid CLONE_CHILD_SETTID parent_tid parent_tid CLONE_PARENT_SETTID flags & 0xff exit_signal stack stack --- stack_size tls tls CLONE_SETTLS --- set_tid --- set_tid_size --- cgroup CLONE_INTO_CGROUP . flags (clone()) cl_args.exit_signal (clone3()). SIGCHLD __WALL __WCLONE wait(2). ( ) . set_tid PID PID . clone3() set_tid ( 5.5) PID PID . PID PID PID ( flags CLONE_NEWPID) set_tid PID set_tid_size 1. PID PID set_tid . PID PID PID PID . PID PID set_tid_size PID . PID PID: PID PID 0 31496 PID 1 42 2 7 PID : set_tid[0] = 7; set_tid[1] = 42; set_tid[2] = 31496; set_tid_size = 3; PID PID : set_tid[0] = 7; set_tid[1] = 42; set_tid_size = 2; PID PID PID . set_tid CAP_SYS_ADMIN ( 5.9) CAP_CHECKPOINT_RESTORE PID . PID 1 PID init ( PID 1) . PID PID 1. clone() clone3() . -- flags clone() cl_args.flags clone3() -- flags . flags OR . ( ) clone() clone3(). CLONE_CHILD_CLEARTID ( 2.5.49) () child_tid (clone()) cl_args.child_tid (clone3()) (wakeup) futex . set_tid_address(2). . CLONE_CHILD_SETTID ( 2.5.49) child_tid (clone()) cl_args.child_tid (clone3()) . . ( CLONE_VM ). CLONE_CLEAR_SIGHAND ( 5.5) . ( SIG_IGN) (SIG_DFL) . CLONE_SIGHAND . CLONE_DETACHED () ( 2.5) CLONE_DETACHED . CLONE_THREAD 2.6.0 . 2.6.2 CLONE_THREAD. clone(). CLONE_PIDFD . CLONE_FILES ( 2.0) CLONE_FILES . . ( F_SETFD fcntl(2)) . execve(2) ( ). CLONE_FILES . . ( open(2)). CLONE_FS ( 2.0) CLONE_FS . (umask). chroot(2) chdir(2) umask(2) . CLONE_FS . chroot(2) chdir(2) umask(2) . CLONE_INTO_CGROUP ( 5.7) (cgroup) 2 . CLONE_INTO_CGROUP 2 . ( CLONE_INTO_CGROUP 2.) CLONE_INTO_CGROUP cl_args.flags 2 cl_args.cgroup. ( cgroup v2 O_RDONLY O_PATH.) ( cgroups(7)) 2 . CLONE_INTO_CGROUP : o . . . o CLONE_INTO_CGROUP . ( cgroups(7) .) o ( ) . CLONE_IO ( 2.6.25) CLONE_IO / . ( fork(2)) / . / / ( / / ). / / . . / / . / (aio_read(3) ) CLONE_IO / . CONFIG_BLOCK . CLONE_NEWCGROUP ( 4.6) cgroup . ( fork(2)) cgroup . cgroup cgroup_namespaces(7). (CAP_SYS_ADMIN) CLONE_NEWCGROUP. CLONE_NEWIPC ( 2.6.19) CLONE_NEWIPC IPC . ( fork(2)) IPC . IPC ipc_namespaces(7). (CAP_SYS_ADMIN) CLONE_NEWIPC. CLONE_SYSVSEM. CLONE_NEWNET ( 2.6.24) ( 2.6.29.) CLONE_NEWNET . ( fork(2)) . network_namespaces(7). (CAP_SYS_ADMIN) CLONE_NEWNET. CLONE_NEWNS ( 2.4.19) CLONE_NEWNS . CLONE_NEWNS . namespaces(7) mount_namespaces(7). (CAP_SYS_ADMIN) CLONE_NEWNS. CLONE_NEWNS CLONE_FS . CLONE_NEWPID ( 2.6.24) CLONE_NEWPID PID . ( fork(2)) PID . PID namespaces(7) pid_namespaces(7). (CAP_SYS_ADMIN) CLONE_NEWPID. CLONE_THREAD. CLONE_NEWUSER ( clone() 2.6.23 clone() 3.5 3.8.) CLONE_NEWUSER . ( fork(2)) . namespaces(7) user_namespaces(7). 3.8 CLONE_NEWUSER : CAP_SYS_ADMIN CAP_SETUID CAP_SETGID. 3.8 . CLONE_THREAD CLONE_PARENT. CLONE_NEWUSER CLONE_FS. CLONE_NEWUTS ( 2.6.19) CLONE_NEWUTS UTS UTS . ( fork(2)) UTS . UTS uts_namespaces(7). (CAP_SYS_ADMIN) CLONE_NEWUTS. CLONE_PARENT ( 2.3.12) CLONE_PARENT ( getppid(2)) . CLONE_PARENT ( fork(2)) . getppid(2) CLONE_PARENT . CLONE_PARENT (PID 1 PID ) PID . PID . CLONE_PARENT_SETTID ( 2.5.49) parent_tid ( clone()) cl_args.parent_tid ( clone3()) . ( 2.5.32-2.5.48 CLONE_SETTID .) . CLONE_PID ( 2.0 2.5.15) CLONE_PID . . 2.3.21 (PID 0). 2.5.16. . CLONE_PIDFD. CLONE_PIDFD ( 5.2) PID . (close-on-exec) . PID pidfd_open(2). o clone3() PID cl_args.pidfd. o clone() PID parent_tid. parent_tid PID CLONE_PIDFD CLONE_PARENT_SETTID clone(). CLONE_PIDFD CLONE_THREAD PID CLONE_THREAD. 6.9. CLONE_DETACHED CLONE_PIDFD clone() . CLONE_DETACHED clone3(). CLONE_DETACHED PID . CLONE_PTRACE ( 2.2) CLONE_PTRACE ( ptrace(2)). CLONE_SETTLS ( 2.5.32) TLS ( ) tls. tls . x86 tls struct user_desc * ( set_thread_area(2)). x86-64 %fs ( ARCH_SET_FS arch_prctl(2)). TLS . . CLONE_SIGHAND ( 2.0) CLONE_SIGHAND . sigaction(2) . . sigprocmask(2) . CLONE_SIGHAND . sigaction(2) . 2.6.0 flags CLONE_VM CLONE_SIGHAND. CLONE_STOPPED ( 2.6.0) CLONE_STOPPED ( SIGSTOP) SIGCONT . 2.6.25 2.6.38. . 4.6 CLONE_NEWCGROUP. CLONE_SYSVSEM ( 2.5.10) CLONE_SYSVSEM System V ( semadj) ( semop(2)). semadj ( unshare(2)). semadj . CLONE_THREAD ( 2.4.0) CLONE_THREAD . CLONE_THREAD "" . 2.4 POSIX (PID) . (TGID). 2.4 getpid(2) TGID . (TID) ( ). TID TID gettid(2). CLONE_THREAD TGID TID . . CLONE_THREAD ( CLONE_PARENT) getppid(2) . CLONE_THREAD SIGCHLD ( ) wait(2). ( ). SIGCHLD ( ). execve(2) . fork(2) (wait(2)) . 2.5.35 flags CLONE_SIGHAND CLONE_THREAD ( 2.6.0 CLONE_SIGHAND CLONE_VM). : ( ) . sigprocmask(2). . ( TGID) . kill(2) sigqueue(3). ( ) . tgkill(2) pthread_sigqueue(3) ( SIGSEGV SIGFPE). sigpending(2) . . sigwaitinfo(2) . CLONE_UNTRACED ( 2.5.46) CLONE_UNTRACED CLONE_PTRACE . CLONE_VFORK ( 2.2) CLONE_VFORK execve(2) _exit(2) ( vfork(2)). CLONE_VFORK . CLONE_VM ( 2.0) CLONE_VM . . mmap(2) munmap(2) . CLONE_VM . / fork(2). CLONE_VM CLONE_VFORK sigaltstack(2) . (thread ID) . -1 errno . E2BIG ( clone3() ) clone3() : (struct) . E2BIG ( clone3() ) . EACCES ( clone3() ) CLONE_INTO_CGROUP cl_args.flags ( cgroups(7)) 2 (cgroup) cl_args.cgroup . EAGAIN fork(2). EBUSY ( clone3() ) CLONE_INTO_CGROUP cl_args.flags cl_args.cgroup (cgroup) 2 (domain controller) . EEXIST ( clone3() ) ( ) (PIDs) set_tid PID . EINVAL CLONE_SIGHAND CLONE_CLEAR_SIGHAND flags. EINVAL CLONE_SIGHAND flags CLONE_VM . ( 2.6.0.) EINVAL CLONE_THREAD flags CLONE_SIGHAND . ( 2.5.35.) EINVAL CLONE_THREAD flags unshare(2) CLONE_NEWPID setns(2) PID. EINVAL CLONE_FS CLONE_NEWNS flags. EINVAL ( 3.9) CLONE_NEWUSER CLONE_FS flags. EINVAL CLONE_NEWIPC CLONE_SYSVSEM flags. EINVAL CLONE_NEWPID ( ) CLONE_THREAD CLONE_PARENT flags. EINVAL CLONE_NEWUSER CLONE_THREAD flags. EINVAL ( 2.6.32) CLONE_PARENT init. EINVAL clone() glibc fn stack NULL. EINVAL CLONE_NEWIPC flags CONFIG_SYSVIPC CONFIG_IPC_NS. EINVAL CLONE_NEWNET flags CONFIG_NET_NS. EINVAL CLONE_NEWPID flags CONFIG_PID_NS. EINVAL CLONE_NEWUSER flags CONFIG_USER_NS. EINVAL CLONE_NEWUTS flags CONFIG_UTS_NS. EINVAL stack . aarch64 stack 16. EINVAL ( clone3() ) CLONE_DETACHED flags. EINVAL ( clone() ) CLONE_PIDFD CLONE_DETACHED flags. EINVAL ( 6.9) CLONE_PIDFD CLONE_THREAD flags. EINVAL ( clone() ) CLONE_PIDFD CLONE_PARENT_SETTID flags. EINVAL ( clone3() ) set_tid_size PID . EINVAL ( clone3() ) (PIDs) set_tid . EINVAL ( clone3() ) CLONE_THREAD CLONE_PARENT flags exit_signal. EINVAL ( AArch64 4.6 ) stack 128 . ENOMEM . ENOSPC ( 3.7) CLONE_NEWPID flags PID pid_namespaces(7). ENOSPC ( 4.9 EUSERS) CLONE_NEWUSER flags . user_namespaces(7). 3.11 4.8 EUSERS. ENOSPC ( 4.9) flags /proc/sys/user. namespaces(7). EOPNOTSUPP ( clone3() ) CLONE_INTO_CGROUP cl_args.flags cl_args.cgroup (cgroup) 2 domain invalid. EPERM CLONE_NEWCGROUP CLONE_NEWIPC CLONE_NEWNET CLONE_NEWNS CLONE_NEWPID CLONE_NEWUTS ( CAP_SYS_ADMIN). EPERM CLONE_PID 0. ( 2.5.15 ). EPERM CLONE_NEWUSER flags (mapping) ( user_namespaces(7)). EPERM ( 3.9) CLONE_NEWUSER flags chroot ( ). EPERM ( clone3() ) set_tid_size CAP_SYS_ADMIN PID . ERESTARTNOINTR ( 2.6.17) . ( trace.) EUSERS ( 3.11 4.8) CLONE_NEWUSER flags . ENOSPC . clone() glibc stack ( ) clone(). clone() . i386 clone() vsyscall int $0x80. C clone() fork(2) . fn arg clone(). glibc clone() NULL stack ( clone3() cl_args.stack NULL). . ( Copy-on-write ). CLONE_VM. ( CLONE_VM ). . x86-64 ( sh tile alpha) : long clone(unsigned long flags, void *stack, int *parent_tid, int *child_tid, unsigned long tls); x86-32 ( score ARM ARM 64 PA-RISC arc Power PC xtensa MIPS) : long clone(unsigned long flags, void *stack, int *parent_tid, unsigned long tls, int *child_tid); cris s390 : long clone(void *stack, unsigned long flags, int *parent_tid, int *child_tid, unsigned long tls); microblaze : long clone(unsigned long flags, void *stack, int stack_size, /* */ int *parent_tid, int *child_tid, unsigned long tls); blackfin m68k sparc blackfin m68k sparc . ( glibc). . clone3() 5.3. 2.4 2.4.x CLONE_THREAD . 2.4.7 2.4.18 CLONE_THREAD CLONE_PARENT ( 2.6.0 ). 2.4 clone() parent_tid tls child_tid. ia64 ia64 : int __clone2(typeof(int (void *)) *fn, void *stack_base, size_t stack_size, int flags, void *arg, ... /* pid_t *parent_tid, struct user_desc *tls, pid_t *child_tid */ ); glibc ( clone() microblaze): long clone2(unsigned long flags, void *stack_base, int stack_size, /* */ int *parent_tid, int *child_tid, unsigned long tls); __clone2() clone() stack_base stack_size stack_base. : . kcmp(2) System V . pthread_atfork(3) clone. GNU C 2.3.4 2.24 ( ) getpid(2) (PIDs). glibc clone() . () clone() getpid(2) (<<>>) . ( CLONE_THREAD getpid(2) clone() . flags CLONE_VM.) : #include pid_t mypid; mypid = syscall(SYS_getpid); getpid(2) 2.25 glibc. clone() UTS . UTS . UTS . setns(2). mmap(2) malloc(3) : o mmap(2) . ( PROT_NONE) mprotect(2). o MAP_STACK . (no-op) . #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include static int /* */ childFunc(void *arg) { struct utsname uts; /* UTS . */ if (sethostname(arg, strlen(arg)) == -1) err(EXIT_FAILURE, "sethostname"); /* . */ if (uname(&uts) == -1) err(EXIT_FAILURE, "uname"); printf("uts.nodename in child: %s\n", uts.nodename); /* . -- . */ sleep(200); return 0; /* */ } #define STACK_SIZE (1024 * 1024) /* */ int main(int argc, char *argv[]) { char *stack; /* */ char *stackTop; /* */ pid_t pid; struct utsname uts; if (argc < 2) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(EXIT_SUCCESS); } /* . */ stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0); if (stack == MAP_FAILED) err(EXIT_FAILURE, "mmap"); stackTop = stack + STACK_SIZE; /* */ /* UTS childFunc(). */ pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]); if (pid == -1) err(EXIT_FAILURE, "clone"); if (munmap(stack, STACK_SIZE)) err(EXIT_FAILURE, "munmap"); printf("clone() returned %jd\n", (intmax_t) pid); /* */ sleep(1); /* */ /* UTS . UTS . */ if (uname(&uts) == -1) err(EXIT_FAILURE, "uname"); printf("uts.nodename in parent: %s\n", uts.nodename); if (waitpid(pid, NULL, 0) == -1) /* */ err(EXIT_FAILURE, "waitpid"); printf("child has terminated\n"); exit(EXIT_SUCCESS); } fork(2), futex(2), getpid(2), gettid(2), kcmp(2), mmap(2), pidfd_open(2), set_thread_area(2), set_tid_address(2), setns(2), tkill(2), unshare(2), wait(2), capabilities(7), namespaces(7), pthreads(7) 3 . . : . 6.18 8 2026 clone(2)