how many processes can be created with python os.fork()

余生颓废 提交于 2021-01-24 07:46:24

问题


If I do this below:

for i in range(10000):
    os.fork()

What's going on? don't consider reaping.. I just wanna know about os.fork() can make how much processes in linux, if it 's just like windows, only can make about 2000 processes, how the next 8000 processes will do?
Thanks.


回答1:


os.fork() spawns a new OS-level process. Any limits on the number of those aren't dependent on Python, but on the operating system. According to this previous question, on Linux you can find out about any software-imposed constraints on that by looking at:

cat /proc/sys/kernel/pid_max

but that might be further restrained by /etc/security/limits.conf. If those don't hit you first, you will eventually run into problems with available hardware resources - your code is a fork bomb, and type of trivial denial of service attack (any software limits on the number of processes are set to avoid this kind of attack).




回答2:


The code doesn't do what you think it does.

When you're forking, you're actually duplicating the current process.

This means that after forking, both processes will continue the iteration from the same point. And then both will fork, and so on... The number of created threads will be exponential.

Example:

import os
for i in range(3):
    os.fork();

print "T"

When executed, you should see 2^3 = 8 processes.

Just to return to your original question, since fork creates processes, the limit on the number of threads is irrelevant.




回答3:


The os.fork() function creates a copy of the calling process. Threads are created in Python by using the threading module.

The amount of processes you can create on a UNIX-like system such as Linux is generally limited by the amount of memory the computer has and certain limits set in the operating system. The output of the command ulimit -a shows all the limits.

The amount of threads you can create within a process is usually limited by the amount of stack space that the process has and the maximal amount of (virtua)l memory that the process is allowed to have. These limits are also shown by ulimit.

Note that when you are using the standard python implementation (a.k.a CPython), only one thread at a time can be executing Python bytecode.



来源:https://stackoverflow.com/questions/20452150/how-many-processes-can-be-created-with-python-os-fork

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!