Why do I see one more thread than the number I created in my `ps` listing?

后端 未结 3 844
我在风中等你
我在风中等你 2021-01-19 05:27

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

相关标签:
3条回答
  • 2021-01-19 05:46

    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. You pthread_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 your pthread_creates.

    0 讨论(0)
  • 2021-01-19 05:47

    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

    0 讨论(0)
  • 2021-01-19 05:55

    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.

    0 讨论(0)
提交回复
热议问题