Solved

Thead ID

Posted on 2004-04-22
8
1,132 Views
Last Modified: 2009-07-29
hi all!

i am confused regarding the thread identifier obtained using via pthread_self( ) and getpid( ). What exactly is the difference between the two and what do they imply??

Can somebody help me and explain/send a suitable link?
Thanks!
0
Comment
Question by:linux_newb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 8

Expert Comment

by:Anthony2000
ID: 10895933
check this link out: http://www.talkaboutprogramming.com/group/comp.programming.threads/messages/41158.html
It contains an exmple program that you can try as well.

Cheers,

Anthony
0
 

Expert Comment

by:tomatoyohe
ID: 10896197
Well pthread_self() returns the thread identifier for the calling thread and getpid() returns a process id.  The same process can fire up multiple threads each with a different thread identifier.

http://www.die.net/doc/linux/man/man2/getpid.2.html

http://www.die.net/doc/linux/man/man3/pthread_self.3.html
0
 

Author Comment

by:linux_newb
ID: 10896485
I think i didnt put my question properly..
I know pthread_self() returns thread identifier & getpid() returns process id..

What i want to know is, what  exactly is the relevance of thread id  and process id?
When i print the process id for a thread, it is different and unique to it, as is thread id..
I am using RedHat 8.
So which should be used if one is looking for a particular thread? thread id or pid??

Sorry for not framing the question correctly..
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Accepted Solution

by:
sunnycoder earned 50 total points
ID: 10896574
In a thread you should be looking for thread id and not pid ..

In linux pthreads are implemented using clone and hence all of them are separate processes and thus all of them do have separate unique pids ... But since you are working with threads, it is better to use its interface ... this will allow your code tp be portable for platforms which do not have different pids for each thread
0
 

Author Comment

by:linux_newb
ID: 10896771
sunnycoder,

I got your point..

Still have a doubt. A created a small multithreaded prog, which loops for a while before terminating. Then executed it  twice at the same time. In two executions, PID of main process and threads are different but the thread id are identical.
Is it possible that two different processes (executing at same time) can have threads with  same thread ids?
Below is the output..

Main thread -- TID: 8192 PID: 1466
Child1 thread -- TID: 8194 PID: 1468 PPID: 1467
Child1 thread -- TID: 16387 PID: 1469 PPID: 1467
Child2 thread -- TID: 24580 PID: 1470 PPID: 1467
Child2 thread -- TID: 32773 PID: 1471 PPID: 1467
Child2 thread -- TID: 40966 PID: 1472 PPID: 1467
Child2 thread -- TID: 49159 PID: 1473 PPID: 1467

---------------------------------------------------
Main thread -- TID: 8192 PID: 1458
Child1 thread -- TID: 8194 PID: 1460 PPID: 1459
Child1 thread -- TID: 16387 PID: 1461 PPID: 1459
Child2 thread -- TID: 24580 PID: 1462 PPID: 1459
Child2 thread -- TID: 32773 PID: 1463 PPID: 1459
Child2 thread -- TID: 40966 PID: 1464 PPID: 1459
Child2 thread -- TID: 49159 PID: 1465 PPID: 1459

Thanks!!
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10896827
Honestly, I never tried it and never thought that thread-ids could be repeated in different processes ... but as your results show, they are!!! May be it internally maintains a threadid pid combination to identify a thread system-wide uniquely ...
stick to pthread_self
0
 
LVL 12

Assisted Solution

by:stefan73
stefan73 earned 20 total points
ID: 10897629
Hi linux_newb,
> Is it possible that two different processes (executing at same time)
> can have threads with  same thread ids?

If I remember correctly, a POSIX thread ID is only valid within the parent process context. The fact that Linux creates new PIDs via clone() does not change that.

But indeed the Linux behaviour is a bit strange. I wonder how signal handling works in the case of different PIDs.

Cheers,
Stefan
0
 

Author Comment

by:linux_newb
ID: 10914646
thanks guys!!
i'll split the points..
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question