Create a daemon in Linux

Source: Internet
Author: User
Create a daemon in Linux
01-7-27 10:30:08 AM

There are many ways to create a daemon in Linux. For example, you can use programs such as cron and inetd to create a daemon. Here we will introduce the daemon enabled by users on the control terminal. This daemon does not rely on any terminal and does not end with the user's exit. This type of program is often used in network programs.
To change a program to a daemon, follow these steps.
Call the fork function, and then launch the parent process, so that the process becomes a background process. At the same time, the child process does not become the process group leader (the leader may be the parent process or the process that creates the parent process) to prepare for the second step of the system to call setsid.
Call setsid to create a new meeting group, and the process becomes the leader of a new meeting group. In this way, the Meeting Group has no control terminal.
Add signal sighup processing. Handle the withdrawal of the later meeting leader. Call fork again. Then the parent process is released. I ignore this signal because the process leader sends sighup to all meeting members when exiting. In this way, our process has no control terminal.
Call the chdir function to change the working directory of the process to the root directory (/). In this way, our program can not occupy a detachable device. The system reports that the device is busy when the root user uninstalls the device.
Disable all file descriptors and redirect three standard file descriptors.
The following program creates a daemon which redirects standard output, standard input, and standard error output. Output the number of available system memory pages per minute to the standard error.
 
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
 
Int main (INT argc, char ** argv)
{
Struct sigaction Act;
Int error, in, out;
Time_t now;
Int memory;
 
/* The parent process exits */
If (Fork ()! = 0) Exit (1 );
 
/* Create a new meeting group */
If (setsid () <0) Exit (1 );
 
/* Ignore signal sighup */
Act. sa_handler = sig_ign;
Sigemptyset (& act. sa_mask );
Act. sa_flags = 0;
If (sigaction (sighup, & act, null) =-1) Exit (1 );
 
/* The sub-process exits, and the sun process has no control terminal */
If (Fork ()! = 0) Exit (1 );
 
If (chdir ("/") =-1) Exit (1 );
 
/* Standard error redirection */
Error = open ("/tmp/error", o_wronly | o_creat, 0600 );
Dup2 (error, stderr_fileno );
Close (error );
 
/* Standard input redirection */
In = open ("/tmp/in", o_rdonly | o_creat, 0600 );
If (dup2 (in, stdin_fileno) =-1) perror ("in ");
Close (in );
 
/* Standard output redirection */
Out = open ("/tmp/out", o_wronly | o_creat, 0600 );
If (dup2 (Out, stdout_fileno) =-1) perror ("out ");
Close (out );
 
While (1)
{
Time (& now );
Memory = sysconf (_ SC _avphys_pages );
Fprintf (stderr, "Time/T % S/T available memory [% d]/n", ctime (& now), memory );
Sleep (60 );
}
Exit (0 );
}
 
After running this program, run the PS-x command to see if the program's tty is? Indicates that this program has no control terminal.
Related Article

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.