分页: 1 / 1

使用fork() +execv() 遇到问题 请各位帮忙 ??[已解决]

发表于 : 2008-01-05 22:26
wkt

代码: 全选

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> 就没有了!
这个东西太烦人了!!