太多孩子用fork() [英] Too many child with fork()

查看:73
本文介绍了太多孩子用fork()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

for ( ii = 0; ii < 24; ++ii) {

    switch (fork()) {

        case -1 : {
                printf("\n\nproblem with fork() !!! \n\n");
                exit(0);
                };

        case 0 : {
                WriteOnShared_Mem(ii);
                }break;
        default : {
                ChildPidTab[ii] = p;
                usleep(50000);
                ReadShared_MemMp(nbSect, 24,ChildPidTab);
                };
    }
}

我的问题是我生了太多孩子(nbenfant = 24),我的孩子超过了24岁:/

My problem is that i get too many child (nbenfant = 24), i got much more than 24 :/

这是我今天在这里的第三篇文章,但仍未解决:(

This is my 3rd post today here but still not solved :(

谢谢

推荐答案

仔细阅读 叉子(系统调用)进程(计算).

Read carefully the fork(2) man page. Read that page several times, it is hard to understand. Read also the wikipage on fork (system call) and on processes (computing).

请理解-并且花费时间-成功时fork同时返回两次:一次在父级中,一次在子级中

Please understand -and that takes time- that fork is returning simultaneously twice on success: once in the parent and once in the child

fork系统调用可能由于多种原因而失败(然后返回-1).如果fork失败,请使用perror或其他方式显示errno.并且您应始终保留fork的结果.这样的代码

The fork syscall can fail (and then returns -1) for a number of reasons. On failure of fork please use perror or some other way to show the errno. And you should always keep the result of fork. So code

for (ii = 0; ii < 24; ++ii) {
 fflush(NULL);
 pid_t p = fork();
 switch (p) {
    case -1 : // fork failed 
            printf("\n\nproblem with fork() in pid %d error %s!!! \n\n",
                   (int) getpid(), strerror(errno));
            exit(EXIT_FAILURE);
            break;
    case 0: // child process
            WriteOnShared_Mem(ii);
            ii = MAX_INT; // to stop the for loop
            break;
    default: // parent process
            ChildPidTab[ii] = p;
            /// etc.... some synchronization is needed
            break;
    }

尤其是fork可能会因为

   EAGAIN fork() cannot allocate sufficient memory to copy the
          parent's page tables and allocate a task structure for 
          the child.
   EAGAIN It was not possible to create a new process because the
          caller's RLIMIT_NPROC resource limit was encountered.  To
          exceed this limit, the process must have either the
          CAP_SYS_ADMIN or the CAP_SYS_RESOURCE capability.
   ENOMEM fork() failed to allocate the necessary kernel structures
          because memory is tight.

如果您希望能够派生更多进程,请尝试:

If you want to be able to fork more processes, try to:

  • 使用 setrlimit( 2)(系统设施可能会调用它,因此也请查看/etc/pam.d/login

  • increase the RLIMIT_NPROC resource limit with setrlimit(2) (which might be called by system facilities, so look also into /etc/pam.d/login etc

降低fork -ing程序所需的资源.特别是,降低堆内存需求

lower the resources required by the fork-ing program. In particular, lower the heap memory requirements

增加一些系统资源,例如交换.您可以swapon一些临时文件进行测试.

increase some system resources, like perhaps swap. You could swapon some temporary file for testing.

正如 Joachim Pileborg答复,您应避免分叉过多(分叉的过程继续循环,因此也再次分叉)

As Joachim Pileborg replied you should avoid forking too much (the forked process continues the loop so is also forking again).

请不要忘记stdio例程已被缓冲.适当使用 fflush(3).

Don't forget that stdio routines are buffered. Use fflush(3) appropriately.

我建议您阅读高级Linux编程本书(可在线获得),该书整章介绍了Linux上的进程处理.

I suggest reading the Advanced Linux Programming book (available online) which has a full chapter explaining process handling on Linux.

顺便说一句,用pstoppstree检查您有多少个进程(以及使用free命令使用了多少内存,但是请阅读

BTW, check with ps or top or pstree how many processes you have (and with the free command how much memory is used, but read http://linuxatemyram.com/ before complaining). It could happen that your particular system is not able to fork more than 24 times your particular program (because of lack of resources)

还要研究简单shell的源代码(例如sash),并使用strace -f(例如在某些shell上或在您的程序上)来了解更多的系统调用.另请学习如何使用gdb调试器.

Study also the source code of simple shells (like sash) and use strace -f (e.g. on some shell, or on your program) to understand more what syscalls are done. Also learn how to use the gdb debugger.

这篇关于太多孩子用fork()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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