A simplified daemon framework written by X macro

Project introduction

This is a daemon framework under Linux

For the purpose of project optimization and learning to master and use X Macro skills, this project skillfully uses X Macro to write a daemon management framework that can expand the number of processes arbitrarily and pass parameters.

This code is extremely concise and easy to use. If you just need a daemon and don't want to make too big adjustments to the input of existing projects, you might as well try it.

Of course, if you can fully ensure the stable work of the project, you really don't need to use the daemon, but there is a contingency plan, which may surprise you at the critical moment.

directory structure

.
├── build.sh                    # Compile execution script
├── CMakeLists.txt              # CMake root project file
├── build/                      # The compiled directory and executable files are included in this directory
└── source/                         # Source directory
    |
    ├── CMakeLists.txt                  # CMake sub project documents
    ├── controller/                     # Controller directory, daemon and child process controller directory
    │   ├── w21Deamons.c                # The implementation source code of the daemon of this project
    │   └── w21Deamons.h                # Header file of daemon of this project
    |
    └── main.cpp                        # Project entry file

instructions

1. Compile

w0x0021@ubuntu:~/w21Deamons$ ./build.sh

2. Code modification

In this project, there are three key locations that need to be modified and one entry call for w21deamons The modification location of H file has been marked with comments in the header file, and the comments are marked as follows:

/*******************************************************************
 *       Please change the code for this area - Start              *
 *******************************************************************/
/*
    ...
    Code area to be modified
    ...
*/
W21DMS_XMACROS(TASK_MAX , w21Dms_gTask, w21Dms_taskMaxFunc)         /*** This line cannot be modified***/
/*******************************************************************
 *       Please change the code for this area - End                *
 *******************************************************************/

The three key and one entrance to be modified are:

  • Modification 1: format specification of subprocess entry function
  • Modification 2: in the header file w21deamons H add declaration of subprocess entry function
  • Modification 3: in the header file w21deamons H add sub process task
  • Entry: calling the daemon in the main() function of the project entry

The specific modification method is as follows:

Modification 1: format specification of subprocess entry function

  • 1. Arbitrary entry function name

  • 2. The return value is void

  • 3. There are two parameters: (int, char * *). The accepted parameters are the two parameters passed by the main() function of the project, which have not been modified

    Example:

    void task_name(int argc, char **argv);
    

Modification 2: in the header file w21deamons H add declaration of subprocess entry function

In this project, there are two ways to declare the entry function of adding sub processes

  • Method 1: directly write the subprocess entry function declaration to w21deamons H header file

  • Method 2: include the header file with the subprocess entry function declaration to w21deamons H header file

    Example:

    Suppose: the number of subtasks to be added is 3, and the function names are task1_main,task3_main,task3_ main

    Method 1:

    /*******************************************************************
     *       Please change the code for this area - Start              *
     *******************************************************************/
    void task1_main(int argc, char **argv);
    void task2_main(int argc, char **argv);
    void task3_main(int argc, char **argv);
    #define W21DMS_MACROS                                               \
                                                                        \
        W21DMS_XMACROS(TASK_MAX , w21Dms_gTask, w21Dms_taskMaxFunc)     /*** The line dose not be changed ***/
    /*******************************************************************
     *       Please change the code for this area - End                *
     *******************************************************************/
    

    Method 2:

    task1.h

    void task1_main(int argc, char **argv);
    

    task2.h

    void task1_main(int argc, char **argv);
    

    task3.h

    void task1_main(int argc, char **argv);
    

    w21Demons.h

    /*******************************************************************
     *       Please change the code for this area - Start              *
     *******************************************************************/
    #include "task1.h"
    #include "task2.h"
    #include "task3.h"
    #define W21DMS_MACROS                                               \
                                                                        \
        W21DMS_XMACROS(TASK_MAX , w21Dms_gTask, w21Dms_taskMaxFunc)     /*** The line dose not be changed ***/
    /*******************************************************************
     *       Please change the code for this area - End                *
     *******************************************************************/
    

    Note: to avoid unnecessary compilation exceptions, it is recommended to use method 1

Modification 3: in the header file w21deamons H add sub process task

The add sub process task is mainly composed of the macro W21DMS_MACROS control, the program code specification of each new task is as follows:

W21DMS_XMACROS(TASK_INDEX_x, w21Dms_gTask, task_mainName)   \

Of which:

  • W21DMS_XMACROS: this macro is the implementation of X macro skills. The implementation process is in w21demons In the C document, only the instructions are given here, and the technical description is omitted temporarily.

  • TASK_INDEX_x: the index of the task. The format of this parameter is not required. It only needs to keep the writing standard and use uppercase, but it cannot be the same as the index name of other tasks, and task cannot be used_ MAX

  • w21Dms_gTask: this parameter is a fixed global variable name, which is w21Dms_TaskStatus_t type array, the number of arrays is TASK_MAX, while task_ The value of Max is equal to the number of all child processes under the header file + 1. This value is changed according to the number of new tasks and does not need to be modified separately

  • task_mainName: the entry function name of the child process

  • \: for each new task, there must be no line break escape at the end, indicating w21dms_ Macro is not finished, and the real location of the end of macro is changed to TASK_MAX is a task. This process is just an empty process. It ends after one execution. The purpose is to create a task named task_ Enumeration index of max.

Example:

Or suppose: the number of subtasks to be added is 3, and the function names are task1_main,task3_main,task3_ Main, the modified program code fragment is as follows:

/*******************************************************************
 *       Please change the code for this area - Start              *
 *******************************************************************/
void task1_main(int argc, char **argv);
void task2_main(int argc, char **argv);
void task3_main(int argc, char **argv);
#define W21DMS_MACROS                                               \
    W21DMS_XMACROS(TASK_FUN1, w21Dms_gTask, task1_main)             \
    W21DMS_XMACROS(TASK_FUN2, w21Dms_gTask, task2_main)             \
    W21DMS_XMACROS(TASK_FUN3, w21Dms_gTask, task3_main)             \
                                                                    \
    W21DMS_XMACROS(TASK_MAX , w21Dms_gTask, w21Dms_taskMaxFunc)     /*** The line dose not be changed ***/
/*******************************************************************
 *       Please change the code for this area - End                *
 *******************************************************************/

Entry: calling the daemon in the main() function of the project entry

This project only needs to call the daemon's entry function w21dms at the program entry_ Main().

The entry call is as follows:

int main(int argc, char **argv)
{
	w21Dms_Main(argc, argv);
	return 0;
}

The project has not yet processed the return value of sub processes and adding processes according to conditions. There is an opportunity to improve them later.

Gitee project address: w21Deamons
GitHub project address: w21Deamons

Personal blog home page: Yamanaka book

Tags: C C++ Linux

Posted by LukeO on Sun, 22 May 2022 04:59:18 +0300