为什么我看到的线程数比我在`ps`列表中创建的线程数多? [英] Why do I see one more thread than the number I created in my `ps` listing?

查看:88
本文介绍了为什么我看到的线程数比我在`ps`列表中创建的线程数多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从主进程创建线程(pthread_create())时,在ps列表中看到三(3)个线程,这是为什么?也就是说,我看到了主线程的过程,一个是创建线程的过程,第三个是其他线程的过程. 还有什么? 一切正常,我只是想知道列出的额外过程是什么.

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);
}

当我在运行Linux 2.6.14和BusyBox(但使用bash 2.04g)的系统上执行此代码时,重新启动并启动上面的测试程序后得到的ps列表:

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

该内核是2.6.14内核,其中添加了一些驱动程序模块.

The kernel is a 2.6.14 kernel with a few added drivers modules.

推荐答案

可能是线程管理器"线程.在此链接中查看答案D.5.

It is likely the "thread manager" thread. See answer D.5 at this link.

如果大多数现代Linux系统使用NPTL,您将不会看到列出的额外过程.但是我进行了搜索,听起来好像BusyBox使用ulibc,我认为它最近才添加了NPTL支持.所以我不确定,但是我猜是您正在使用LinuxThreads,并将管理器线程视为额外的线程.

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.

这篇关于为什么我看到的线程数比我在`ps`列表中创建的线程数多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆