Process vs Thread in Linux
From the book Linux Kernel Development by Robert Love 3rd edition it is mentioned that both a process and a thread are no different from each other. Following is the what the book mentions:
Threads are created the same as normal tasks, with the exception that the clone() system call is passed flags corresponding to the specific resources to be shared:
clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);
In contrast, a normal fork() can be implemented as:
clone(SIGCHLD, 0);
However, I have written a dummy code under "process vs thread" directory, creating a process and a thread (using pthread) to verify this and ran strace on it. You can get the code here:
Following is the clone system call while creating a pthread:
clone3({flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, child_tid=0x7f4e993b6910, parent_tid=0x7f4e993b6910, exit_signal=0, stack=0x7f4e98bb6000, stack_size=0x7fff00, tls=0x7f4e993b6640} => {parent_tid=[989576]}, 88) = 989576
Following is the clone system call while creating a process:
clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7fe8012d6a10) = 989755
Following points should be kept in mind for this discussion:
It is seen that the clone() calls set_tid_address() internally, which sets pointer to the thread ID. Hence, it can be clearly understood that a process and a thread are same in Linux's point of view.
Following is the list of flags that can be used with these set of calls.