#include <stdio.h>
int main(int argc, char** argv)
{
if(argc == 1)
{
printf("only one parameter \n");
}
else if(argc == 2)
{
printf("%s \n", argv[1]);
}
else
{
printf("too more parameter: %d\n", argc);
}
return 0;
}int daemon_init(const char* pname, int facility)
{
umask(0);
pid_t pid;
if((pid = fork()) < 0)
return -1;
else if(pid > 0)
_exit(0); // parent terminates
// child 1 continues
// become session leader
if(setsid() < 0)
return -1;
// ignore SIGHUP
// signal(SIGHUP, SIG_IGN);
struct sigaction sa;
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if(sigaction(SIGHUP, &sa, nullptr) < 0)
_exit(0);
if((pid = fork()) < 0)
return -1;
else if(pid > 0)
_exit(0); // child 1 terminates
// child 2 continues
helpguy::daemon_flag = 1;
// changes working directory
chdir("/");
// close off file descriptors
rlimit rl;
if(getrlimit(RLIMIT_NOFILE, &rl) < 0)
return -1;
int fdMax = rl.rlim_max == RLIM_INFINITY ? 1024 : rl.rlim_max;
for(int i = 0; i < fdMax; ++i)
close(i);
// redirect stdin, stdout and stderr to /dev/null
open("/dev/null", O_RDONLY);
open("/dev/null", O_RDWR);
open("/dev/null", O_RDWR);
openlog(pname, LOG_PID, facility);
return 0;
}#include "helpguy.h" // 含有daemon_init函数
#include <syslog.h>
int main(int argc, char** argv)
{
// daemon process
daemon_init(argv[0], 0);
int count = 0;
while(++count <= 2)
{
sleep(3);
// notifice
pid_t pid;
if((pid = fork()) == 0)
{
// 子进程继承父进程的文件描述符
// 在daemon_init中,父进程的stdout被重定向到/dev/null
close(1);
open("/dev/pts/0", O_WRONLY);
// exec
execl("/home/alex_my/Code/UNP/example_daemon_help", "example_daemon_help", "3 sec now", nullptr);
// 也可以使用这个
// execl("/bin/echo", "echo", "Clock 3", nullptr);
}
// parent
waitpid(pid, nullptr, 0);
}
return 0;
}原文地址:http://blog.csdn.net/alex_my/article/details/43529365