1 创建一个输出程序
2 创建一个守护进程
1 创建一个输出程序
守护进程不与终端联系,所以,需要另外创建一个程序用于输出。
也可以直接使用/bin/echo
----- example_daemon_help.cc
#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;
}
编译:
g++ -o example_daemon_help example_daemon_help.cc --std=c++11
2 创建一个守护进程
先写一个函数: int daemon_init(const char* pname, int facility);
之后,只要将进程名传入此函数,就会变成守护进程。
----- 来自UNP第十三章, 有改动
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;
}
// 关于_exit请看:
http://blog.csdn.net/alex_my/article/details/39318607
下面是整个程序代码:
----- example_daemon.cc
#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;
}
可以将daemon_init放在这个文件中。
编译:
g++ -o example_daemon example_daemon.cc --std=c++11
有使用到nullptr,因此添上 --std=c++11,注意,等号两边不要空格
我是放在libhelpguy.a这个静态库文件中的。
编译:
g++ -o example_daemon example_daemon.cc -L. -lhelpguy --std=c++11
// 关于建立静态库请看
http://blog.csdn.net/alex_my/article/details/43529323
// 关于execl请看:
http://blog.csdn.net/alex_my/article/details/39374447