当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 1 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : 使用fork() +execv() 遇到问题 请各位帮忙 ??[已解决]
帖子发表于 : 2008-01-05 22:26 

注册: 2006-09-07 22:51
帖子: 849
送出感谢: 0 次
接收感谢: 0 次
代码:
int my_exec(char **cmd,int flag)
{
 int status = 0;
 int pid = 0;
 if((pid=fork())==0){
  execv(cmd[0],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[strlen(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==0)my_exec(cmd,1);
  if(fd)fclose(fd);
}

handler 在一个 程序 运行多次后:
$ps aux
root 19748 0.0 0.0 0 0 pts/2 Z 22:21 0:00 [pppoecount.sh] <defunct>
root 19755 0.0 0.0 0 0 pts/2 Z 22:21 0:00 [pppoecount.sh] <defunct>
root 19761 0.0 0.0 0 0 pts/2 Z 22:21 0:00 [pppoecount.sh] <defunct>
root 19767 0.0 0.0 0 0 pts/2 Z 22:21 0:00 [pppoecount.sh] <defunct>
root 19773 0.0 0.0 0 0 pts/2 Z 22:21 0:00 [pppoecount.sh] <defunct>
root 19779 0.0 0.0 0 0 pts/2 Z 22:22 0:00 [pppoecount.sh] <defunct>
root 19785 0.0 0.0 0 0 pts/2 Z 22:22 0:00 [pppoecount.sh] <defunct>
root 19791 0.0 0.0 0 0 pts/2 Z 22:22 0:00 [pppoecount.sh] <defunct>
root 19797 0.0 0.0 0 0 pts/2 Z 22:22 0:00 [pppoecount.sh] <defunct>
root 19803 0.0 0.0 0 0 pts/2 Z 22:22 0:00 [pppoecount.sh] <defunct>
root 19809 0.0 0.0 0 0 pts/2 Z 22:22 0:00 [pppoecount.sh] <defunct>
root 19815 0.0 0.0 0 0 pts/2 Z 22:22 0:00 [pppoecount.sh] <defunct>
root 19821 0.0 0.0 0 0 pts/2 Z 22:23 0:00 [pppoecount.sh] <defunct>
root 19827 0.0 0.0 0 0 pts/2 Z 22:23 0:00 [pppoecount.sh] <defunct>
root 19835 0.0 0.0 0 0 pts/2 Z 22:23 0:00 [pppoecount.sh] <defunct>
root 19841 0.0 0.0 0 0 pts/2 Z 22:23 0:00 [pppoecount.sh] <defunct>
root 19847 0.0 0.0 0 0 pts/2 Z 22:23 0:00 [pppoecount.sh] <defunct>
root 19853 0.0 0.0 0 0 pts/2 Z 22:23 0:00 [pppoecount.sh] <defunct>
root 19859 0.0 0.0 0 0 pts/2 Z 22:23 0:00 [pppoecount.sh] <defunct>
root 19865 0.0 0.0 0 0 pts/2 Z 22:23 0:00 [pppoecount.sh] <defunct>
root 19871 0.0 0.0 0 0 pts/2 Z 22:24 0:00 [pppoecount.sh] <defunct>
root 19877 0.0 0.0 0 0 pts/2 Z 22:24 0:00 [pppoecount.sh] <defunct>
root 19884 0.0 0.0 0 0 pts/2 Z 22:24 0:00 [pppoecount.sh] <defunct>
root 19890 0.0 0.0 0 0 pts/2 Z 22:24 0:00 [pppoecount.sh] <defunct>
root 19896 0.0 0.0 0 0 pts/2 Z 22:24 0:00 [pppoecount.sh] <defunct>
root 19902 0.0 0.0 0 0 pts/2 Z 22:24 0:00 [pppoecount.sh] <defunct>
root 19908 0.0 0.0 0 0 pts/2 Z 22:24 0:00 [pppoecount.sh] <defunct>
root 19914 0.0 0.0 0 0 pts/2 Z 22:25 0:00 [pppoecount.sh] <defunct>
root 19920 0.0 0.0 0 0 pts/2 Z 22:25 0:00 [pppoecount.sh] <defunct>
root 19926 0.0 0.0 0 0 pts/2 Z 22:25 0:00 [pppoecount.sh] <defunct>
root 19932 0.0 0.0 0 0 pts/2 Z 22:25 0:00 [pppoecount.sh] <defunct>
root 19938 0.0 0.0 0 0 pts/2 Z 22:25 0:00 [pppoecount.sh] <defunct>
root 19944 0.0 0.0 0 0 pts/2 Z 22:25 0:00 [pppoecount.sh] <defunct>
root 19950 0.0 0.0 0 0 pts/2 Z 22:25 0:00 [pppoecount.sh] <defunct>
root 19956 0.0 0.0 0 0 pts/2 Z 22:25 0:00 [pppoecount.sh] <defunct>
root 19962 0.0 0.0 0 0 pts/2 Z 22:26 0:00 [pppoecount.sh] <defunct>
root 19968 0.0 0.0 0 0 pts/2 Z 22:26 0:00 [pppoecount.sh] <defunct>
root 19974 0.0 0.0 0 0 pts/2 Z 22:26 0:00 [pppoecount.sh] <defunct>
root 19980 0.0 0.0 0 0 pts/2 Z 22:26 0:00 [pppoecount.sh] <defunct>
root 19986 0.0 0.0 0 0 pts/2 Z 22:26 0:00 [pppoecount.sh] <defunct>
root 19992 0.0 0.0 0 0 pts/2 Z 22:26 0:00 [pppoecount.sh] <defunct>
root 19998 0.0 0.0 0 0 pts/2 Z 22:26 0:00 [pppoecount.sh] <defunct>
linux 20005 1.3 5.3 40952 13676 pts/1 SL+ 22:26 0:00 mplayer -ao alsa -vo null /media/hdb5/music/ha
root 20006 0.0 0.0 0 0 pts/2 Z 22:27 0:00 [pppoecount.sh] <defunct>
root 20012 0.0 0.0 0 0 pts/2 Z 22:27 0:00 [pppoecount.sh] <defunct>
root 20018 0.0 0.0 0 0 pts/2 Z 22:27 0:00 [pppoecount.sh] <defunct>
root 20024 0.0 0.0 0 0 pts/2 Z 22:27 0:00 [pppoecount.sh] <defunct>
linux 20030 0.0 0.3 5804 1028 pts/4 R+ 22:27 0:00 ps aux
[pppoecount.sh] <defunct> 为什么还在啊??
脚本都运行结束了啊!!
当然如果程序关闭,[pppoecount.sh] <defunct> 就没有了!
这个东西太烦人了!!


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 1 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 2 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译