Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write daemon code by using templates

Shulou Source: shulou.com Published: 2022-06-03 07:42:34 09月29日 Update

This article mainly introduces "how to create daemon code by template". In daily operation, I believe that many people have doubts about how to create daemon code by template. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to create daemon code by template". Next, please follow the editor to study!

The code is as follows:

# include "wrap.h"

# define MYDOMAIN_FLAG "/ tmp/.mydomain_log"

Void domain_end () {

Unlink (MYDOMAIN_FLAG)

}

Int domain_init () {

Int fd

Fd=open (MYDOMAIN_FLAG,O_RDWR | O_CREAT | O_EXCL | Olympus APPENDT 0600)

If (fd==-1)

Err_sys (errno, "domain faild,% s", MYDOMAIN_FLAG)

Dup2 (fd,1)

Dup2 (fd,2)

Return fd

}

/ *

Features:

The work function is our working function, and its return value will be collected by the init () system process as the exit code of the guard.

Parameters:

Argc,argv is a parameter copy of the entry function main ().

, /

Int work (int argc,char * * argv) {

While (1) {

Sleep (60)

Err_msg ("one loop....")

}

Return 0

}

Int start_domain (int argc,char * * argv,int (* work) (int argc,char * * argv)) {

Pid_t pid

Int fd,ecode

Fd=domain_init ()

If (Fork () = = 0) {

If (Fork () = = 0) {

Atexit (domain_end)

Err_msg ("domain has ran sucessfully....")

/ * if the return time of work () is earlier than when its parent process called exit (0), it will not be adopted by the init () process. In the future, we will use the characteristics of the pipeline to synchronize the parent-child process. , /

Ecode=work (argc,argv)

Exit (ecode)

}

Exit (0)

}

Wait (NULL)

Return 0

}

Int main (int argc,char * * argv) {

Return start_domain (argc,argv,work)

}

At this point, the study on "how to write how to create daemon code in a template" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Process code mode template learning function parameter time more help utility next entry copy function article method father and son characteristics theory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL vpn Apple Huawei OPPO Reno