首先看一段将普通进程转换为守护进程的代码: 【程序编程相关:unix入侵教程】
【推荐阅读:Linux的安全】/**************************************************************** 【扩展信息:Linux系统各文件、目录介绍】
---------------------------
function: daemonize description: detach the server process from the current context, creating a pristine, predictable environment in which it will execute. arguments: servfd file descriptor in use by server. return value: none. calls: none. globals: none. ****************************************************************/ void daemonize (servfd) int servfd; { int childpid, fd, fdtablesize; /* ignore terminal i/o, stop signals */ signal(sigttou,sig_ign); signal(sigttin,sig_ign); signal(sigtstp,sig_ign); /* fork to put us in the background (whether or not the user specified & on the command line */ if ((childpid = fork()) < 0) { fputs("failed to fork first childrn",stderr); exit(1); } else if (childpid > 0) ... 下一页