Why am I seeing another stream than the number I created in my `ps` sheet?

When I create a thread ( pthread_create() ) from my main process, I see three (3) threads in the ps list, why is this? That is, I see a process for the main thread, one for the created thread and the third for something else. Something else? Everything works fine, I'm just wondering what an additional 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 list that I get after rebooting and running 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 the 2.6.14 kernel with several driver modules added.

+4
source share
3 answers

Most likely, this is a stream of threads. See Answer D.5 in this link .

You will not see the additional process specified in most modern Linux systems if they use NPTL. But I searched, and it looks like BusyBox is using ulibc, which I think has recently added NPTL support. Therefore, I do not know for sure, but I suppose you are using LinuxThreads and see the manager thread as an additional thread.

+2
source

You see one stream more than you create, because you do not consider the main stream of programs.

Each time you run the program, you start the process with 1 thread. If you pthread_create one thread, then you have two threads. You pthread_create second, and you get three threads.

That's why your ps (which you think is in one of the comments) shows threads, shows you more than the number of your pthread_create s.

+2
source

Could it be that ps displays 1 line for the process and 2 lines for both threads. You do not show how ps is issued, which version, and you do not include the entire ps command.

ps usually only shows processes, not threads.

According to busybox.net/downloads/BusyBox.html the ps command will not show streams. ps -T will show streams. Therefore, if you are sure that only ps is issued (I do not know about aliases in BusyBox or nothing at all, I never used it), then you see 3 processes, not threads.

Also can you use the old version of BusyBox? See This Error Report: bugs.busybox.net/show_bug.cgi?id=3835

0
source

Source: https://habr.com/ru/post/1384728/


All Articles