View the maximum number of threads in CentOS

Source: Internet
Author: User

View the maximum number of threads in CentOS

View the maximum number of threads:

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, blocks t 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 command
You can add the command to the profile file or define it in the/etc/security/limits. conf file.
.
Command Parameters
-A: display all limits.
-Maximum size of c core files
-D the maximum data segment size of the process
-F shell: Maximum file size that can be created
-Maximum size of m resident memory
-S maximum stack size
-T the maximum CPU time per second
-P MPs queue size
-N: Maximum number of opened files
-Maximum number of u Processes
-V virtual memory limit
You can also define limits in the/etc/security/limits. conf file.
Domino type item value
Domino is the username or group name starting with the symbol @. * indicates all users. Set "type" to hard or soft. Item refers
Resources to be restricted. Such as cpu, core nproc or maxlogins. Value is the corresponding limit value.

Default Value of System Limit

[Root @ flyinweb ~] # Ulimit-
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 File (-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-
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

In linux, the maximum number of threads for a single process has the maximum limit PTHREAD_THREADS_MAX.

This restriction can be viewed in/usr/include/bits/local_lim.h.

The value of linuxthreads is generally 1024, and there is no hard limit on nptl, only limited by system resources.

The resources of this system are mainly the memory occupied by the thread stack. You can use ulimit-s to view the default thread stack size. Generally, this value is 8 Mb.

You can write a simple code to verify the maximum number of threads that can be created.

  1. Intmain ()
  2. {
  3. Inti = 0;
  4. Pthread_tthread;
  5. While (1 ){
  6. If (pthread_create (& thread, NULL, foo, NULL )! = 0)
  7. Return;
  8. I ++;
  9. Printf ("I = % d \ n", I );
  10. }
  11. }


The test shows that up to 381 threads can be created on linuxthreads, and EAGAIN will be returned

A maximum of 382 threads can be created on nptl, and ENOMEM will be returned.

This value is exactly the same as the theory, because the process user space in 32-bit linux is 3 GB, that is, 3072 M. Divide 3072 M by 8 M to 384, but in fact, code segments and data segments still occupy some space. This value should be rounded down to 383, and then subtracted from the main thread to get 382.

Why is one thread missing on linuxthreads? This is correct, because linuxthreads also needs a management thread

There are two methods to break through the memory limit.

1) Use ulimit-s 1024 to reduce the default stack size
2) use pthread_attr_getstacksize to set a small stack size when calling pthread_create.

It should be noted that, even so, the hard limit of 1024 threads cannot be broken, unless the C library is re-compiled.

Related content:

I. Main differences between 2.4 kernel and 2.6 kernel
On a typical system of the 2.4 kernel (AS3/RH9), the thread is implemented by a lightweight process. Each thread occupies a process ID. In a server program, if a high click rate is used for access, this will cause a table overflow. In order to maintain the overflow table, the system will suspend the service intermittently, the 2.6 kernel will not cause the problem of table overflow caused by the creation and destruction of a large number of threads. 2. The thread stack must be released when the thread ends.
That is to say, the thread function must call pthread_exit () to end, otherwise it will not be released until the main process function exits, especially in the 2.6 kernel environment. The thread creation speed is fast and the memory will be eaten up accidentally immediately, in contrast, the 2.4 kernel environment is better, because the 2.4 kernel creates processes and the thread creation speed is several orders of magnitude slower than the 2.6 kernel. Note: The thread creation speed in 64-bit CPU and 2.6 kernel is even more crazy. If it is too fast, it would be better to pause a little bit of time with usleep (). 3. Do not compile the thread application to be locked.
Only programs that do not need mutex can take advantage of thread programming to the maximum extent, otherwise it will only be slower. The 2.6 kernel is a preemptible kernel, the probability of shared conflicts between threads is much higher than that in the 2.4 kernel environment. Pay special attention to thread security, otherwise, even a single CPU may encounter inexplicable memory non-synchronization (the High-speed cache of the CPU is inconsistent with the content of the primary memory). Intel's new CPU uses the NUMA architecture for performance, in Thread Programming, you must pay attention to weaknesses. Iv. Maximum number of concurrent threads and memory of a single-process server
It is interesting that the kernel header file is not modified under the default ulimit parameter.
AS3 1000 m memory up to concurrent persistent connections
CentOS4.3 300 m memory up to concurrent persistent connections
It seems that CentOS is not as good as AS3. The main reason here is the ulimit configuration. The default configurations of the two systems vary greatly. To maintain a multi-thread concurrent connection for a single process, it is necessary to reduce the ulimit-s parameter as much as possible and insert more memory records. It is not difficult to set 2000 concurrency on a single-process server. The default POSIX limit is 64 threads per process, however, NTPL is not purely POSIX, so you don't have to worry about this restriction. The real limit in the 2.6 kernel is the number of memory slots (and the amount of money to buy memory) in programming in the last few days, note that on the 32-bit x86 platform, the maximum number of threads created by a single process in the 2.6 kernel = VIRT upper limit/stack has little to do with the total number of memories, the default VIRT ceiling for 32-bit x86 systems is 3 GB (3 GB + 1 GB memory allocation), and the default stack size is 10240 kb, therefore, the maximum number of threads created by a single process is 300 (3072 M/10240 K) by default. If you use ulimit-s to modify the stack to 3050 K, the maximum number is increased to about. I don't have a 64-bit system at hand, and I don't know the maximum number of threads that a single process can create on 64-bit kernel 2.6 (in fact, I am too lazy to install fc4_x86_64 on my colleague's machine ). I bought a cheap 64-bit x86 System (64-bit sai Yang + miscellaneous card 915 motherboard) the other day, installed the x86_64 version of CentOS4.3, and ran the following small program, the result is: in the case of ulimit-s 4096, the maximum number of threads of a single process is more than 16000.
The maximum VIRT value is 64 GB, that is, 36 bits. The result of cat/proc/cpuinfo is: address sizes: 36 bits physical, 48 bits virtual, unlike the standard 64-bit system, I always thought that the memory space of the 64-bit system is also 64-bit.
Note 1:
In the unit, a bsd fans uses the AMD64 notebook to run the applet to test the thread creation speed (phread_detach () immediately after the thread is created, followed by pthread_exit (), a total of 1 million threads ), similarly, OpenBSD is three times faster than FreeBSD. When will OpenBSD become crazy? NOTE 2:
Test Single Process Creation thread ceiling C source code (test. c ):
  1. # Include <stdio. h>
  2. # Include <string. h>
  3. # Include <stdlib. h>
  4. # Include <unistd. h>
  5. # Include <pthread. h>
  6. Void * thread_null (void );
  7. Intmain (intargc, char * argv [])
  8. {
  9. Unsignedinti;
  10. Intrc;
  11. Pthread_tpool_id [65536]; // thread ID
  12. Sleep (1 );
  13. // Create a thread
  14. For (I = 0; I <65536; I ++ ){
  15. Rc = pthread_create (pool_id + I, 0, (void *) thread_null, NULL );
  16. If (0! = Rc ){
  17. Fprintf (stderr, "pthread_create () failure \ r \ nMaxpthreadnumis % d \ r \ n", I );
  18. Exit (-1 );
  19. }
  20. }
  21. Fprintf (stdout, "Maxpthreadnumis65536 \ r \ nYoursystemispower_full \ r \ n ");
  22. Exit (0 );
  23. }
  24. Void * thread_null (void)
  25. {
  26. Pthread_detach (pthread_self ());
  27. Sleep (60 );
  28. Pthread_exit (NULL );
  29. }

Compile:

[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

The above results are tested on the CentOS release 5.2 (32Bit) system.

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.