通過命令ps -e -o pid,ppid,cmd,tty可以看出但凡daemon進程,其tty都顯示爲?,也就是不存在tty
1629 1 /usr/sbin/sshd ?
1684 1 /usr/sbin/boa ?
可以通過這樣的方法來判斷進程是否爲daemon
/*
* 判斷進程是否爲daemon進程
* /dev/tty可以打開的爲前臺程序, 不可打開的爲daemon
* @return: true, daemon; false, 非daemon
* */
bool is_daemon(void)
{
int fd = -1;
fd = open("/dev/tty", O_RDWR);
if(fd < 0)
return true;
close(fd);
return false;
}