Drill down to the maximum number of threads, process maximum, and number of files open by process in Linux _c language

Source: Internet
Author: User
Maximum number of ===== threads = =
The maximum number of threads for a single process in a Linux system has its maximum limit Pthread_threads_max
This restriction can be viewed in/usr/include/bits/local_lim.h
For linuxthreads This value is generally 1024, there is no hard limit for NPTL, only limited by the resources of the system
The resource of this system is mainly the memory occupied by the thread stack, with ulimit-s can see the default thread stack size, in general, this value is 8M
Can write a simple code to verify the maximum number of threads that can be created
Copy Code code 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, you can create up to 381 threads on the linuxthreads and then return to Eagain
You can create up to 382 threads on NPTL and then return Enomem
This value is completely consistent with the theory, because the 32-bit Linux process user space is 3G size, that is, 3072M, with 3072M divided by 8M 384, but in fact, code snippets and data segments, etc. to occupy some space, this value should be taken down to 383, minus the main thread, get 382 。
Why is there one less thread on the linuxthreads? That's right, because Linuxthreads also needs a management thread
to break the memory limit, there are two ways to
1) using ulimit-s 1024 to reduce the default stack size
2) when calling Pthread_create, use Pthread_attr_getstacksize to set a smaller stack size
Note that even if this does not break the hard limit of 1024 threads, unless recompiling C library <= is worth discussing here, I can get 3,054 threads with ulimit-s 1024 on Ubuntu 7.04+3g memory.
Maximum number of =============== processes =================
The maximum number of theoretical calculations for processes in Linux:
Each process's local segment describes the table Ldt as a separate segment, and in the Global Segment Description table GDT there is a table entry that points to the starting address of the segment and describes the length of the segment and some other parameters. In addition to the above, each process also has a TSS structure (task status segment) as well. Therefore, each process occupies two table entries in the Global Segment Description table GDT. So, how big is the capacity of the GDT? The segment registers are used as the GDT of the table under the 13-bit width, so GDT can have 8,192 descriptors. In addition to some system overhead (such as the 2nd and 3rd items in GDT for the kernel's code snippets and data segments, the 4th and 5th items are always used for the code snippets and data segments of the current process, and the 1th item is always 0, and so on, and 8,180 table entries are available, so the theoretical maximum number of processes in the system is 4090.

= = = Recompile the kernel to modify the maximum number of files opened by the process and modify the Listen listening queue = =
You can see these limitations with "ulimit-a", such as:
Copy Code code 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 (bytes, p) 8
Stack size (Kbytes,-s) 8192
CPU time (seconds,-t) unlimited
MAX User Processes (-u) 2047
Virtual Memory (Kbytes,-V) Unlimited

Change the number of open files with Ulimit n 10240 to 10240
Although the use of ulimit a can be seen to become 10240, but when I do stress testing, when more than 1024 users, the service will be down machine.
Finally, only recompile the kernel, compile the kernel after all ok!
The operation method is as follows:
Different Linux kernel versions have different tuning methods,
The following commands can be used to modify the Linux kernel 2.2.x:
Copy Code code as follows:

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

and add the above command to the/etc/rc.c/rc.local file so that the system sets the above values each time it restarts.
In the Linux kernel 2.4.x, it is necessary to modify the source code and recompile the kernel before it takes effect. Edit the Include/linux/fs.h file in the Linux kernel source code,
Change the nr_file from 8192 to 65536, and change the nr_reserved_files from 10 to 128. Edit the fs/inode.c file to change the Max_inode from 16384 to 262144.
In general, the maximum number of open files is more reasonable set to 4 m physical memory 256, such as 256M memory can be set to 16384,
The maximum number of I nodes to use should be 3 times to 4 times times the maximum number of open files.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.