The number of processes created in Linux reaches 65535.

Source: Internet
Author: User

Creating processes on Linux is said to consume a small amount of time. This has always been a feature of Linux, so it specifically tests the limits of creating processes on Linux and tests the process.CodeAs follows:

 //  Fork. c  
# Include <unistd. h>
# Include <stdlib. h>
# Include <stdio. h>
# Include < String . H>
# Define # Maxprocess 65535
# Define Sleeptime 60
Int Main ( Int Argc, Char ** Argv ){
Pid_t PID;
Int Count = 0 ;
Int Maxprocess = maxprocess;
If (Argc = 2 ){
Maxprocess = atoi (argv [ 1 ]);
}
For (COUNT = 0 ; Count <maxprocess; count ++ ){
PID = fork ();
If (PID < 0 ){
Perror ( " Fork Error " );
Exit ( 1 );
} Else If (Pid = 0 ){
Printf ( " Child % d start \ n " , Count );
Sleep (sleeptime );
Printf ( " Child % d end \ n " , Count );
Exit ( 0 );
}
Printf ( " Parent: Create % d child \ n " , Count );
}

For (COUNT = 0 ; Count <maxprocess; count ++ ){
Wait ();
}
Exit ( 0 );

}

This code keeps creating sub-processes. By default, it creates 65535 processes. If the sub-processes cannot be created, an error is printed.

The test code is a blue account I created to run the test. To allow the process to create as many processes as possible, you must first set a hard limit on the number of processes created for the blue account.

Use the root account to modify the/etc/security/limits. conf file and add the following lines:

 
Blue hard nproc65535

Then, set the soft restrictions for the creation process for the blue account and execute the following code on the terminal:

 
Ulimit-u65535

At this point, although we set the hard and soft limits of the number of blue Account creation processes to 65535, we still cannot use the blue account to create 65535 processes, we also need to set Kernel Parameters in Linux. pid_max. this parameter is installed on 32768 by default. Therefore, even if the root account is used, this kernel parameter is not set. The maximum number of processes that can be created by the entire system is 32768, therefore, we need to make the following settings:

 
Sysctl-W kernel. pid_max =65535

Note: If the root account is used for executionProgramYou do not need to set hard and soft limits on resources, but you still need to set the Kernel Parameter of the maximum process PID value to create 65535 processes.

Switch to Blue account on the same terminal:

 
Su blue

Compile and execute the test code:

 
GCC fork. C-o fork
./Fork

My VM Linux memory is 512 MB. When I created more than processes, the program ran very slowly. Through the vmstat command, I found that swap memory was frequently placed, it can be determined that the virtual memory is insufficient, resulting in frequent Io operations, making the test code very slow. Therefore, when too many processes are created, the system memory is an important measure.

Later, I put the test code to a well-configured Xeon server for testing. The memory is 8 GB. When nearly processes are created, the program runs to a bottleneck and the memory bottleneck remains.

In the test code, the created sub-process occupies a relatively small amount of memory. In actual use, it may only use more memory than the process created in the test code, for machines with the same configuration, fewer available processes can be created.

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.