【发布时间】:2015-09-03 16:14:39
【问题描述】:
在我的守护进程中,我尝试使用 ping 来检测主机是否存活。
代码:
sprintf(pingCmd,"ping -c1 -t%d %s",time,ip);
if (system(pingCmd)) {
//fail
}
else {
//success
}
但是当我移除目标主机的网线时。
我的守护进程将在 system() 处阻塞
#0 0x00007f75362fbc7d in waitpid () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x00007f7536281f5e in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#2 0x00007f7536282370 in system () from /lib/x86_64-linux-gnu/libc.so.6
所以我写的是anthor程序,而不是守护进程
代码没有在 sysem() 处阻塞,
即使我移除了目标主机网线,
如何在守护进程中使用 system("ping")
【问题讨论】:
-
system总是阻塞 -
and (system()) returns after the command has been completedlinux.die.net/man/3/system -
ping 的意义何在?如果是在做联系宿主的事情之前先检查宿主是否活着,那就没用了。
-
alive 是什么意思?活着意味着所有服务都在工作?然后不要使用ping。 ping 是一个等待没有活着的主机服务,在无法等待主机响应它不活着的服务器上执行得很糟糕。