免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5059 | 回复: 13
打印 上一主题 下一主题

[C] 多次fork出现资源不足? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-12-29 15:38 |只看该作者 |倒序浏览

unsigned int gchild = 0;
extern   int errno;

static void
sigchild(int signo)
{
    if (signo == SIGCHLD)
    {
        gchild++;
        printf("child %d exit pid : %d\n", gchild, wait(NULL));
    }
    else
    {
        printf("unknown signal : %d\n", signo);
    }
}

int
main(void)
{
    pid_t pid;
    int i;
    if (signal(SIGCHLD, sigchild) == SIG_ERR)
    {
        printf("can't catch sigchild\n");
        exit(1);
    }

&nbsp;&nbsp;&nbsp;&nbsp;for (i=0; i<100000000; i++)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ((pid = fork()) < 0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("fork error : %s\n", strerror(errno));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(1);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if (pid == 0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(0);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;sleep(4);
&nbsp;&nbsp;&nbsp;&nbsp;exit(0);
}

我写了上面的小段代码用来测试僵尸进程与信号,
在执行到 73691 次时, fork 失败, 错误原因为:
Resource temporarily unavailable, 使用
ps -o pid,ppid,command,tty,state
没有发现僵尸,为什么呢?我只是fork,fork后马上退出为什么会资源不足??

论坛徽章:
0
2 [报告]
发表于 2007-12-29 15:43 |只看该作者
系统进程数目是有限制的。
另外,你创建的子进程退出后,父进程并没有为其收尸

论坛徽章:
0
3 [报告]
发表于 2007-12-29 15:48 |只看该作者
原帖由 G00GLE 于 2007-12-29 15:43 发表
系统进程数目是有限制的。
另外,你创建的子进程退出后,父进程并没有为其收尸

wait(NULL)已经收了,抗议!
进程数的限制是并发数还是总数?
我看apue的时候书上是讲了子进程有数目限制,可是我再去看找不到在哪里了。。。

论坛徽章:
0
4 [报告]
发表于 2007-12-29 15:51 |只看该作者
原帖由 我learnc 于 2007-12-29 15:48 发表

wait(NULL)已经收了,抗议!
进程数的限制是并发数还是总数?
我看apue的时候书上是讲了子进程有数目限制,可是我再去看找不到在哪里了。。。


不好意思,没看见你在信号处理函数那里wait了

论坛徽章:
0
5 [报告]
发表于 2007-12-29 16:15 |只看该作者
你那样wait很有可能漏wait了一些子进程。应该用
while ( waitpid(...) != -1 );
waitpid里应该填啥自个儿查书去

论坛徽章:
0
6 [报告]
发表于 2007-12-29 16:23 |只看该作者
原帖由 beilian 于 2007-12-29 16:15 发表
你那样wait很有可能漏wait了一些子进程。应该用
while ( waitpid(...) != -1 );
waitpid里应该填啥自个儿查书去


我用ps -o 查了,没漏,唉,你们看贴都不认真。waitpid要打的字多,而且要pid, 你让我怎么传进去?搞个数组么?

论坛徽章:
0
7 [报告]
发表于 2007-12-29 16:27 |只看该作者
原帖由 我learnc 于 2007-12-29 16:23 发表


我用ps -o 查了,没漏,唉,你们看贴都不认真。waitpid要打的字多,而且要pid, 你让我怎么传进去?搞个数组么?

waitpid也可以不用pid。好好去看看apue2吧

论坛徽章:
0
8 [报告]
发表于 2007-12-29 16:57 |只看该作者
你清楚的while结构中的waitpid在我写的程序里不用pid会有什么结果么?我在看apue,我也把程序改后试过了,问题依旧,而且fork483个的时候就报Resource temporarily unavailable, 使用waitpid(1, NULL, 0)。
谢谢你的回复,不过下次让我改程序请说明原因吧,也许你很牛X,可是你只说这样做那样做,我还是会有很多疑问的。

论坛徽章:
38
2017金鸡报晓
日期:2017-02-08 10:39:4215-16赛季CBA联赛之深圳
日期:2023-02-16 14:39:0220周年集字徽章-年
日期:2022-08-31 14:25:28黑曼巴
日期:2022-08-17 18:57:0919周年集字徽章-年
日期:2022-04-25 13:02:5920周年集字徽章-20	
日期:2022-03-29 11:10:4620周年集字徽章-年
日期:2022-03-14 22:35:1820周年集字徽章-周	
日期:2022-03-09 12:51:3220周年集字徽章-年
日期:2022-02-10 13:13:4420周年集字徽章-周	
日期:2022-02-03 12:09:4420周年集字徽章-20	
日期:2022-01-25 20:14:2720周年集字徽章-周	
日期:2022-01-13 15:12:33
9 [报告]
发表于 2007-12-29 17:00 |只看该作者
fork太快了?有速度限制的。

论坛徽章:
0
10 [报告]
发表于 2007-12-29 17:06 |只看该作者
while (waitpid(-1, &status, WNOHANG) > 0)

The wpid argument specifies the set of child processes for which to wait.
If wpid is -1, the call waits for any child process
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP