-lthr vs. -pthread
David Xu
davidxu at freebsd.org
Sun Jun 20 02:37:45 GMT 2004
libpthread default is M:N threading model, kernel thread
entity is allocated on demand, things like sleep() only block
thread in userland, no kernel thread will be allocated, so
in your example, you won't see 5 kernel threads, only two
threads are showed here, the extra thread is a signal thread,
there is only one signal thread in process live cycle.
libthr is 1:1, when you allocate a thread in userland, it creates
a kernel thread too.
David Xu
Cyrille Lefevre wrote:
>Hi,
>
>I'm currently working on enhancements to ps w/ "Garance A Drosehn".
>I've just added some thread related stuffs and to see them, I'm
>using the following program :
>
>#define _REENTRANT
>#include <pthread.h>
>
>#define NUM_THREADS 5
>#define SLEEP_TIME 10
>
>void *sleeping(void *);
>pthread_t tid[NUM_THREADS];
>
>int
>main(int argc, char *argv[])
>{
> int i;
>
> for (i = 0; i < NUM_THREADS; i++)
> pthread_create(&tid[i], NULL, sleeping, (void
>*)SLEEP_TIME);
> for (i = 0; i < NUM_THREADS; i++)
> pthread_join(tid[i], NULL);
> printf("main() reporting that all %d threads have terminated\n",
>i);
> return (0);
>}
>
>void *
>sleeping(void *arg)
>{
> int sleep_time = (int)arg;
> printf("thread %d sleeping %d seconds ...\n", thr_self(),
>sleep_time);
> sleep(sleep_time);
> printf("\nthread %d awakening\n", thr_self());
> return (NULL);
>}
>
>then, I compile this one in 2 way :
>
># cc -o thread thread.c -lthr
>and
># cc -pthread -o pthread thread.c
>
>here is some of the "new ps" outputs :
>
>"lwp" is the thread id and "nlwp" the the number of threads.
>-q switch in posix mode (aka SystemV) and -C select processes
>by name (a la pgrep).
>
># ./thread& sleep 1; ps -H -O lwp,nlwp -qC thread
>(thread, using -H)
> PID LWP NLWP TTY TIME COMMAND
>85146 100005 6 ttyp0 00:00:00 thread
>85146 100004 6 ttyp0 00:00:00 thread
>85146 100003 6 ttyp0 00:00:00 thread
>85146 100002 6 ttyp0 00:00:00 thread
>85146 100001 6 ttyp0 00:00:00 thread
>85146 85146 6 ttyp0 00:00:00 thread
># ./pthread& sleep 1; ps -H -O lwp,nlwp -qC thread
>(pthread, using -H)
> PID LWP NLWP TTY TIME COMMAND
>96689 100002 2 ttyp0 00:00:00 pthread
>96689 96689 2 ttyp0 00:00:00 pthread
>
>is it normal that -pthread only forks only 1 thread where
>-lthr forks 5 of them ?
>
># ./thread& sleep 1; ps -O lwp,nlwp -qC thread
>(thread ot pthread, not using -H)
> PID LWP NLWP TTY TIME COMMAND
>73718 100005 6 ttyp0 00:00:00 thread
>
>
>is it normal that the selected process is the last forked thread and
>not the thread owner (father) ?
>
>PS : using -lc_r, there is no thread at all, but I suppose this is an
>expected behaviour.
>
>CC -current and -hackers
>
>Cyrille Lefevre.
>
>
More information about the freebsd-hackers
mailing list