linux

如何编写Linux C++守护进程

小樊
40
2025-08-09 23:41:02
栏目: 编程语言

编写一个Linux C++守护进程涉及几个步骤,包括创建子进程、脱离终端、设置工作目录、关闭文件描述符等。以下是一个简单的示例代码,展示了如何编写一个基本的守护进程:

#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <cstring>

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;
}

解释

  1. Fork off the parent process:

    pid = fork();
    if (pid < 0) {
        exit(EXIT_FAILURE);
    }
    if (pid > 0) {
        exit(EXIT_SUCCESS);
    }
    

    这段代码创建了一个子进程,并让父进程退出。这样可以确保守护进程不会成为终端的子进程。

  2. Change the file mode mask:

    umask(0);
    

    设置文件模式创建掩码为0,这样可以确保守护进程创建的文件具有预期的权限。

  3. Get a new session ID and create a new process group:

    sid = setsid();
    if (sid < 0) {
        exit(EXIT_FAILURE);
    }
    

    创建一个新的会话并成为该会话的领头进程,从而脱离终端控制。

  4. Change the current working directory to root:

    if ((chdir("/")) < 0) {
        exit(EXIT_FAILURE);
    }
    

    将工作目录切换到根目录,以避免占用任何文件系统。

  5. Close standard file descriptors:

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);
    

    关闭标准输入、输出和错误文件描述符。

  6. Redirect standard file descriptors to /dev/null:

    open("/dev/null", O_RDONLY);
    open("/dev/null", O_RDWR);
    open("/dev/null", O_RDWR);
    

    将标准输入、输出和错误重定向到/dev/null,以避免与终端交互。

  7. 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);
    

    设置信号处理程序,以便在接收到SIGINTSIGTERM信号时优雅地退出。

  8. Main loop:

    while (true) {
        std::cout << "Daemon is running..." << std::endl;
        sleep(1);
    }
    

    守护进程的主循环,可以在这里执行守护进程的主要任务。

编译并运行这个程序:

g++ -o daemon daemon.cpp
./daemon

这样,你就创建了一个基本的Linux C++守护进程。

0
看了该问题的人还看了