LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2004, 02:07 AM   #1
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Rep: Reputation: 15
how to get thread's pid?


if call getpid() in the thread , it will return the father's pid,
but I want the thread's true pid, how?

note:
I use pthread_create(&id,NULL,&fun,&arg) to get the thread.
 
Old 12-14-2004, 02:29 AM   #2
melinda_sayang
Member
 
Registered: Dec 2003
Location: Petaling Jaya
Distribution: Ubuntu
Posts: 475

Rep: Reputation: 31
what is your code?

getppid() will return father's pid but getpid() will return own pid.

Look for example:

Code:
#include <stdio.h>
#include <stdlib.h>

int main(void) {

printf( "I am parent. This is my pid: %d", getpid() );

if(fork()==0) {
     printf( "I am child. This is the child pid: %d", getpid() );
     printf( "This is the parent's pid: %d", getppid() );
}

}
Maybe you put getpid() in parent's section code. Of course it will give parent pid not child pid. I guess the case will be similar with pthread_create function.
 
Old 12-14-2004, 02:34 AM   #3
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Original Poster
Rep: Reputation: 15
I don't think so.
maybe it 's true when using fork(),
but I think getppid() and getpid() return the same thing in threads(created by pthread_create).
 
Old 12-14-2004, 04:09 AM   #4
Marius2
Member
 
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 276

Rep: Reputation: 31
Use pthread_self().
 
Old 12-14-2004, 04:51 AM   #5
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Original Poster
Rep: Reputation: 15
find something:
if rm /lib/tls/
then getpid() return different pid in thread from its father.

my distribution=rh9.0

but why?
 
Old 12-14-2004, 05:05 AM   #6
mayur
LQ Newbie
 
Registered: Jun 2003
Posts: 14

Rep: Reputation: 0
check out man pthread_self
 
Old 12-14-2004, 05:55 AM   #7
shy
Member
 
Registered: Dec 2002
Location: Russia
Distribution: ASP linux
Posts: 94

Rep: Reputation: 15
pthread_self() returns thread id, not process id
AFAIK in kernel 2.6 they changed something, and it's not possible (or harder then just use getpid()) to get the correct pid in thread.
Check out this thread

Last edited by shy; 12-14-2004 at 06:01 AM.
 
Old 12-14-2004, 06:08 AM   #8
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Original Poster
Rep: Reputation: 15
I see now,
thanks a lot!
 
Old 11-21-2017, 12:21 AM   #9
Clauselan
LQ Newbie
 
Registered: Nov 2017
Location: South Korea
Posts: 2

Rep: Reputation: Disabled
use gettid()

Just use gettid();

^^;
 
Old 11-21-2017, 12:40 AM   #10
Clauselan
LQ Newbie
 
Registered: Nov 2017
Location: South Korea
Posts: 2

Rep: Reputation: Disabled
Quote:
Originally Posted by Clauselan View Post
Just use gettid();

^^;
If it doesn't work, use following code instead of gettid().

syscall(__NR_gettid)
 
Old 11-21-2017, 02:06 PM   #11
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,583
Blog Entries: 11

Rep: Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601
Clauselan, welcome to LQ!

You have replied to a 13-year quiet thread, which is generally discouraged. Please consider posting to your own thread, with a description of your own questions or information so that it may be more accessible and helpful to current and future visitors.

Please review the Site FAQ, especially this page, for guidance in writing posts and general forum participation.

Welcome aboard, and good luck!
 
Old 11-22-2017, 09:30 AM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
All of the threads created by a process belong to that process and all of them will therefore retrieve the same process-ID.

Each thread also has its own unique, separate, identifier.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
/var/run/[XXX].pid - Tcl pid code liguorir Linux - Software 1 05-20-2004 10:32 PM
rm cannot remove /var/run/atd.pid and /var/run/xdm.pid danishmr Linux - Software 1 05-04-2004 08:01 AM
ERROR: Couldn't write pid to pid file lawrencegoodman Linux - Newbie 2 02-13-2004 08:05 PM
Pid ck7802 Linux - Newbie 3 08-20-2003 11:22 AM
How to get the PID? Steave Programming 14 10-17-2001 09:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration