wkt 发表于 2008-1-6 11:47:20

使用fork() +execv() 遇到问题 请各位帮忙 ??

还是先看一下代码吧!!
int my_exec(char **cmd,int flag)
{
int status = 0;
int pid = 0;
if((pid=fork())==0){
execv(cmd,cmd);
fprintf(stderr,"execv():%s .\n",strerror(errno));
exit(errno);
//child !
}
else if(pid == -1 ){ ///error .
   fprintf(stderr,"fork():%s \n",strerror(errno));
   return pid;
}
if(flag == 0){//father
    waitpid(pid,&status, WNOHANG);
    return WEXITSTATUS(status);
}
else if (flag == 1 )
   return pid;
return (!0);
}

void handler()
{
FILE *fd = NULL;
char *fpath ="/proc/net/dev";
ppp *p = NULL;
char *stt = NULL;
char *cmd[]={"/home/codes/script/pppoecount.sh",username,"8",NULL};
alarm(2);
fd= fopen (fpath, "r" );
if(!fd){
    g_print("Can't fopen(): %s :%s\n",fpath,strerror(errno));
    return;
}
//g_print("come to handler for alarm .\n");
p = get_ppps(fd);
stt = sprint_ppp(p);
if(g_strrstr(stt,"eth"))t_sec+=2;
else t_sec = 0;
sprintf(&stt,"ppp time: %ld:%ld:%ld.\n",t_sec/(3600),(t_sec/60)%60,t_sec%60);
if(label)gtk_label_set_text(GTK_LABEL(label),stt);
free_ppp(p);
////////////////////// 只能这样做标记了
if(t_sec%8==0my_exec(cmd,1);
/////////////////////
if(fd)fclose(fd);
}
handler 在一个 程序 调用多次后:
$ps aux
root   197480.00.0      0   0 pts/2    Z    22:21   0:00 <defunct>
root   197550.00.0      0   0 pts/2    Z    22:21   0:00 <defunct>
root   197610.00.0      0   0 pts/2    Z    22:21   0:00 <defunct>
root   197670.00.0      0   0 pts/2    Z    22:21   0:00 <defunct>
root   197730.00.0      0   0 pts/2    Z    22:21   0:00 <defunct>
root   197790.00.0      0   0 pts/2    Z    22:22   0:00 <defunct>
root   197850.00.0      0   0 pts/2    Z    22:22   0:00 <defunct>
root   197910.00.0      0   0 pts/2    Z    22:22   0:00 <defunct>
root   197970.00.0      0   0 pts/2    Z    22:22   0:00 <defunct>
root   198030.00.0      0   0 pts/2    Z    22:22   0:00 <defunct>
root   198090.00.0      0   0 pts/2    Z    22:22   0:00 <defunct>
root   198150.00.0      0   0 pts/2    Z    22:22   0:00 <defunct>
root   198210.00.0      0   0 pts/2    Z    22:23   0:00 <defunct>
root   198270.00.0      0   0 pts/2    Z    22:23   0:00 <defunct>
root   198350.00.0      0   0 pts/2    Z    22:23   0:00 <defunct>
root   198410.00.0      0   0 pts/2    Z    22:23   0:00 <defunct>
root   198470.00.0      0   0 pts/2    Z    22:23   0:00 <defunct>
root   198530.00.0      0   0 pts/2    Z    22:23   0:00 <defunct>
root   198590.00.0      0   0 pts/2    Z    22:23   0:00 <defunct>
root   198650.00.0      0   0 pts/2    Z    22:23   0:00 <defunct>
root   198710.00.0      0   0 pts/2    Z    22:24   0:00 <defunct>
root   198770.00.0      0   0 pts/2    Z    22:24   0:00 <defunct>
root   198840.00.0      0   0 pts/2    Z    22:24   0:00 <defunct>
root   198900.00.0      0   0 pts/2    Z    22:24   0:00 <defunct>
root   198960.00.0      0   0 pts/2    Z    22:24   0:00 <defunct>
root   199020.00.0      0   0 pts/2    Z    22:24   0:00 <defunct>
root   199080.00.0      0   0 pts/2    Z    22:24   0:00 <defunct>
root   199140.00.0      0   0 pts/2    Z    22:25   0:00 <defunct>
root   199200.00.0      0   0 pts/2    Z    22:25   0:00 <defunct>
root   199260.00.0      0   0 pts/2    Z    22:25   0:00 <defunct>
root   199320.00.0      0   0 pts/2    Z    22:25   0:00 <defunct>
root   199380.00.0      0   0 pts/2    Z    22:25   0:00 <defunct>
root   199440.00.0      0   0 pts/2    Z    22:25   0:00 <defunct>
root   199500.00.0      0   0 pts/2    Z    22:25   0:00 <defunct>
root   199560.00.0      0   0 pts/2    Z    22:25   0:00 <defunct>
root   199620.00.0      0   0 pts/2    Z    22:26   0:00 <defunct>
root   199680.00.0      0   0 pts/2    Z    22:26   0:00 <defunct>
root   199740.00.0      0   0 pts/2    Z    22:26   0:00 <defunct>
root   199800.00.0      0   0 pts/2    Z    22:26   0:00 <defunct>
root   199860.00.0      0   0 pts/2    Z    22:26   0:00 <defunct>
root   199920.00.0      0   0 pts/2    Z    22:26   0:00 <defunct>
root   199980.00.0      0   0 pts/2    Z    22:26   0:00 <defunct>
linux    200051.35.340952 13676 pts/1    SL+22:26   0:00 mplayer -ao alsa -vo null /media/hdb5/music/ha
root   200060.00.0      0   0 pts/2    Z    22:27   0:00 <defunct>
root   200120.00.0      0   0 pts/2    Z    22:27   0:00 <defunct>
root   200180.00.0      0   0 pts/2    Z    22:27   0:00 <defunct>
root   200240.00.0      0   0 pts/2    Z    22:27   0:00 <defunct>
linux    200300.00.3   58041028 pts/4    R+   22:27   0:00 ps aux
脚本都运行结束了啊!!
<defunct> 为什么还在啊??

当然如果程序关闭, <defunct> 就没有了!
这个东西有点烦人,恳请有人能帮我把它弄掉??

[ 本帖最后由 wkt 于 2008-1-6 11:50 编辑 ]

PoewerGUI 发表于 2008-1-6 18:43:14

oh,著名的僵尸进程。
在程序中ignore掉SIGCHLD即可。

Regards
PowerGUI
---------------------------------
开放手机联盟 Android中文社区(Google手机操作系统)
http://www.openhandsetalliance.org.cn

wkt 发表于 2008-1-6 23:32:33

原帖由 PoewerGUI 于 2008-1-6 18:43 发表 http://www.linuxfans.org/bbs/images/common/back.gif
oh,著名的僵尸进程。
在程序中ignore掉SIGCHLD即可。


很有效多谢了!!
页: [1]
查看完整版本: 使用fork() +execv() 遇到问题 请各位帮忙 ??