编写一个Linux C++守护进程涉及几个步骤,包括创建子进程、脱离终端、设置工作目录、关闭文件描述符等。以下是一个简单的示例代码,展示了如何编写一个基本的守护进程:
#include
#include
#include
#include
#include
#include
#include
void daemonize() {
pid_t pid, sid;
// Fork off the parent process
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
// If we got a good PID, then we can exit the parent process.
if (pid > 0) {
exit(EXIT_SUCCESS);
}
// Change the file mode mask
umask(0);
// Get a new session ID and create a new process group
sid = setsid();
if (sid < 0) {
exit(EXIT_FAILURE);
}
// Change the current working directory to root
if ((chdir("/")) < 0) {
exit(EXIT_FAILURE);
}
// Close standard file descriptors
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
// Redirect standard file descriptors to /dev/null
open("/dev/null", O_RDONLY);
open("/dev/null", O_RDWR);
open("/dev/null", O_RDWR);
}
void handle_signal(int signum) {
std::cout << "Received signal " << signum << ", exiting..." << std::endl;
exit(EXIT_SUCCESS);
}
int main() {
// Set up signal handlers
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handle_signal;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
// Daemonize the process
daemonize();
// Main loop
while (true) {
std::cout << "Daemon is running..." << std::endl;
sleep(1);
}
return 0;
}
解释
-
Fork off the parent process:
pid = fork(); if (pid < 0) { exit(EXIT_FAILURE); } if (pid > 0) { exit(EXIT_SUCCESS); }这段代码创建了一个子进程,并让父进程退出。这样可以确保守护进程不会成为终端的子进程。
-
Change the file mode mask:
umask(0);设置文件模式创建掩码为0,这样可以确保守护进程创建的文件具有预期的权限。
-
Get a new session ID and create a new process group:
sid = setsid(); if (sid < 0) { exit(EXIT_FAILURE); }创建一个新的会话并成为该会话的领头进程,从而脱离终端控制。
-
Change the current working directory to root:
if ((chdir("/")) < 0) { exit(EXIT_FAILURE); }将工作目录切换到根目录,以避免占用任何文件系统。
-
Close standard file descriptors:
close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO);关闭标准输入、输出和错误文件描述符。
-
Redirect standard file descriptors to /dev/null:
open("/dev/null", O_RDONLY); open("/dev/null", O_RDWR); open("/dev/null", O_RDWR);将标准输入、输出和错误重定向到
/dev/null,以避免与终端交互。 -
Set up signal handlers:
struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = handle_signal; sigaction(SIGINT, &sa, NULL); sigaction(SIGTERM, &sa, NULL);设置信号处理程序,以便在接收到
SIGINT和SIGTERM信号时优雅地退出。 -
Main loop:
while (true) { std::cout << "Daemon is running..." << std::endl; sleep(1); }守护进程的主循环,可以在这里执行守护进程的主要任务。
编译并运行这个程序:
g++ -o daemon daemon.cpp
./daemon
这样,你就创建了一个基本的Linux C++守护进程。
以上就是关于“如何编写Linux C++守护进程”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm