Copy Code code 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| O_APPEND,0600);
if (fd==-1)
Err_sys (errno, "Domain Faild,%s", Mydomain_flag);
Dup2 (fd,1);
Dup2 (fd,2);
return FD;
}
/*
Function:
The work function for our work function, his return value, will be collected as the daemon exit code by the Init () system process
Parameters:
ARGC,ARGV are a copy of the parameters 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 work () returns longer than the parent process calls exit (0), it will not be adopted by the Init () process. Later we will use the characteristics of the pipe 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);
}