Linux之线程管理

linux下查看线程数的几种方法  
1、 cat /proc/${pid}/status
[root@limt01 2325]# ps -ef|grep xinetd|grep -v grep
root      2325     1  0 00:26 ?        00:00:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
[root@limt01 2325]# cat /proc/2325/status
Name:   xinetd
State:  S (sleeping)
Tgid:   2325
Pid:    2325
PPid:   1
TracerPid:      0
Uid:    0       0       0       0
Gid:    0       0       0       0
Utrace: 0
FDSize: 64
Groups:
VmPeak:    22344 kB
VmSize:    22180 kB
VmLck:         0 kB
VmHWM:       964 kB
VmRSS:       964 kB
VmData:      404 kB
VmStk:        88 kB
VmExe:       164 kB
VmLib:      3004 kB
VmPTE:        64 kB
VmSwap:        0 kB
Threads:        1
SigQ:   1/15637
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000381000
SigCgt: 000000007fc3eeff
CapInh: 0000000000000000
CapPrm: ffffffffffffffff
CapEff: ffffffffffffffff
CapBnd: ffffffffffffffff
Cpus_allowed:   f
Cpus_allowed_list:      0-3
Mems_allowed:   00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
Mems_allowed_list:      0
voluntary_ctxt_switches:        22
nonvoluntary_ctxt_switches:     1


2、pstree -p ${pid}
# pstree -p 6648
agent_executor(6648)─┬─tar(15601)───gzip(15607)
├─{agent_executor}(6650)
├─{agent_executor}(14214)
├─{agent_executor}(14216)
├─{agent_executor}(15374)
├─{agent_executor}(15376)
├─{agent_executor}(15378)
├─{agent_executor}(15380)
├─{agent_executor}(15392)
├─{agent_executor}(15394)
└─{agent_executor}(15398)


3、top -p ${pid} 再按H   或者直接输入 top -bH -d 3 -p  ${pid}


top -H
手册中说:-H : Threads toggle
加上这个选项启动top,top一行显示一个线程。否则,它一行显示一个进程。


4、ps 
[root@limt01 2325]# ps -L 2325
  PID   LWP TTY      STAT   TIME COMMAND
 2325  2325 ?        Ss     0:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
[root@limt01 2325]# ps H 2325
  PID TTY      STAT   TIME COMMAND
 2325 ?        Ss     0:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
[root@limt01 2325]# 
[root@limt01 2325]# ps -T 2325
  PID  SPID TTY      STAT   TIME COMMAND
 2325  2325 ?        Ss     0:00 xinetd -stayalive -pidfile /var/run/xinetd.pid


5. pstack
pstack显示每个进程的栈跟踪
$ pstack 4551
Thread 7 (Thread 1084229984 (LWP 4552)):
#0  0x000000302afc63dc in epoll_wait () from /lib64/tls/libc.so.6
#1  0x00000000006f0730 in ub::EPollEx::poll ()
#2  0x00000000006f172a in ub::NetReactor::callback ()
#3  0x00000000006fbbbb in ub::UBTask::CALLBACK ()
#4  0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0
#5  0x000000302afc6003 in clone () from /lib64/tls/libc.so.6
#6  0x0000000000000000 in ?? ()
Thread 6 (Thread 1094719840 (LWP 4553)):
#0  0x000000302afc63dc in epoll_wait () from /lib64/tls/libc.so.6
#1  0x00000000006f0730 in ub::EPollEx::poll ()
#2  0x00000000006f172a in ub::NetReactor::callback ()
#3  0x00000000006fbbbb in ub::UBTask::CALLBACK ()
#4  0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0
#5  0x000000302afc6003 in clone () from /lib64/tls/libc.so.6
#6  0x0000000000000000 in ?? ()
Thread 5 (Thread 1105209696 (LWP 4554)):
#0  0x000000302b80baa5 in __nanosleep_nocancel ()
#1  0x000000000079e758 in comcm::ms_sleep ()
#2  0x00000000006c8581 in ub::UbClientManager::healthyCheck ()
#3  0x00000000006c8471 in ub::UbClientManager::start_healthy_check ()
#4  0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0
#5  0x000000302afc6003 in clone () from /lib64/tls/libc.so.6
#6  0x0000000000000000 in ?? ()
Thread 4 (Thread 1115699552 (LWP 4555)):
#0  0x000000302b80baa5 in __nanosleep_nocancel ()
#1  0x0000000000482b0e in armor::armor_check_thread ()
#2  0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0
#3  0x000000302afc6003 in clone () from /lib64/tls/libc.so.6
#4  0x0000000000000000 in ?? ()
Thread 3 (Thread 1126189408 (LWP 4556)):
#0  0x000000302af8f1a5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
#1  0x000000302af8f010 in sleep () from /lib64/tls/libc.so.6
#2  0x000000000044c972 in Business_config_manager::run ()
#3  0x0000000000457b83 in Thread::run_thread ()
#4  0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0
#5  0x000000302afc6003 in clone () from /lib64/tls/libc.so.6
#6  0x0000000000000000 in ?? ()
Thread 2 (Thread 1136679264 (LWP 4557)):
#0  0x000000302af8f1a5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
#1  0x000000302af8f010 in sleep () from /lib64/tls/libc.so.6
#2  0x00000000004524bb in Process_thread::sleep_period ()
#3  0x0000000000452641 in Process_thread::run ()
#4  0x0000000000457b83 in Thread::run_thread ()
#5  0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0
#6  0x000000302afc6003 in clone () from /lib64/tls/libc.so.6
#7  0x0000000000000000 in ?? ()
Thread 1 (Thread 182894129792 (LWP 4551)):
#0  0x000000302af8f1a5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
#1  0x000000302af8f010 in sleep () from /lib64/tls/libc.so.6
#2  0x0000000000420d79 in Ad_preprocess::run ()
#3  0x0000000000450ad0 in main ()


Linux线程介绍
查看最大线程数:
cat /proc/sys/kernel/threads-max
ulimit
User limits - limit the use of system-wide resources.
Syntax
ulimit [-acdfHlmnpsStuv] [limit]
Options
-S Change and report the soft limit associated with a resource. 
-H Change and report the hard limit associated with a resource. 
-a All current limits are reported. 
-c The maximum size of core files created. 
-d The maximum size of a process's data segment. 
-f The maximum size of files created by the shell(default option) 
-l The maximum size that may be locked into memory. 
-m The maximum resident set size. 
-n The maximum number of open file descriptors. 
-p The pipe buffer size. 
-s The maximum stack size. 
-t The maximum amount of cpu time in seconds. 
-u The maximum number of processes available to a single user. 
-v The maximum amount of virtual memory available to the process. 


ulimit provides control over the resources available to the shell and to processes started by it, on systems that allow such control.
If limit is given, it is the new value of the specified resource. Otherwise, the current value of the soft limit for the specified resource is printed, unless the `-H' option is supplied.
When setting new limits, if neither `-H' nor `-S' is supplied, both the hard and soft limits are set.
Values are in 1024-byte increments, except for `-t', which is in seconds, `-p', which is in units of 512-byte blocks, and `-n' and `-u', which are unscaled values.
The return status is zero unless an invalid option is supplied, a non-numeric argument other than unlimited is supplied as a limit, or an error occurs while setting a new limit.

ulimit is a bash built in command.
Ulimit命令
设置限制 可以把命令加到profile文件里,也可以在/etc/security/limits.conf文件中定义
限制。
命令参数
-a 显示所有限制
-c core文件大小的上限
-d 进程数据段大小的上限
-f shell所能创建的文件大小的上限
-m 驻留内存大小的上限
-s 堆栈大小的上限
-t 每秒可占用的CPU时间上限
-p 管道大小
-n 打开文件数的上限
-u 进程数的上限
-v 虚拟内存的上限
除可用Ulimit命令设置外,也可以在/etc/security/limits.conf文件中定义限制。
domino type item value
domino是以符号@开头的用户名或组名,*表示所有用户,type设置为hard or soft。item指
定想限制的资源。如cpu,core nproc or maxlogins。value是相应的限制值。
系统限制默认值
[root@flyinweb ~]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 32764
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 32764
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[root@flyinweb ~]# lsb_release -a
LSB Version: :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: CentOS
Description: CentOS release 5.2 (Final)
Release: 5.2
Codename: Final
linux 系统中单个进程的最大线程数有其最大的限制 PTHREAD_THREADS_MAX


这个限制可以在 /usr/include/bits/local_lim.h 中查看


对 linuxthreads 这个值一般是 1024,对于 nptl 则没有硬性的限制,仅仅受限于系统的资源


这个系统的资源主要就是线程的 stack 所占用的内存,用 ulimit -s 可以查看默认的线程栈大小,一般情况下,这个值是 8M


可以写一段简单的代码验证最多可以创建多少个线程int main() { int i = 0; pthread_t thread; while (1) { if (pthread_create(&thread, NULL, foo, NULL) != 0) return; i ++; printf("i = 


%d\n", i); } }


试验显示,在 linuxthreads 上最多可以创建 381 个线程,之后就会返回 EAGAIN


在 nptl 上最多可以创建 382 个线程,之后就会返回 ENOMEM


这个值和理论完全相符,因为 32 位 linux 下的进程用户空间是 3G 的大小,也就是 3072M,用 3072M 除以 8M 得 384,但是实际上代码段和数据段等还要占用一些空间,这个值应该向下取整到 383


,再减去主线程,得到 382。


那为什么 linuxthreads 上还要少一个线程呢?这可太对了,因为 linuxthreads 还需要一个管理线程


为了突破内存的限制,可以有两种方法


1) 用 ulimit -s 1024 减小默认的栈大小
2) 调用 pthread_create 的时候用 pthread_attr_getstacksize 设置一个较小的栈大小


要注意的是,即使这样的也无法突破 1024 个线程的硬限制,除非重新编译 C 库
相关内容:
一、2.4内核与2.6内核的主要区别
在2.4内核的典型系统上(AS3/RH9),线程是用轻量进程实现的,每个线程要占用一个进程ID,在服务器程序上,如果遇到高点击率访问,会造成进程表溢出,系统为了维护溢出的进程表,会有间歇的


暂停服务现象,而2.6内核就不会发生由于大量线程的创建和销毁导致进程表溢出的问题
二、线程结束必须释放线程堆栈
就是说,线程函数必须调用pthread_exit()结束,否则直到主进程函数退出才释放,特别是2.6内核环境,线程创建速度飞快,一不小心立刻内存被吃光,这一点反倒是2.4内核环境好,因为2.4内核创建


的是进程,而且线程创建速度比2.6内核慢几个数量级。特别提醒,在64位CPU,2.6内核创建线程的速度更加疯狂,要是太快的话,加上usleep ()暂停一点点时间比较好
三、不要编需要锁的线程应用
只有那些不需要互斥量的程序才能最大限度的利用线程编程带来的好处,否则只会更慢,2.6内核是抢占式内核,线程间共享冲突发生的几率远比2.4内核环境高,尤其要注意线程安全,否则就算是单CPU


也会发生莫名其妙的内存不同步(CPU的高速缓存和主存内容不一致),Intel的新CPU为了性能使用NUMA架构,在线程编程中一定要注意扬长避短。
四、单进程服务器最大并发线程数与内存
很有趣,在默认的ulimit参数下,不修改内核头文件
AS3 512M内存最多1000并发持续连接
CentOS4.3 512M内存最多300并发持续连接
似乎是CentOS不如AS3,这里主要原因是ulimit的配置造成,两个系统默认的配置差距很大,要想单进程维持更多线程接收并发连接,就要尽量缩小 ulimit -s的参数,插更多的内存条,单进程服务器上


2000并发一点都不难,POSIX默认的限制是每进程64线程,但NTPL并非纯正POSIX,不必理会这个限制,2.6内核下真正的限制是内存条的插槽数目(也许还有买内存的钱数)
最近几天的编程中,注意到在32位x86平台上2.6内核单进程创建最大线程数=VIRT上限/stack,与总内存数关系不大,32位x86系统默认的VIRT上限是3G(内存分配的3G+1G方式),默认 stack大小是


10240K,因此单进程创建线程默认上限也就300(3072M / 10240K),用ulimit -s 修改stack到1024K则使上限升到大约3050。我手头没有64位系统,不知道2.6内核在64位上单进程创建线程上限(实际


上是本人懒得在同事的机器上装fc4_x86_64)。
前些天买了一套廉价的64位x86系统(64位赛杨+杂牌915主板),安装了CentOS4.3的x86_64版本,跑了一遍下面的小程序,得到的结果是:在ulimit -s 4096的情况下,单进程最大线程数在16000多一点,用top



VIRT 的上限是64G,也就是36位, cat /proc/cpuinfo的结果是:address sizes : 36 bits physical, 48 bits virtual, 和我想象的标准64位系统不同, 我一直以为64位系统的内存空间也是64位的
附注1:
单位里某BSD FANS用AMD64笔记本跑小程序测试线程创建速度(线程创建后立即phread_detach()然后紧跟着pthread_exit(),共计 100万个线程),同样源码OpenBSD竟然比FreeBSD快了3倍,什么时候


OpenBSD也变得疯狂起来了?
附注2:
测试单进程创建线程上限C源码(test.c):
#include  #include  #include  #include  #include  void * thread_null(void); int main(int argc, char*argv[]) { unsigned int i; int rc; pthread_t pool_id[65536]; //线程ID sleep


(1); //创建线程 for(i = 0; i < 65536; i++) { rc = pthread_create(pool_id + i, 0, (void*)thread_null, NULL); if (0 != rc) { fprintf(stderr, "pthread_create() failure\r\nMax 


pthread num is %d\r\n", i); exit(-1); } } fprintf(stdout, "Max pthread num is 65536\r\nYour system is power_full\r\n"); exit(0); } void * thread_null(void) { pthread_detach


(pthread_self()); sleep(60); pthread_exit(NULL); }
编译:
[root@localhost test]# gcc test.c -o test -lpthread
[root@localhost test]# ulimit -s 10240
[root@localhost test]# ./test 
pthread_create() failure
Max pthread num is 305
[root@localhost test]# ulimit -s 1024 
[root@localhost test]# ./test 
pthread_create() failure
Max pthread num is 3054
以上结果在 CentOS release 5.2 (32Bit)系统上测试得到

你可能感兴趣的:(linux)