Daemon function creation daemon-General Linux technology-Linux programming and kernel information. for details, see the following. Reading books and creating daemon on the Internet are extremely complicated. you have to write a lot of code.
Today, we finally found that linux has functions for creating daemon.
Copy ---------------------- from the internet ----------------------
Daemon function usage
Note:
Let a program run in the background.
Prototype:
View plaincopy to clipboardprint?
# Include
Int daemon (int nochdir, int noclose );
Parameters:
When nochdir is zero, the current directory is changed to the root directory; otherwise, it remains unchanged;
When noclose is set to zero, the standard input, standard output, and error output are redirected to/dev/null, that is, no message is output. Otherwise, the output is still performed.
Return value:
Deamon () calls fork (). if fork is successful, the parent process calls _ exit (2) to exit. Therefore, all the error messages are generated by the child process. If the function is successful, 0 is returned. otherwise,-1 is returned and errno is set.
Int main ()
{
Daemon (1, 1); // The parameters are determined based on requirements.
/* Add the work code you need to do in the background here */
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.