SoFunction
Updated on 2025-03-02

In-depth discussion on the maximum number of threads, maximum number of processes, and number of files opened by processes under Linux

==========================================
The maximum number of threads for a single process in a linux system has its maximum limit. PTHREAD_THREADS_MAX
This limitation can be viewed in /usr/include/bits/local_lim.h
The value for linuxthreads is generally 1024, and there is no hard limit for nptl, only limited by system resources.
The resources of this system are mainly the memory occupied by the thread's stack. Use ulimit -s to view the default thread stack size. Generally, this value is 8M
You can write a simple piece of code to verify how many threads can be created at most
Copy the codeThe code is as follows:

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

Test shows, up to 381 threads can be created on linuxthreads, and EAGAIN will be returned after that
Up to 382 threads can be created on nptl, and then ENOMEM will be returned
This value is exactly consistent with the theory, because the process user space under 32-bit Linux is the size of 3G, that is, 3072M. Dividing 3072M by 8M will result in 384. However, in fact, the code segment and data segment will take up some space. This value should be rounded down to 383, and then subtracted from the main thread to get 382.
Then why do you need one less thread on Linuxthreads? This is really true, because linuxthreads also requires a management thread
In order to break through memory limitations, there are two ways to
1) Use ulimit -s 1024 to reduce the default stack size
2) When calling pthread_create, use pthread_attr_getstacksize to set a smaller stack size
It should be noted that even such a person cannot break through the hard limit of 1024 threads. Unless the C library is recompiled <= is worth discussing here. I use ulimit -s 1024 on ubuntu 7.04 + 3G memory, and I can get 3054 threads.
===========================================
Calculate the maximum theoretical number of processes in LINUX:
The local segment description table LDT of each process exists as an independent segment. In the global segment description table GDT, there must be a table entry pointing to the starting address of this segment, and explain the length of the segment and some other parameters. In addition to the above, each process has a TSS structure (task state segment). Therefore, each process must occupy two table entries in the global segment description table GDT. So, how big is the capacity of GDT? The bit segment width used as the subscript of the GDT table in the segment register is 13 bits, so there can be 8192 descriptive terms in GDT. In addition to some system overhead (such as the code segments and data segments of the kernel respectively in GDT, the 4th and 5th items are always used for the code segments and data segments of the current process, the 1th items are always 0, etc.), there are 8180 table entries available, so in theory the largest number of processes in the system is 4090.

====================================================================================�
Use "ulimit -a" to see these limitations, such as:
Copy the codeThe code is as follows:

[root@HQtest root]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 2047
virtual memory (kbytes, -v) unlimited

Use ulimit ?n 10240 to modify the number of open files to become 10240
Although using ulimit ?a can see that it becomes 10240, when I was doing stress tests, when more than 1024 users, the service will be down.
In the end, I can only recompile the kernel, and everything is OK after compiling the kernel!
The operation method is as follows:
Different Linux kernel versions have different adjustment methods.
In Linux kernel 2, you can use the following command to modify:
Copy the codeThe code is as follows:

# echo '8192' > /proc/sys/fs/file-max
# echo '32768' > /proc/sys/fs/inode-max

And add the above command to the /etc// file to enable the above value to be set every time the system restarts.
In Linux kernel 2, you need to modify the original code and then recompile the kernel before it takes effect. Edit the include/linux/ file in the original code of the Linux kernel,
Change NR_FILE from 8192 to 65536, and change NR_RESERVED_FILES from 10 to 128. Edit the fs/ file to change MAX_INODE from 16384 to 262144.
Generally speaking, the maximum number of open files is set to 256 per 4M physical memory, for example, 256M memory can be set to 16384.
The maximum number of i nodes used should be 3 to 4 times the maximum number of open files.