When I create a thread (pthread_create()
) from my main process, I see three (3) threads in the ps
listing, why is this? That is, I see the process for the main thread, one for the created thread, and third for something else. What is the something else? Everything works just fine, I'm just wondering what the extra listed process is.
~/ cat test.c
#include <errno.h>
#include <pthread.h>
static pthread_t thread;
void * test_thread(void * ptr)
{
sleep(30);
return(ptr);
}
void thread_init(void)
{
if (pthread_create( &thread , NULL, test_thread, NULL))
perror("Thread not created!");
}
int main(int argc, char ** argv)
{
thread_init();
sleep(30);
}
When I execute this code on a system running Linux 2.6.14 and BusyBox (but using bash 2.04g), the ps
listing I get after rebooting and launching my test program above:
...
52 root SW [kswapd0]
667 root SW [mtdblockd]
710 root SWN [jffs2_gcd_mtd4]
759 root 980 S /bin/sh
760 root 500 S /bin/inetd
761 root 516 S /bin/boa
762 root 644 S /sbin/syslogd -n
763 root 640 S /sbin/klogd -n
766 root 1516 S /bin/sshd -i
767 root 1036 S -sh
768 root 420 S ./test
769 root 420 S ./test
770 root 420 S ./test
771 root 652 R ps
The kernel is a 2.6.14 kernel with a few added drivers modules.
Could it be that ps is displaying 1 line for the process and 2 lines for both threads. You don't show how ps is being issued, what version, nor do you contain the entire about of the ps command.
ps typically shows only processes, not threads.
According to busybox.net/downloads/BusyBox.html the ps command will not show threads. ps -T will show threads. So if you are sure only ps is being issued (I don't know about alias in BusyBox or anything at all, I've never used it) then you are seeing 3 processes, not threads.
Also you may be using an old version of BusyBox? Refer to this bug report: bugs.busybox.net/show_bug.cgi?id=3835
You are seeing one thread more than you are creating because you are not counting the programs main thread.
Every time you start a program, you fire up a process that has 1 thread running. If you
pthread_create
one thread, then you have two threads running. Youpthread_create
a second one and you get three threads running.That is why your
ps
(which according to you on one of the comments) shows threads, is showing you one more than the number of yourpthread_create
s.It is likely the "thread manager" thread. See answer D.5 at this link.
You won't see the extra process listed on most modern Linux systems if they are using NPTL. But I searched and it sounds like BusyBox uses ulibc which I think only added NPTL support recently. So I don't know for sure, but my guess is you are using LinuxThreads and seeing the manager thread as the extra thread.