[Linux] Process (vi)-Daemon process

Source: Internet
Author: User

15, Guardian Process

Concept:
A daemon (Daemon) is a special process that runs in the background. It is independent of the control terminal and periodically performs some sort of task or waits to handle certain occurrences.
features of the daemon:
The daemon must be isolated from the environment before it is run. These environments include file descriptors that are not closed, control terminals, sessions and process groups, working directories, and files
Create masks, and so on. These environments are typically inherited by daemons from the parent process that executes it, especially the shell.
Programming Essentials for Daemons:
(1) Background operation
Parent process Nine exits after the parent process fork () out of the child process
if (Pid=fork ())
Exit (0);//is the parent process that ends the parent process, and the child process continues
(2) Out of control terminal, login session and process group
The method is to call Setsid ();
Description: The Setsid () call failed when the process was the session leader. But the 1th has ensured that the process is not a conversation leader. After the Setsid () call succeeds, the process becomes the new session
Team leader and the new process leader, and detach from the original login session and process group.
(3) prohibit the process from reopening the control terminal
Now, the process has become a session leader without terminal. But it can be re-applied to open a control terminal. You can prevent a process from reopening the control terminal by making the process no longer a session leader:
if (Pid=fork ())
Exit (0);//End first child process, second child process continue (second child process is no longer a conversation leader)
(4) Close Open File descriptor
For (i=0;i Close Open file descriptor Close (i);>
(5) Change the current directory
The file system in which the working directory resides cannot be unloaded while the process is active. It is generally necessary to change the working directory to the root directory. For the core to be dumped,
The process of writing a run log changes the working directory to a specific directory such as/tmpchdir ("/")
(6) Resetting the file creation mask
The process inherits the file creation mask from the parent process that created it. It may modify the access bit of the file created by the daemon. To prevent this, the file creation mask is cleared: umask (0);
(7) Processing SIGCHLD signal
Signal (sigchld,sig_ign);

[CPP]View Plaincopy
  1. Daemon instance:
  2. #include <unistd.h>
  3. #include <signal.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #ifndef Nofile
  9. #define Nofile 3
  10. #endif
  11. void Init_daemon ()
  12. {
  13. int pid;
  14. int i;
  15. if (PID = fork ()) exit (0); //parent process, exiting
  16. Else if (PID < 0) exit (1); //fork failure
  17. / * Child process continues to execute * /
  18. Setsid (); //Create new session group, sub-process becomes leader and separate from Control terminal
  19. /* Prevent child process (group leader) from acquiring control terminal */
  20. if (PID = fork ()) exit (0); //parent process, exiting
  21. Else if (PID < 0) exit (1); //fork Error, exiting
  22. / * The second child process continues execution, the second child process is no longer the session group leader * /
  23. //for (i = 0; i < nofile; i++)/* Close Open File descriptor */
  24. //{   
  25. //close (i);
  26. //}   
  27. ChDir ("/tmp"); / * Switch working directory * /
  28. Umask (0); / * Reset the file creation mask * /
  29. return;
  30. }
  31. int main ()
  32. {
  33. FILE *FP;
  34. Signal (SIGCHLD, sig_ign); / * Ignore child process end signal to prevent zombie process * /
  35. Init_daemon ();
  36. While (1)
  37. {
  38. Sleep (1);
  39. if (fp = fopen ("/home/mantou/test.log", "a"))! = NULL)
  40. {
  41. fprintf (FP, "%s\n", "test message");
  42. Fclose (FP);
  43. }
  44. }
  45. return 0;
  46. }


Add:
(1) About process struct RLIMIT structure click to open link
In Linux systems, resouce limit refers to the amount of resources that can be obtained during the execution of a process, such as the maximum value of the core file of the process, the maximum value of the virtual memory, and so on.
The size of the resouce limit can directly affect the execution status of the process. It has two of the most important concepts: soft limit and hard limit.
struct Rlimit {
Rlim_t rlim_cur;
Rlim_t Rlim_max;
};
(2) How the daemon prints log
Answer: You can use the syslog () function
(3) Linux kernel code to start the user space init daemon
KERNEL/INIT/MAIN.C Click to open link
At the moment, at least we know that there are two ways to execute a user-space application in kernel space:
1. Call_usermodehelper
2. Kernel_execve

[Linux] Process (vi)-Daemon process

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.