??????????????????????Daemon???????Linux?е???????????????????????????????????????????????????????????????????????????Щ???????????????????????????????????????????????????Linux???к?????????????????????????????????????????????????????????????????磬????滮????crond?????????lqd?????????β???d??Daemon?????????
??????????Linux?У???????????????н?????????????????????????????е??????????????????????????????Щ????????????????????????????????????????????????????????????????????????????????п????????????????????????????????????????????????????????????仯???????????????????????????????????????潫?????????????
1 /************************************************
2  * ?????????Linux?????????????
3 ************************************************/
4 #include <unistd.h>
5 #include <signal.h>
6 #include <sys/param.h>  // NOFILE
7 #include <sys/stat.h>   // umask
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <time.h>
11 #include <assert.h>
12
13 bool initDaemon()
14 {
15     // ?????Щ?й???????????????
16     // ??????????????????????????????????????????????????
17     assert(signal(SIGINT?? SIG_IGN) != SIG_ERR); // ????ж?
18     assert(signal(SIGHUP?? SIG_IGN) != SIG_ERR); // ??????
19     assert(signal(SIGQUIT?? SIG_IGN) != SIG_ERR);// ??????
20     assert(signal(SIGPIPE?? SIG_IGN) != SIG_ERR);// ????????????д????
21     assert(signal(SIGTTOU?? SIG_IGN) != SIG_ERR);// ?????????д????
22     assert(signal(SIGTTIN?? SIG_IGN) != SIG_ERR);// ??????????????
23     assert(signal(SIGTERM?? SIG_IGN) != SIG_ERR);// ???
24
25     // [1] ??????????????????????
26     int pid = fork();
27     if (pid)
28     {
29         // ?????????
30         exit(0);
31     }
32     else if (pid < 0)
33     {
34         return false;
35     }
36
37     // ????????????
38
39     // [2] ????????д????μ????setsid??????????
40     // a.?y??????????????
41     // b.?y?????????????????
42     // c.?y??????????????????
43     int ret = setsid();
44     if (ret < 0)
45     {
46         return false;
47     }
48
49     // [3] ??????????′????????
50     // ??????????????????鳤?????????????????????????????
51     // ????????????????????鳤????????????′????????
52     pid = fork();
53     if (pid)
54     {
55         // ??????????????
56         exit(0);
57     }
58     else if (pid < 0)
59     {
60         return false;
61     }
62
63     // ?????????????????
64
65     // [4] ???????????????
66     // ??????????????????????????????????????????????????????????????
67     // ???????????????????ж???????????????????
68     for (int i = 0; i < NOFILE; ++i)
69     {
70         close(i);
71     }
72
73     // [5] ??????????
74     // ?????????乤??????????????????ж?£?????????????????
75     ret = chdir("/");
76     if (ret < 0)
77     {
78         return false;
79     }
80
81     // [6] ???????????????????
82     // ???????????????????????????????????????????????????????????????????λ
83     // ???????????????????
84     umask(0);
85
86     return true;
87 }
88
89 int main()
90 {
91     // ????????????
92     bool ret = initDaemon();
93     if (!ret)
94     {
95         printf("Init daemon failed ");
96         return 1;
97     }
98
99     FILE* file = NULL;
100     time_t t = 0;
101
102     // ???10????test.log??????????
103     while (true)
104     {
105         sleep(10);
106         file = fopen("./var/test.log"?? "a+");
107         if (file != NULL)
108         {
109             t = time(NULL);
110             fprintf(file?? "I am here at %s "?? asctime(localtime(&t)));
111             fclose(file);
112         }
113     }
114
115     return 0;
116 }
???????????github?????https://github.com/chxuan/samples/blob/master/Daemon/Daemon.cpp