13

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.

Community
  • 1
  • 1
neo
  • 969
  • 1
  • 11
  • 23
  • Possible duplicate of [difference-between-pid-and-tid](http://stackoverflow.com/questions/4517301/difference-between-pid-and-tid). – rodrigo Oct 30 '13 at 09:54
  • I dont think this a duplicate of http://stackoverflow.com/questions/4517301/difference-between-pid-and-tid. I am asking here about the thread and process id when we run a program and create threads. – neo Oct 30 '13 at 09:57
  • 1
    @mohit :: go through this [link](http://stackoverflow.com/questions/19676071/understanding-pthreads), I just posted few hours before. – Raju Kunde Oct 30 '13 at 10:59
  • @user1940987 Thanks alot ...+1. It cleared my confusion. – neo Oct 30 '13 at 13:39

5 Answers5

16

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).

Dervin Thunk
  • 19,515
  • 28
  • 127
  • 217
  • @Adam: Hah, that's cool, never heard that before. Thanks for the analogy. – Dervin Thunk Oct 30 '13 at 10:00
  • +1 for answering one of my doubt of whether TID and PID is same for main thread – pRAShANT Oct 30 '13 at 10:01
  • Threads and processes usually share more than just memory: they share file descriptors, signal disposition, current directory... and probably other few things. – rodrigo Oct 30 '13 at 10:09
  • @DervinThunk: That means if I create multiple thread inside a process then all the threads will have different PID? – neo Oct 30 '13 at 10:11
  • @rodrigo: Hence the "simplifying a bit" disclaimer... at this level, it makes little sense to talk about those concepts. – Dervin Thunk Oct 30 '13 at 10:15
  • @mohit: Multiple threads in same process should have different TIDs but share a PID. But, to tell you the truth, I've never bothered to try it... should be easy to check: create a bunch of threads with pthread_create, run htop, press T for threads and check out the TIDs vs. PIDs of your process. – Dervin Thunk Oct 30 '13 at 10:18
  • @DervinThunk: But when you go on the http://stackoverflow.com/questions/9305992/linux-threads-and-process link and check the image submitted by paxdiablo it shows that If a process creates thread it will have different PID and 28 people up voted that answer. – neo Oct 30 '13 at 10:35
  • @mohit: Well, truth is not a democracy, don't you think? :) ps -eLf | grep will show PID and TID for chrome. If you have a few tabs open, you will see that "2496 1 2496 1 35 06:20 ? 00:01:17 /opt/chrome", while the next one is "2496 1 2525 0 35 06:20 ? 00:00:00 /opt/chrome" – Dervin Thunk Oct 30 '13 at 10:47
  • @DervinThunk: One doubt, I hope Chrome opens each tab as a separate process. Thus the pid should be different. – pRAShANT Oct 30 '13 at 11:08
  • @pRAShANT: As a separate process?! I don't think so... too expensive. Maybe it does have some optimization, but I don't know about it. Only thing I know is that the example with ps -eLf tells us more or less what I want to say. – Dervin Thunk Oct 30 '13 at 11:12
  • @DervinThunk I was also not sure for the same, so I googled it, as I read it before somewhere, thus after reading on following [How can Google Chrome isolate tabs into seperate processes while looking like a single application?](http://stackoverflow.com/questions/2019500/how-can-google-chrome-isolate-tabs-into-seperate-processes-while-looking-like-a), I did post my previous comment. – pRAShANT Oct 30 '13 at 11:46
  • @pRAShANT: I don't understand. Did you read the answers? In any case, process and threads are usually not distinguished... mainly b/c a process always has a thread associated to it (main). But in any case, if you see the first answer, it says"There's only one browser process, which manages the tabs, windows, and "chrome" of the browser." One process. I think the guy means threads after that. Please, do run ps eLF and check for yourself! – Dervin Thunk Oct 30 '13 at 12:08
6

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).

Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • So when I create multiple thread within a process, does pid of all the threads will be same or different? – neo Oct 30 '13 at 11:59
  • They are technically TIDs and they will be different. Check `/proc/[pid]/tasks`. The process id PID will be equal to the main thread's TID and when you address that PID you will be addressing the main thread. – Sergey L. Oct 30 '13 at 12:11
2

I got the answer here on stackoverflow. It states that if we run a program on Linux that contains the libc libuClibc-0.9.30.1.so (1). Basically an older version of libc then thread created will have different PID as shown below

root@OpenWrt:~# ./test
main thread pid is 1151
child thread pid is 1153

and I tried to run this program with a linux that contains the libc from ubuntu libc6 (2) i.e newer version of libc then Thread created will have the same PID as the process.

$ ./test
main thread pid is 2609
child thread pid is 2609
The libc (1) use linuxthreads implementation of pthread

And the libc (2) use NPTL ("Native posix thread library") implementation of pthread

According to the linuxthreads FAQ (in J.3 answer):

each thread is really a distinct process with a distinct PID, and signals sent to the PID of a thread can only be handled by that thread

So in the old libc which use linuxthreads implementation, each thread has its distinct PID

In the new libc version which use NPTL implementation, all threads has the same PID of the main process.

The NPTL was developed by redhat team. and according to the redhat NPTL document: One of the problems which are solved in the NPTL implementation is:

(Chapter: Problems with the Existing Implementation, page5)

Each thread having a different process ID causes compatibility problems with other POSIX thread implementations. This is in part a moot point since signals can't be used very well but is still noticeable

And that explain this issue.

I am using the new libc version that contains the NPTL ("Native posix thread library") implementation of pthread.

Community
  • 1
  • 1
neo
  • 969
  • 1
  • 11
  • 23
1

The post you have shown describes of Linux threading implementation which I suppose is the older version of Linux implementation where threads were created as a different process. In the POSIX implementation of threads, the threads are not created as a different process rather they create different streams of parallel execution of the code which have some components differ in the those parallel execution, the information of which is stored by Thread Descriptor storing the TID. Whereas the process creating multiple thread can be referred as a multi-threaded process, thus has a same PID of all its thread but different TID's. The main process creating thread can be referred as Main thread

pRAShANT
  • 523
  • 1
  • 6
  • 16
0

You will get same Process ID as all threads are sharing your program data which is your process so when you call for Process ID you get the same.

EduardoSaverin
  • 545
  • 4
  • 19