1.防止zombie process產生
http://home.educities.edu.tw/shirock/comp/Anti_zombie_process.htm
linux上的正解
int child_stat;
void reapchild() {
while( wait(&child_stat) <= 0 )
/*NOTHING or your code*/;
}
. . .
struct sigaction act;
act.sa_handler = reapchild;
act.sa_flags = SA_NOCLDSTOP; /* Do not signal while process paused */
sigaction( SIGCHLD, &act, NULL);
這個方式不適合在寫library時用
只要用library的人去重設signal就完了
2.system(const char *string)的傳回值
system這個function的return value傳回的是fork process的return value
並不是所執行的程式傳回值.
要拿到執行的程式傳回值要用以下方式
#include
int r;
r = system("ifconfig eth0");
if(WEXITSTATUS(r) == 1) /*Fail*/
return;
2.Fork
pid_t pid;
if((pid = vfork()) == 0) { /*Child*/
execl("/bin/ping", "ping", "-c", "3", ip, (char *)0);
_exit(127);
} else if(pid < 0)
perror("fork");
else /*Parent*/
dev->ping->pid = pid;
.......
.......
int status;
pid_t pid = waitpid(dev->ping->pid, &status, WNOHANG);
if(pid > 0)
if(WIFEXITED(status)) {
dev->ping->pid = 0;
if(WEXITSTATUS(status) != 0) { /*ping without response*/
SxNetDev_Fail(dev, "Ping fail");
return;
} else {
SxNetDev_Connect(dev);
SxNetDev_Ping(dev, config->pppoe.ping);
}
}
沒有留言:
張貼留言