测试系统:centos 64位
首先提供测试程序,改写自linux程序设计的例子
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <pthread.h> void *thread_function(void *arg); char message[] = "Hello World"; int main() { int res; pthread_t a_thread; void *thread_result; int thread_num = 0; while(1) { res = pthread_create(&a_thread, NULL, thread_function, (void *)message); if (res != 0) { perror("Thread creation failed"); break; } thread_num++; } printf("thread_num = %d\n", thread_num); sleep(1000); return -1; } void *thread_function(void *arg) { // printf("thread_function is running. Argument was %s\n", (char *)arg); sleep(1000); pthread_exit("Thank you for the CPU time"); }
例子中打印最大可以开启的线程数
运行该程序,得到如下结果
[zy@localhost ~]$ 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) 7761 max locked memory (kbytes, -l) 64 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) 1024 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited [zy@localhost ~]$ ps aux | grep zy zy 2421 0.0 0.4 218232 4616 ? S 06:15 0:01 smbd -D root 2440 0.0 0.3 99948 3936 ? S 06:19 0:00 sshd: zy [priv] zy 2459 0.0 0.1 99948 1744 ? S 06:19 0:00 sshd: zy@pts/0 zy 2460 0.0 0.1 108524 1968 pts/0 Ss+ 06:19 0:00 -bash root 5791 0.0 0.3 99948 3940 ? S 06:22 0:00 sshd: zy [priv] zy 5794 0.0 0.1 99948 1728 ? S 06:22 0:00 sshd: zy@pts/1 zy 5795 0.0 0.1 108524 1956 pts/1 Ss+ 06:22 0:00 -bash root 20533 0.0 0.3 99948 3940 ? S 07:57 0:00 sshd: zy [priv] zy 20535 0.0 0.1 99948 1728 ? S 07:57 0:00 sshd: zy@pts/3 zy 20536 0.0 0.1 108520 1900 pts/3 Ss 07:57 0:00 -bash root 20635 0.0 0.1 161096 1920 pts/3 S 08:05 0:00 su zy zy 20637 0.0 0.1 108524 1952 pts/3 S 08:05 0:00 bash zy 22779 1.0 0.1 110288 1144 pts/3 R+ 08:08 0:00 ps aux zy 22780 0.0 0.0 103300 848 pts/3 S+ 08:08 0:00 grep zy [zy@localhost ~]$ ./main Thread creation failed: Resource temporarily unavailable thread_num = 1015 ^C [zy@localhost ~]$ ulimit -u 500 [zy@localhost ~]$ ./main Thread creation failed: Resource temporarily unavailable thread_num = 491从图中可以看到最大线程数由ulimit决定,当然这里使用的是64位系统,如果使用32位系统,该问题还与内存地址空间有关系。详细可以参考如下文章: Linux系统最大进程数和单进程最大线程数
此外,注意的是最大线程数的限制仅用于单个用户,如果系统存在多个用户,那么每个用户创建的线程数(进程数)是有用户的ulimit配置决定大的,就是说不同用户是相互独立的
但是如果切换到root用户,明显发现线程数量不受ulimit的限制,如下:
[root@localhost zy]# 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) 7761 max locked memory (kbytes, -l) 64 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) 1024 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited [root@localhost zy]# ./main Thread creation failed: Resource temporarily unavailable thread_num = 15300