|
- #include <stdio.h>
- #include <signal.h>
- #include <syslog.h>
- #include <fcntl.h>
- #include <sys/resource.h>
- #include <stdlib.h>
- void deamonize(const char *cmd)
- {
- int i = 0;
- int fd0 = 0;
- int fd1 = 0;
- int fd2 = 0;
- pid_t pid;
- struct sigaction sa;
- struct rlimit rl;
- umask(0);
- getrlimit(RLIMIT_NOFILE, &rl);
- pid = fork();
- if (pid < 0)
- {
- exit(0);
- }
- else if (pid != 0)
- {
- exit(0);
- }
- setsid();
- sa.sa_handler = SIG_IGN;
- sigemptyset(&sa.sa_mask);
- sa.sa_flags = 0;
- sigaction(SIGHUP, &sa, NULL);
- pid = fork();
- if (pid < 0)
- {
- exit(0);
- }
- else if (pid != 0)
- {
- exit(0);
- }
-
- chdir("/");
- if (rl.rlim_max = RLIM_INFINITY)
- {
- rl.rlim_max = 1024;
- }
- for (i = 0; i < 1024; i++)
- {
- close (i);
- }
- fd0 = open ("/dev/null", O_RDWR);
- fd1 = dup (0);
- fd2 = dup (0);
- openlog(cmd, LOG_CONS, LOG_DAEMON);
-
- if (fd0 != 0 || fd1 != 1 || fd2 != 2)
- {
- syslog(LOG_ERR, "%d, %d, %d", fd0, fd1, fd2);
- exit (1);
- }
- }
- int main ()
- {
- deamonize("=.=");
- }
复制代码
APUE2里面的例子
为什么我用 ps axj 看不到我这个精灵进程呢??
问题出在什么地方了? |
|