Linux上最大子进程数 [英] Maximum number of children processes on Linux

查看:370
本文介绍了Linux上最大子进程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码将产生尽可能多的子代.自己不会再分叉了,一旦父进程退出,它们就会变成僵尸.
父进程将生成多少个子进程?

The code below will spawn as many children as possible. Themselves won't fork further, and will become zombies once the parent process exits.
How many children processes will the parent process spawn ?

int main(int argc, char *arg[])
{
     while(fork() > 0);
}

推荐答案

子进程的数量可以通过 fork(2)可能由于以下几个原因而失败.您可以使用内置的bash ulimit设置该限制.

The number of child processes can be limited with setrlimit(2) using RLIMIT_NPROC. Notice that fork(2) can fail for several reasons. You could use bash builtin ulimit to set that limit.

您可以使用getrlimit(或解析/proc/self/limits,请参见 proc(5))来获取该信息.

You can use getrlimit (or parse /proc/self/limits, see proc(5)) to get that information.

在整个系统范围内,由于以下原因,您可能会使用/proc/sys/kernel/threads-max:

System-wide, you might use /proc/sys/kernel/threads-max since:

此文件指定系统范围内的线程数限制 (任务)可以在系统上创建.

This file specifies the system-wide limit on the number of threads (tasks) that can be created on the system.

还有/proc/sys/kernel/pid_max

此文件指定PID环绕的值(即 此文件中的值比最大PID大1). PID 大于此值的不分配;因此,在此的价值 文件还充当系统范围内对文件总数的限制 进程和线程.该文件的默认值32768, 产生的PID范围与早期内核相同.在32位上 在平台上,pid_max的最大值为32768.在64位上 系统,pid_max可以设置为2 ^ 22(PID_MAX_LIMIT, 大约四百万).

This file specifies the value at which PIDs wrap around (i.e., the value in this file is one greater than the maximum PID). PIDs greater than this value are not allocated; thus, the value in this file also acts as a system-wide limit on the total number of processes and threads. The default value for this file, 32768, results in the same range of PIDs as on earlier kernels. On 32-bit platforms, 32768 is the maximum value for pid_max. On 64-bit systems, pid_max can be set to any value up to 2^22 (PID_MAX_LIMIT, approximately 4 million).

但是,可能还有其他限制(尤其是交换空间).

However, there could be other limitations (notably swap space).

内核任务要么是单线程进程,要么是某个进程中的某个线程-例如由低级系统调用 clone(2)创建(或一些内核线程,例如kworkerksoftirqd等).

A task for the kernel is either a single-threaded process or some thread inside some process - e.g. created by low-level syscall clone(2) (or some kernel thread like kworker, ksoftirqd etc...).

顺便说一句,实际的过程数量更多地受到可用资源的限制.一个典型的Linux桌面只有几百个(现在,我的具有32Gb RAM和i5-4690S的Debian/x86-64桌面有227个进程).因此,进程是一个非常昂贵的资源(它需要RAM,它需要CPU ...).如果它们太多,您会遇到崩溃.实际上,您不希望有太多的可运行进程或可调度的任务(最多可能只有几十个,也许每个

BTW, the practical number of processes is much more limited by available resources. A typical Linux desktop has only a few hundreds of them (right now, my Debian/x86-64 desktop with 32Gb RAM & i5-4690S has 227 processes). So a process is a quite expensive resource (it needs RAM, it needs CPU...). If you have too many of them you'll experience thrashing. And in practice, you don't want to have too many runnable processes or schedulable tasks (probably only a few dozens of them at most, perhaps no more than a few per core).

这篇关于Linux上最大子进程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆