1. 자식 프로세스 기다리기
부모 프로세스는 자식중에 하나가 종료되기를 기다릴수 있으며, 이때 os.wait() 를 실행시켜 자식의 종료코드를 받을수 있다.
형식 : (pid, status) = os.wait() # 자식 프로세스가 종료될때 까지 대기
- pid : 종료되는 자식 프로세스 - status : 종료코드(8bit) + 시그널번호(8bit)
예제1) 자식 프로세스 종료대기하기
import os
print 'Original Process PID : %s' % os.getpid()
pid = os.fork() # 여기서 프로세스가 두개로 분리
if pid :
# 부모 클래스
print 'I am Parent-Process PID = %s Child-Process = %s ' % (os.getpid(), pid)
pid, status = os.wait()
print ' Child-Process(%s) died with status(%s)' % (pid, status >> 8 )
else :
# 자식 프로세스
print 'I am Child-Process PID = %s, My Parent-Process = %s ' % (os.getpid(), os.getppid())
print 'Finish Child-Process(%s)' % os.getpid()
os._exit(42) # 자식 프로세스는 종료코드를 남기고 죽는다
print 'Original Process PID : %s' % os.getpid()
pid = os.fork() # 여기서 프로세스가 두개로 분리
if pid :
# 부모 클래스
print 'I am Parent-Process PID = %s Child-Process = %s ' % (os.getpid(), pid)
pid, status = os.wait()
print ' Child-Process(%s) died with status(%s)' % (pid, status >> 8 )
else :
# 자식 프로세스
print 'I am Child-Process PID = %s, My Parent-Process = %s ' % (os.getpid(), os.getppid())
print 'Finish Child-Process(%s)' % os.getpid()
os._exit(42) # 자식 프로세스는 종료코드를 남기고 죽는다
(실행결과)
[citylock@nmsfile thread]$ ./waitChildProcess.py
Original Process PID : 27107
I am Child-Process PID = 27108, My Parent-Process = 27107
Finish Child-Process(27108)
I am Parent-Process PID = 27107 Child-Process = 27108
Child-Process(27108) died with status(42)
Original Process PID : 27107
I am Child-Process PID = 27108, My Parent-Process = 27107
Finish Child-Process(27108)
I am Parent-Process PID = 27107 Child-Process = 27108
Child-Process(27108) died with status(42)
2. 고아프로세스
부모 프로세스가 자식프로세스보다 먼저 종료된 경우,
자식프로세스는 자신의 리턴값을 받아줄 새로운 부모프로세스가 필요하다. 이경우 pid = '1' 인 init 프로세스가 받게 된다.
( 참고로 init 프로세는 모든 프로세스의 조상 프로세스가 된다.)
예제2) 고아 프로세스의 부모프로세스 확인하기
import os
import time
print 'Original Process PID : %s ' % os.getpid()
pid = os.fork() # 프로세스 분리
if pid :
# Parent Process
print 'I am Parent-Process(%s), My Child-Process(%s)' % (os.getpid(), pid)
else :
# Child Process
print 'I am Child-Process(%s), My Parent-Process(%s)' % (os.getpid(),os.getppid())
time.sleep(5) # wait 5 sec. ( 5초 대기후 다시 확인)
print 'I am Child-Process(%s), My Parent-Process(%s)' % (os.getpid(),os.getppid())
(실행결과) import time
print 'Original Process PID : %s ' % os.getpid()
pid = os.fork() # 프로세스 분리
if pid :
# Parent Process
print 'I am Parent-Process(%s), My Child-Process(%s)' % (os.getpid(), pid)
else :
# Child Process
print 'I am Child-Process(%s), My Parent-Process(%s)' % (os.getpid(),os.getppid())
time.sleep(5) # wait 5 sec. ( 5초 대기후 다시 확인)
print 'I am Child-Process(%s), My Parent-Process(%s)' % (os.getpid(),os.getppid())
[citylock@nmsfile thread]$ ./orphanProcess.py
Original Process PID : 27164
I am Child-Process(27165), My Parent-Process(27164)
I am Parent-Process(27164), My Child-Process(27165)
[citylock@nmsfile thread]$ I am Child-Process(27165), My Parent-Process(1)
- 부모프로세스가 종료되면서 프로프트($)가 나오고 5초뒤에 자식 프로세스 종료 Original Process PID : 27164
I am Child-Process(27165), My Parent-Process(27164)
I am Parent-Process(27164), My Child-Process(27165)
[citylock@nmsfile thread]$ I am Child-Process(27165), My Parent-Process(1)
- 고아프로세스의 부모프로세스가 My Parent-Process(1) 로 확인
3. 좀비 프로세스
자식프로세스는 부모프로세스가 종료코드를 받아야지만 시스템을 떠날수 있다. 어떤 이유로 부모프로세시가 종료코드를 받지 않으면 무한 대기 상태로 빠지게 되는데 이를 좀비 프로세스라 한다. 좀비 프로세스는 코드, 자료, 스택 등 모든 자원을 반환한 상태이므로 자원을 낭비하지는 않지만 프로세스 제어블록을 점유하고 있기 때문에 많은 수의 좀비 가 존재한다면 반드시 처리해 주어야 한다.
예제3) 좀비 프로세스 확인하기
import os
import time
pid = os.fork() # 자식 프로세스 생성
if pid :
# 부모 프로세스
while 1:
time.sleep(10)
else :
# 자식 프로세스
os._exit(42)
(실행 결과)import time
pid = os.fork() # 자식 프로세스 생성
if pid :
# 부모 프로세스
while 1:
time.sleep(10)
else :
# 자식 프로세스
os._exit(42)
[citylock@nmsfile thread]$ ./zombie.py
=> 아무런 동작을 하지 않고 대기하고 있다. (프로세스 확인)
[citylock@nmsfile ~]$ ps -ef |grep zom
citylock 27210 26356 0 19:01 pts/1 00:00:00 vim zombie.py
citylock 27224 26467 0 19:05 pts/2 00:00:00 /usr/bin/python ./zombie.py
citylock 27225 27224 0 19:05 pts/2 00:00:00 [zombie.py] <defunct>
citylock 27255 27229 0 19:05 pts/3 00:00:00 grep zom
=> 좀비 프로세스의 경우 <defunct>로 표시된다. citylock 27210 26356 0 19:01 pts/1 00:00:00 vim zombie.py
citylock 27224 26467 0 19:05 pts/2 00:00:00 /usr/bin/python ./zombie.py
citylock 27225 27224 0 19:05 pts/2 00:00:00 [zombie.py] <defunct>
citylock 27255 27229 0 19:05 pts/3 00:00:00 grep zom