2014-10-28 1 views
1

posix_spawn()으로 다른 프로세스를 생성하는 프로세스가 하나 있습니다. 자식 프로세스가 자동 종료 될 때 그들은 기본 기대 동작 인 것처럼 좀비 상태에 들어갑니다. 나는 그것들을 완전히 종료시키고 싶지만 posix_spawnattr_setflags()를 사용하여 그것을 달성 할 수 없었다. 아마 나는 올바른 깃발을 사용하지 않을 것입니다. 그것을하는 방법을 아는 누군가?posix_spawn 리눅스 : 종료 할 때 아이들 프로세스가 좀비 상태가되지 않게하는 방법

P. 바람직하게는 나는 아이들 프로세스가 완전히 분리되고 부모 프로세스와 독립적이되도록하고 싶습니다.

+1

setsid() 시스템 호출은 하위 프로세스를 상위 프로세스에서 분리하여 독립 프로세스로 만들지 만 닫지 않은 경우 stdin/stdout을 공유합니다. – pelya

답변

0

부모는 자식을 기다리고 종료 상태를 읽어야합니다. "사람이 대기"에서 : 부모가 아직 살아 있다면 기본적으로

A child that terminates, but has not been waited for becomes a "zombie". 
The kernel maintains a minimal set of information about the zombie process (PID, 
termination status, resource usage information) in order to allow the parent to later 
perform a wait to obtain information about the child. As long as a zombie is not 
removed from the system via a wait, it will consume a slot in the kernel process 
table, and if this table fills, it will not be possible to create further 
processes. 
If a parent process terminates, then its "zombie" children (if any) are adopted by 
init(8), which automatically performs a wait to remove the zombies. 

POSIX.1-2001 specifies that if the disposition of SIGCHLD is set to SIG_IGN or the 
SA_NOCLDWAIT flag is set for SIGCHLD (see sigaction(2)), then children that terminate 
do not become zombies and a call to wait() or waitpid() will block until all 
children have terminated, and then fail with errno set to ECHILD. (The original POSIX 
standard left the behavior of setting SIGCHLD to SIG_IGN unspecified. Note that even 
though the default disposition of SIGCHLD is "ignore", explicitly setting the 
disposition to SIG_IGN results in different treatment of zombie process children.) 
Linux 2.6 conforms to this specification. However, Linux 2.4 (and earlier) does 
not: if a wait() or waitpid() call is made while SIGCHLD is being ignored, the 
call behaves just as though SIGCHLD were not being ignored, that is, the call blocks 
until the next child terminates and then returns the process ID and status of that 
child. 
+0

그는 과거에 두 번'fork()'를 사용하기를 원합니다. 'posix_spawn' 이전에 당신은'fork' +'exec'을해야만했고 이중 포크 트릭이 존재했습니다. 당신은 아이 A를 포크로 잡고 아이 A는 아이 B를 포크로 만들었습니다. 이제는 당신이 아이 A를 죽게하고 깨끗하게합니다. 결과는 자식 B가 부모를 잃어 버렸고 이제 좀비 정리를 수행 한 PID 1 프로세스 (init 또는 systemd)에서 채택되었으므로 프로세스가 자식 B 자체를 정리할 필요가 없으며 자식 B는 심지어 시작한 프로세스에서도 살아남을 수 있습니다. – Mecki

3

exit() 또는 _exit() 중 하나를 호출하면 좀비에 대한 호출 프로세스의 상태를 변경합니다.

#include <signal.h> 

struct sigaction arg = { 
    .sa_handler=SIG_IGN, 
    .sa_flags=SA_NOCLDWAIT // Never wait for termination of a child process. 
}; 

sigaction(SIGCHLD, &arg, NULL); 

부모 프로세스에 위의 코드를 작성 후, 아이를 기다릴하지 않습니다, 그리고 아이 대신 좀비의, 완료 후 종료 상태가됩니다 :이 설정을 변경하려면 sigaction()를 사용할 수 있습니다.

관련 문제