-lthr vs. -pthread

Cyrille Lefevre clefevre-lists at 9online.fr
Sun Jun 20 02:14:59 GMT 2004


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.
-- 
mailto:clefevre-lists at 9online.fr


More information about the freebsd-current mailing list