Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write daemon code by using templates

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report