UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2006-12-19 06:39 AM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: how to get threadpid?

hiho

i'm pretty sure many of you already know this, but for me it seems something new
i'm currently debugging my app and using pstree i saw that my prog has more child's than i forked
pstree seems to show pid's of the thread's i created too
i wrote a little threadtest.c

#include<stdio.h>
#include<pthread.h>

void* thread_func(void *argv);

int main(int argc,char **argv){
     void *arg;

     printf("mainpid: %u\n",getpid());
     pthread_t th;
     pthread_create(&th,NULL,thread_func,NULL);
     pthread_join(th,&arg);
}

void* thread_func(void *argv){
   printf("threadpid: %u\n",getpid());
   sleep(5);
   return NULL;
}


gcc -lpthread -o threadtest.o threadtest.c

the code above prints something like this:
mainpid: 13156
threadpid: 13156

but pstree print's
threadtest.o(13156)----{threadtest.o}(13157)

i already know that a thread is created using clone which means that the kernel doesn't differnentiate between thread and process (unlike windows does)
using strace for my testprog it shows:
clone(child_stack=0x40800260, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x408009d0, tls=0x40800940, child_tidptr=0x408009d0) = 13157

Q1: if getpid() in the thread doesn't return the threadpid, how do i get it?
Q2: what exactly is the consequence of this? example: if i limited the kernel to be able to handle a max of 65536 processes and i create 65536 thread's
is there a free pid available or is the system running out of pid's cause of too much threads?
Q3: to learn from your expiriences, anyone has some troubles with this? e.g. are there some exotic architectures which return the threadpid and not the pid using getpid()? or something different which was unexpected

[email protected]

Offline

#2 2006-12-19 01:11 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: how to get threadpid?

A1: PID is process ID, so it's a process wide thing. You should use pthread_t when using pthreads, and you either have that or can get it with pthread_self(). That the thread is handled as a more or less normal process is a Linux specific thing, just like gettid().

A2: Threads count as processes for the process limit (which makes sense as it are just processes that are sharing memory).

A3: The old pthread (linuxthreads, instead of the newer NPTL one) implementation might intercept getpid and give the pid of the control "thread" instead of the program. Or maybe it will return the pid of each "thread". But whatever crazy things happen, it should be transparent. But you can count on getpid returning the PID you can use to do other PID specific things, like kill().

Offline

#3 2006-12-19 05:01 PM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: how to get threadpid?

seems this post get's more in the thread's topic than processes ;-)
@i3839
maybe i'm just too stupid to find something i can understand, but what exactly is NPTL? is it a kernel patch? or a replacement for libpthread?
in case of programming: what's the difference to pthread's?
i only found a paper from ulrich drepper (i'm sure everyone here knows it), but the link to the (NPTL) sources seems outdated

if it's only a library it would be no problem to switch to NPTL but if it's a kernel patch i'm not sure if our admin's like this kind of work ;-)

and
to be more concrete
how do i use NPTL?
i mean i can use "man pthread" but didn't find much by searching for "man NPTL"
i think i must be an idiot cause this "library" (or whatever it is) exist's since 2002! and i can't find something usefull (for me) about it
so any resource would be great too

thx for your time!

Offline

#4 2006-12-20 03:23 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: how to get threadpid?

NPTL stands for Native POSIX Thread Library. The old Linuxthreads was hacky and not very POSIX compliant, hence the name.

It's mostly a pthread library (or rather, it is the default pthread library used on modern Linux systems), but it has some help from the kernel, which it relies on. On the kernel side there are new clone() flags and futex() support (fast userspace mutex). This is since kernel 2.5, though RedHat carried some patches for 2.4 too.

On glibc's side pthread is implemented using those kernel features, but the rest of the system just uses libpthreads like usually. Only programs relying on the broken Linuxthreads behaviour need updating (but that should be already done by now).

So if you've a 2.6 kernel and a not too old glibc then all should be fine and use NPTL automatically.

Offline

#5 2006-12-20 04:01 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: how to get threadpid?

Offline

Board footer

Powered by FluxBB