Deamon process in linux

Source: Internet
Author: User
The deamon process in linux-general Linux technology-Linux programming and kernel information. The following is a detailed description. The key to designing a deamon process in linux is as follows:

1. fork twice.
2. fork becomes the process group Header between the first and second times.
3. Change the working directory.
4. Ignore the signal.
5. Set the read/write mask.

This is a routine collected from the network. It demonstrates the above points very well and stores this memo.

# Include
# Include
# Include
# Include

Void main (int argc, char ** argv)
{
Time_t now;
Int childpid, fd, fdtablesize;
Int error, in, out;

/* Ignore terminal I/O signals and STOP signals */
Signal (SIGTTOU, SIG_IGN );
Signal (SIGTTIN, SIG_IGN );
Signal (SIGTSTP, SIG_IGN );
Signal (SIGHUP, SIG_IGN );

/* The parent process exits and the program runs in the background */
If (fork ()! = 0)
{
Exit (1 );
}
If (setsid () <0)
{
Exit (1);/* Create a new meeting group */
}

/* The sub-process exits and the sun process loses its control terminal */
If (fork ()! = 0)
{
Exit (1 );
}
If (chdir ("/tmp") =-1)
{
Exit (1 );
}

/* Disable open file descriptors, including standard input, standard output, and standard error output */
Fd = 0;
For (fdtablesize = getdtablesize (); fd <fdtablesize; fd ++)
{
Close (fd );
}

Umask (0);/* reset the file creation mask */
Signal (SIGCHLD, SIG_IGN);/* ignore SIGCHLD signal */

/* Here, you can do the service routine code you want to do. Generally, it is an endless loop. If you want to exit with control, you can set the signal to be obtained. The following example shows a log writing routine.
*/

/* Write log, test result */
Syslog (LOG_USER | LOG_INFO, "daemon test! \ N ");

While (1)
{
Time (& now );
Syslog (LOG_USER | LOG_INFO, "Current Time: \ t % s \ t \ n", ctime (& now ));
Sleep (6 );
}
}

The above program is compiled in gcc4.0.1 and runs stably in the fc4-2.6.11.

Syslog will connect to the syslogd service and output messages in the/var/log/message file. Therefore, you need to start the syslogd service. Otherwise, it will not be displayed in the/var/log/message file.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.