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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
02-28-2008, 02:41 AM
|
#1
|
Member
Registered: Sep 2007
Location: Zhuhai, China
Distribution: Debian, etch
Posts: 40
Rep:
|
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 ?
|
|
|
02-28-2008, 05:27 AM
|
#2
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Rep: 
|
You may be thinking of fork. Threads are still a part of the same process.
ta0kira
|
|
|
02-28-2008, 05:48 PM
|
#3
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
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.
|
|
|
02-29-2008, 01:37 AM
|
#4
|
Member
Registered: Sep 2007
Location: Zhuhai, China
Distribution: Debian, etch
Posts: 40
Original Poster
Rep:
|
Tku for your reply to have my supposition confirmed. 
|
|
|
All times are GMT -5. The time now is 01:37 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|