Process vs Thread in Linux

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:

https://github.com/surendraece08/linux_internals.git

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:

  • clone() is the system call for the wrapper function clone() i.e., name of the system call and the wrapper function is same.
  • On the other hand, clone() and clone3() are equivalent to each other. clone3() is the updated version of clone(), where the way the flags passed as arguments is updated using a structure.

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.

List of flags that can be used with fork(), pthread_create(), clone() or clone3() in Linux



To view or add a comment, sign in

More articles by Surendranath P

  • Process Management in Linux - Part 2

    Process state: The 'state' member of the task_struct structure describes the current state of the process, which falls…

  • Process Management in Linux - Part 1

    Process: A process is a program midst of execution. Each process in Linux is identified using a unique process id (pid).

  • Memory management in Linux - Summary

    If you've missed the other parts, following are the links. Part 1 discusses basic hardware and address binding.

  • Memory management in Linux - Part 7

    Memory-mapped files: Basic memory mapping mechanism: Just like programs from the disk mapped to the pages of main…

  • Memory management in Linux - Part 6

    Page replacement: The amount of programs that we can run by utilizing the memory efficiently is called degree of…

  • Memory management in Linux - Part 5

    There are different mechanisms to improve the efficient usage of memory and to run processes as smooth as possible and…

    4 Comments
  • Memory management in Linux - Part 4

    Paging: Similar to segmentation, paging can be achieved with two approaches - software based paging and hardware based…

  • Memory management in Linux - Part 3

    Solutions for external fragmentation: Solution 1 - Compaction - It means to shuffle the memory contents so as to place…

  • Memory management in Linux - Part 2

    Logical vs Physical address space: An address generated by the CPU is commonly referred to as a logical address…

  • Memory management in Linux - Part 1

    Basic hardware: Main memory and the registers built into the processor itself are the only general-purpose storage that…

Explore content categories