Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create a process in Linux to reach 65535

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to create a process of 65535 in Linux. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

/ / fork.c#include # define MAXPROCESS 65535#define SLEEPTIME 60int 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, and the default is to create 65535 processes, and if it cannot be created, it prints out an error.

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

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

Blue hard nproc 65535

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

Ulimit-u 65535

At this point, although we set the hard limit and soft limit for the number of processes created by the blue account is 65535, we cannot use the blue account to create 65535 processes. We also need to set the kernel parameter kernel.pid_max in Linux, which I install by default is 32768, so even if we use the root account without setting this kernel parameter, the maximum number of processes that can be created by the whole system is 32768. So we need to make 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 hard and soft limits on resources, but you still need to set the kernel parameter of the maximum process pid before you can create 65535 processes.

Then switch to the blue account at the same terminal:

Su blue

Compile and execute the test code:

Gcc fork.c-o fork./fork

My virtual machine Linux memory is 512m. When creating more than 6000 processes, the program runs very slowly. Through the observation of the vmstat command, we find that the swap memory is placed and set out very frequently. It can be judged that it is due to the lack of memory and the use of virtual memory, which leads to frequent IO operations, which makes the test code very slow. So when creating too many processes, the memory of the system is an important aspect to measure.

Later, I put the test code on a well-configured strong server to test, the memory is 8G, when creating nearly 4W processes, the running of the program also reached the bottleneck, which is still the bottleneck of memory.

In the test code, the child processes created take up quite a small amount of memory, and in practice, it is only possible to use more memory than the processes created by the test code, so accordingly, machines with the same configuration, there should be fewer available processes that can be created.

What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

The above is the editor for you to share how to create a process in Linux reached 65535, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report