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 is our working function, and its return value will be collected as the guardian exit code by the init() system process.
parameter:
argc and argv are both parameter copies 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 the time when its parent process calls 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 father-son 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);
}
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 is our working function, and its return value will be collected as the guardian exit code by the init() system process.
parameter:
argc and argv are both parameter copies 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 the time when its parent process calls 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 father-son 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);
}