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 02-28-2008, 03:41 AM   #1
cryincold
Member
 
Registered: Sep 2007
Location: Zhuhai, China
Distribution: Debian, etch
Posts: 40

Rep: Reputation: 15
the return value of getpid() called from main thread and new thread r identical !!!


in linux, the clone system call is used to implement thread.

every thread is a light-weight process .

So, value returned from getpid() from main thread is expected to be different from value returned from getpid() from main thread. But the following code on my system( kernel 2.6.18 ) shows that.


Quote:
void* f(void* arg)
{
printf("thread: PID: %d, thread ID %u\n", getpid(), pthread_self());
}

int main(int argc, char** argv)
{
int err;
pthread_t tid;
err = pthread_create(&tid, NULL, f, NULL);
if (err!=0)
fprintf(stderr, "failed!\n");
printf("main: PID: %d, thread ID %u\n", getpid(), pthread_self());
pause();
exit(0);
}

$./a.out
main: PID: 3666, thread ID 3085272768
thread: PID: 3666, thread ID 3085269936
But. APUE said these 2 process IDs will not match ...
Has 2.6.x changed the way it implement thread ?
 
Old 02-28-2008, 06:27 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You may be thinking of fork. Threads are still a part of the same process.
ta0kira
 
Old 02-28-2008, 06:48 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,682

Rep: Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490
APUE was written a little while ago (although still a great book).
FYI, I wrote a multi-thr prog (in Perl) a while ago on a 2.4 kernel, and (if you ran top cmd) each thr had it's own unique pid (as you expected above).
However, when it was ported to a 2.6 kernel, you only see 1 pid (that of the 'parent' thr) once.
 
Old 02-29-2008, 02:37 AM   #4
cryincold
Member
 
Registered: Sep 2007
Location: Zhuhai, China
Distribution: Debian, etch
Posts: 40

Original Poster
Rep: Reputation: 15
Wink

Tku for your reply to have my supposition confirmed.
 
  


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
Exception in thread "main" java.lang.ClassFormatError: onmyown.Main (unrecognized cla zimboney Fedora 4 07-20-2007 02:00 PM
getting the return value from a thread start function cynthia_thomas Programming 1 02-18-2006 09:23 AM
Return of State in a thread lucky6969b Programming 1 01-09-2006 11:12 AM
can thread return a local variable? iclinux Programming 1 01-14-2005 12:37 AM
Main thread sending notification to child thread rajesh_b Programming 1 09-22-2004 10:15 AM

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

All times are GMT -5. The time now is 05:02 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration