Linux creates processes up to 65535

  • 2020-12-07 04:37:02
  • OfStack

Linux creation process is said to consume very little. This 1 is a characteristic of Linux, so we specially test the limit of Linux creation process. The test code is as 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 is constantly creating child processes. The default is to create 65,535 processes. If it cannot be created, an error is printed.

The test code is a new blue account I created to run the test. In order to create as many processes as possible, I first set a hard limit on the number of blue account creation processes

Use root account change/etc/security/limits conf file, add 1 downside:

blue             hard    nproc          65535

Then set the soft limit for the creation process for the blue account and execute the following code at the terminal:

ulimit -u 65535

Here, though we set the number of blue account creation process hard limit and the soft limit is 65535, but we can't use blue account created 65535 process, we also need to set up the kernel parameters in Linux kernel. pid_max, this parameter I default installation is 32768, so even if use root account, but don't set the kernel parameters, the process of the whole system can create most number is 32768, so we need the following Settings:

sysctl -w  kernel.pid_max=65535

Note: If you are using the root account to execute the program, you do not need to set the hard and soft limits of resources, but you still need to set the kernel parameters of the process pid maximum to create 65535 processes.

Then switch to blue account at same terminal 1:

su blue

Compile and execute the test code:


gcc fork.c -o fork
./fork

My virtual machine Linux M memory is 512, in the process to create more than 6000, the program runs slowly, by observing vmstat command found swap memory into a very frequent, judging is due to insufficient memory, using virtual memory, leading to frequent IO operation, make the test code is slow, so creating too much process, one particular aspect of a system of memory is an important measure.

Later, I put the test code to a well-configured xEON server for testing. The memory was 8G. When I created nearly 4W processes, the program ran to the bottleneck, which was still the bottleneck of memory.

The child process created in the test code takes up a relatively small amount of memory. In actual use, it is only possible to use more memory than the process created in the test code, so correspondingly, the machine with the same configuration should be able to create fewer available processes.

conclusion


Related articles: