代码实现 main.c
#include
#include
#include
static int daemon_proc = 0;
#define MAXDF 64
int test_daemon(const char* pName, int facility)
{
int i;
pid_t pid;
if((pid = fork()) < 0)
{
return -1;
}
else if(pid)
{
_exit(0);
}
if((setsid() < 0))
{
return;
}
signal(SIGHUP,SIG_IGN);
if((pid = fork()) < 0)
{
return -1;
}
else if(pid)
{
_exit(0);
}
daemon_proc = 1;
chdir("/");
for(int i=0; i<MAXDF; ++i)
{
close(i);
}
int p1 = open("/dev/NULL",O_RDONLY);
int p2 = open("/dev/NULL",O_RDWR);
int p3 = open("/dev/NULL",O_RDWR);
openlog(pName,LOG_PID,facility);
syslog(LOG_DEBUG, "daemon create\r\n");
return 0;
}
int main(int argc, char **argv)
{
test_daemon("test_daemon",0);
while(1)
{
syslog(LOG_DEBUG, "daemon test\r\n");
}
return 0;
}
- 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
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
运行结果
