From owner-freebsd-hackers@FreeBSD.ORG Sun Jun 20 02:37:45 2004 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 9417D16A4CE; Sun, 20 Jun 2004 02:37:45 +0000 (GMT) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id 7341743D49; Sun, 20 Jun 2004 02:37:45 +0000 (GMT) (envelope-from davidxu@freebsd.org) Received: from freebsd.org (davidxu@localhost [127.0.0.1]) i5K2bAFd055779; Sun, 20 Jun 2004 02:37:11 GMT (envelope-from davidxu@freebsd.org) Message-ID: <40D4F7E9.60908@freebsd.org> Date: Sun, 20 Jun 2004 10:35:21 +0800 From: David Xu User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.6) Gecko/20040522 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Cyrille Lefevre References: <008901c4566c$610d1300$7890a8c0@dyndns.org> In-Reply-To: <008901c4566c$610d1300$7890a8c0@dyndns.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit cc: hackers@freebsd.org cc: current@freebsd.org Subject: Re: -lthr vs. -pthread X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 20 Jun 2004 02:37:45 -0000 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 > >#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. > >