Solved

Thead ID

Posted on 2004-04-22
8
1,128 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IPtables not running - RHEL7 64bit 5 236
Shell script / command to generate IO / IOPS load 22 300
shell script working with --daemon 18 93
BASH script to modify crontab? 3 50
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 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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

910 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

16 Experts available now in Live!

Get 1:1 Help Now