要是我是想不到这个问题的, 但现在软件遇到了, 我不得不去面对.
1>. HP-UX, AIX, Linux下单一进程下最大可以创建多少个线程呢?
这个问题不怎么好回答, 先来写个小的测试一下:
#include
#include
#include
#include
#include
#include
static void *thr_func(void *argv)
{
while (1)
sleep(1);
return (NULL);
}
int main(int argc, char **argv)
{
int t;
pthread_t thrid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_attr_setstacksize(&attr, 2048*1024);
for (t = 0; ; t++)
{
if (pthread_create(&thrid, &attr, thr_func, NULL) != 0)
{
fprintf(stderr, "create thread error[%d]: %s\n"
, t, strerror(errno));
exit(1);
}
}
return (0);
}
我测试的环境(AS4U3):
[gan@localhost proc]$ cat meminfo
MemTotal: 507924 kB
MemFree: 19948 kB
......
[gan@localhost tmp]$ uname -a
Linux localhost.localdomain 2.6.9-34.EL #1 Fri Feb 24 16:44:51 EST 2006 i686 i686 i386 GNU/Linux
[gan@localhost tmp]$ gcc -v
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.5/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux
Thread model: posix
gcc version 3.4.5 20051201 (Red Hat 3.4.5-2)
[gan@localhost tmp]$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
pending signals (-i) 1024
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
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 8063
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[gan@localhost tmp]$ cat /proc/sys/kernel/threads-max
16126
普通用户运行结果:
[gan@localhost tmp]$ gcc -Wall -lpthread -O2 thr.c
[gan@localhost tmp]$ ./a.out
create thread error[1529]: Cannot allocate memory
看来在该平台下这样的环境可以创建1529个
在网上找了一下没找到一个统一的答案, 只说创建最大线程个数跟这些因数有关[具体怎样我也不太清楚]:
线程栈大小有关, 当线程栈太大那么最大线程数将会小些.
系统核心参数配置有关, 如Linux下的/proc/sys/kernel/threads-max中的数据.
与系统物理内存大小有关.
现在来修改一下系统的相关参数测试一下:
1>. 使用root用户来运行:
结果同不通用户结果一样.
2>. 将代码中:
pthread_attr_setstacksize(&attr, 2048*1024);
注释掉.
结果为:
[gan@localhost tmp]$ a.out
create thread error[305]: Cannot allocate memory
怎么会变小了呢? 系统使用默认栈大小10M了吗?
2.1>. 将10M修改小些看看:
[gan@localhost tmp]$ ulimit -s 1024
[gan@localhost tmp]$ ulimit -s
1024
[gan@localhost tmp]$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
pending signals (-i) 1024
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
stack size (kbytes, -s) 1024
cpu time (seconds, -t) unlimited
max user processes (-u) 8063
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[gan@localhost tmp]$ a.out
create thread error[3054]: Cannot allocate memory
结果是多了很多噢!
再修改小点:
[gan@localhost tmp]$ ulimit -s 10
[gan@localhost tmp]$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
pending signals (-i) 1024
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
stack size (kbytes, -s) 10
cpu time (seconds, -t) unlimited
max user processes (-u) 8063
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[gan@localhost tmp]$ a.out
create thread error[8009]: Resource temporarily unavailable
看到8009同"max user processes (-u) 8063"这个很像阿, 是不是最大进程数限制了该值呢?把这个值也修改大些看看结果.
[gan@localhost tmp]$ ulimit -u 100000
-bash: ulimit: max user processes: cannot modify limit: 不允许的操作
[gan@localhost tmp]$ su
Password:
[root@localhost tmp]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
pending signals (-i) 1024
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
stack size (kbytes, -s) 10
cpu time (seconds, -t) unlimited
max user processes (-u) 8063
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[root@localhost tmp]# ulimit -u 100000
[root@localhost tmp]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
pending signals (-i) 1024
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
stack size (kbytes, -s) 10
cpu time (seconds, -t) unlimited
max user processes (-u) 100000
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[root@localhost tmp]# a.out
create thread error[16011]: Resource temporarily unavailable
这下是很大了. 与/proc/sys/kernel/threads-max中的16126很近了. 看看不用root用户是不是也可以达到这么大呢?
把threads-max给修改了结果又是怎样呢?
[root@localhost tmp]# echo 10000 > /proc/sys/kernel/threads-max
[root@localhost tmp]# cat /proc/sys/kernel/threads-max
10000
[root@localhost tmp]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
pending signals (-i) 1024
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
stack size (kbytes, -s) 10
cpu time (seconds, -t) unlimited
max user processes (-u) 8063
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[root@localhost tmp]# a.out
create thread error[9886]: Resource temporarily unavailable
奇怪了要是与最大进程数有关那么就不应该大于8063阿? 不明白.
写了那么多我还是没太真正的搞明白, 只是可以说与哪些参数有关, 但具体什么关系还是没懂. 由于HP, AIX没有环境就没办法测试了.
HP-UX中与max_thread_proc参数有关, 可以使用mktune命令来修改, 修改后机器重新启动后就好了. 具体怎么使用kmtune那就自己查看man了.
$ whereis kmtune
kmtune: /usr/sbin/kmtune /usr/share/man/man1m.Z/kmtune.1m
$ /usr/sbin/kmtune | grep thread
max_thread_proc 256 - 256
nkthread 7184 - 7184
阅读(3990) | 评论(0) | 转发(0) |