产生大量进程

Spawning big amount of processes

我想生成大量进程。所以我有 master 进程来完成它。

int master(int argc, char* argv[]){
    for (int i = 0; i < 50000; ++i) {
        std::string name = std::to_string(i);
        MSG_process_create(name.c_str(), slave, NULL, MSG_host_self());
    }
    return 0;
    }

int slave(int argc, char* argv[]){
    XBT_INFO("%s", MSG_process_get_name(MSG_process_self()));
    return 0;
}

启动此程序后,我得到以下输出:

....
....
[Master:32734:(32736) 0.000000] [master/INFO] 32734
[Master:32735:(32737) 0.000000] [master/INFO] 32735
[0.000000] /home/ken/Downloads/simgrid-master/src/simix/smx_context.cpp:187: [xbt/CRITICAL] Failed to protect stack: Cannot allocate memory

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

然后我被建议使用 contexts/stack-size 参数来更改堆栈大小,因为之前的程序默认需要 50000 * 8192 KiB。

我添加了这个参数 --cfg=contexts/stack-size:10 但我有相同的输出:

...
...
[Master:32735:(32737) 0.000000] [master/INFO] 32735
[0.000000] /home/ken/Downloads/simgrid-master/src/simix/smx_context.cpp:187: [xbt/CRITICAL] Failed to protect stack: Cannot allocate memory

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

--cfg=contexts/stack-size:100000:

...
...
[Master:32734:(32736) 0.000000] [master/INFO] 32734
[0.000000] /home/ken/Downloads/simgrid-master/src/simix/smx_context.cpp:187: [xbt/CRITICAL] Failed to protect stack: Cannot allocate memory

我的程序似乎没有看到这个参数,但事实并非如此,因为堆栈参数是 5 给我:

Finally, if nothing of the above applies, this can result from a stack overflow.
Try to increase stack size with --cfg=contexts/stack_size (current size is 1 KiB).

我做错了什么?

您能否尝试增加系统上每个进程允许的最大映射数的值?

您可以使用 sudo sysctl -w vm.max_map_count=500000 将最大值设置为 500000

我们最近看到这在某些 SMPI 运行中引起了一些问题,也许您的情况也是如此。 "Cannot allocate memory" 消息可能确实具有误导性,因为出于各种原因设置了 ENOMEM 错误代码(并且根据 http://man7.org/linux/man-pages/man2/mprotect.2.html,其中之一可能是映射数)。