-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeamon-demo.c
60 lines (53 loc) · 1.29 KB
/
deamon-demo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Deamon Demo
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
int main(int argc, char* argv[])
{
FILE *fp = NULL;
pid_t process_id = 0;
pid_t sid = 0;
int count = 0;
// Create child process
process_id = fork();
if (process_id < 0)
{
printf("fork failed!\n");
exit(1);
}
if (process_id > 0)
{
printf("process_id of child process %d\n", process_id);
exit(0);
}
// unmask the file mode
umask(0);
sid = setsid();
if (sid < 0)
{
exit(1);
}
chdir("C:\\Boush");
// Close stdin, stdout and stderr
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
fp = fopen("Log.txt", "w+");
while (1)
{
count++;
sleep(1);
fprintf(fp, "=== Logging info...\n");
fflush(fp);
if (25 == count)
{
printf("=== deamon process stop...\n");
exit(0);
}
}
fclose(fp);
return 0;
}