通过命令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;
}