Solved

Thead ID

Posted on 2004-04-22
8
1,127 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
8 Comments
 
LVL 8

Expert Comment

by:Anthony2000
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 45

Accepted Solution

by:
sunnycoder earned 50 total points
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:linux_newb
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks guys!!
i'll split the points..
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now