Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relation between Thread ID and Process ID

I am having some confusion between Process Id and Thread Id. I have gone through several web-post including stack overflow here, Which says

starting a new process gives you a new PID and a new TGID, while starting a new thread gives you a new PID while maintaining the same TGID.

So when I run a program why all the threads created from the program don't have different PID?

I know in programming we usually say that the main is a thread and execution starts from main , So if I create multiple thread from main, all the threads will have the same PID which is equal to the main's PID.

So what I wanted to ask is as below:

1) When we run a program it will run as a process or a thread?

2) Is there any difference between main thread creating threads and Process creating threads?

3) Is there any difference between thread and process in linux? Since I read somewhere that linux doesn't differentiate between Thread and Process.

like image 493
neo Avatar asked Oct 30 '13 09:10

neo


People also ask

Do threads share process ID?

multiple threads can share the same pid . The kernel, however, views them as tid , since NPTL is implemented in user space, the kernel doesn't really know much about NPTL threads, but threads created ( clone with CLONE_THREAD flag) in the same process share the same pid .

Do all threads have the same process ID?

In the kernel, each thread has its own ID, called a PID, although it would possibly make more sense to call this a TID, or thread ID, and they also have a TGID (thread group ID) which is the PID of the first thread that was created when the process was created.

How do I find the thread ID of a process in Linux?

Identifying the thread On Unix® and Linux® systems, you can use the top command: $ top -n 1 -H -p [pid]replacing [pid] with the process ID of the affected process. On Solaris®, you can use the prstat command: $ prstat -L -p [pid]replacing [pid] with the process ID of the affected process.

How do you get PID from TID?

PID Stands for Process ID and TID stands for Thread ID. To get the process ID, we call getpid() API in Linux and gettid() for thread ID.


2 Answers

This confusion comes from the Linux concept of tasks.

In Linux there is little difference between a task and a thread though.

Every process is a self contained VM running at least one task.

Each task is an independent execution unit within a process scope.

The main task of a process gives it's task id (TID) to the process as it's process id (PID).

Every new thread that you spawn within a process creates a new task within it. In order to identify then individually in the kernel they get assigned their own individual task id (TID).

All tasks within a process share the same task group id (TGID).

like image 64
Sergey L. Avatar answered Sep 27 '22 17:09

Sergey L.


Simplifying a bit:

  1. The PID is the process ID, TID is the thread ID. The thing is that for the first thread created by fork(), PID=TID. If you create more threads within the process, with a clone() command, then PID and TID will be different, PID will always be smaller than TID.

  2. No, there is no difference, except maybe that if main is killed, all other threads are also killed.

  3. Yes, the thread is what actually gets scheduled. Technically, the process is only a memory mapping of the different segments of code (text, bss, stack, heap and the OS).

like image 22
Dervin Thunk Avatar answered Sep 27 '22 16:09

Dervin Thunk