[英]Which process is getting exited and why is close(0) call followed after exit(0)
if (log_daemon) {
pid_t pid;
log_init();
pid = fork();
if (pid < 0) {
log_error("error starting daemon: %m");
exit(-1);
} else if (pid)
exit(0);
close(0);
open("/dev/null", O_RDWR);
dup2(0, 1);
dup2(0, 2);
setsid();
if (chdir("/") < 0) {
log_error("failed to set working dir to /: %m");
exit(-1);
}
}
I have above c program, and couldnt figure out what does the exit(0);
我有上面的C程序,无法找出
exit(0);
do in this case, which process does it exit? 在这种情况下,退出哪个进程? and what does the
close(0);
以及
close(0);
是什么close(0);
is followed for? 是为了? Will the
close(0);
请
close(0);
even execute? 甚至执行?
Is this code just to test whether a child process can be created? 这段代码是否只是为了测试是否可以创建子进程?
UPDATE: ok, I got it from this question Start a process in the background in Linux with C . 更新:好的,我是从这个问题中得到的。 在Linux中使用C在后台启动一个进程 。
Basically, close(0);
基本上,
close(0);
does is closes current standard input for child process and opens /dev/null
as input device. dos将关闭当前子进程的标准输入,并打开
/dev/null
作为输入设备。 This way child process will behave as a deamon and will not read anything from terminal or standard input. 这样子进程将表现为守护进程,并且不会从终端或标准输入中读取任何内容。
The fork returns the process id in the parent process, and 0
in the child process. 分支在父进程中返回进程ID,在子进程中返回
0
。 The main calling process is exiting because pid == 0
so if (pid)
is true in the parent, and false in the child. 主调用进程正在退出,因为
pid == 0
因此if (pid)
在父级中为true,在子级中为false。 The child then proceeds to close(0)
, etc. 然后,孩子进入
close(0)
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.