首页 新闻 会员 周边 捐助

怎么限制Fork的数量?

0
[待解决问题]

本来搞个g_iChildProcessNum来记录当前进程数的,但是一直为0啊。。。
if(g_iChildProcessNum<g_iMaxChdNum)
{
iPid= fork();

switch (iPid)
{
case -1:
{
CloseSockfd(iClientSocketFD, LINE);
}
break;
case 0:
{

CloseSockfd(iServerSocketFD, LINE);

g_iChildProcessNum++;

HandleClient(iClientSocketFD);

g_iChildProcessNum--;

CloseSockfd(iClientSocketFD, LINE);

exit(0);
}
break;
default:
{
CloseSockfd(iClientSocketFD, LINE);
int sStatus;
pid_t childId = waitpid (-1, &sStatus, WNOHANG);
memset(logbuf,0,sizeof(logbuf));
sprintf(logbuf,"waitpid=[%d],status=[%d],line=[%d]\n",childId,sStatus,LINE);
lWriteLog(logfile,logbuf,strlen(logbuf),6);
}
break;
}
}
else
{

CloseSockfd(iClientSocketFD, LINE);

memset(logbuf,0,sizeof(logbuf));
sprintf(logbuf,"Too many client connection,g_iChildProcessNum=[%d],g_iMaxChdNum=[%d],line=[%d]\n",g_iChildProcessNum,g_iMaxChdNum,LINE);
lWriteLog(logfile,logbuf,strlen(logbuf),6);
}

吴顶天的主页 吴顶天 | 初学一级 | 园豆:39
提问于:2019-04-04 13:43
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册