linux守护进程原理及实现很多资料都有讲述,在此不做赘述,仅粘贴一程序轮廓,省得到时候翻书。
#include
#include
#include
#include
int main(int argc, char **argv)
{
int ret = 0;
char pout[32];
pid_t pid, sid;
FILE *pfile=NULL;
pid = fork();
if ( pid < 0 ) {
exit(EXIT_FAILURE);
} else if ( pid > 0 ) {
exit(EXIT_SUCCESS);
}
if ( (sid = setsid()) < 0 ) {
exit(EXIT_FAILURE);
}
if ( chdir("/") < 0 ) {
exit(EXIT_FAILURE);
}
umask(0);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
while ( 1 ) {
......
}
return 0;
}
阅读(1699) | 评论(0) | 转发(0) |