Linux – Why clone() instead of fork()?

Why clone() instead of fork()?… here is a solution to the problem.

Why clone() instead of fork()?

I’m doing some work on pipelines and forks.
I have this strace output, but I’m not sure why clone is used instead of fork. Does this mean they are the same?

Trace output

enter codexecve("./forks", ["./forks"], [/* 55 vars */]) = 0
arch_prctl(ARCH_SET_FS, 0x7f2b0e498700) = 0
pipe([3, 4])                            = 0
clone(Process 7304 attached
child_stack=0, flags=CLONE_CHILD_CLEARTID| CLONE_CHILD_SETTID| SIGCHLD, child_tidptr=0x7f2b0e4989d0) = 7304
[pid  7303] execve("/usr/bin/wc", ["wc", "-l"], [/* 55 vars */] <unfinished ... >
[pid  7304] execve("/bin/ls", ["ls"], [/* 55 vars */] <unfinished ... >
[pid  7303] <... execve resumed> )      = 0
[pid  7304] <... execve resumed> )      = 0
[pid  7303] arch_prctl(ARCH_SET_FS, 0x7f558acde700) = 0
[pid  7304] arch_prctl(ARCH_SET_FS, 0x7f4bef4f67c0) = 0
[pid  7304] exit_group(0)               = ?
Process 7304 detached
--- SIGCHLD (Child exited) @ 0 (0) ---
21
exit_group(0) 

Solution

No, fork() and clone() are not the same. However, you can think of them as functions that serve as wrappers for clone() system calls, which means that they use the same clone() system call internally to create a new process. This explains what you observe in the output of strace.

Check this and this problem; They explained the difference better than I did.

Related Problems and Solutions