fork 사용법
fork(...)
fork() -> pid
Fork a child process.
Return 0 to child process and PID of child to parent process.
fork() -> pid
Fork a child process.
Return 0 to child process and PID of child to parent process.
execl 관련함수 사용법
execl(file, *args)
execl(file, *args)
Execute the executable file with argument list args, replacing the
current process.
※ exec* 관련 함수 : execl(file, *args), execle(file, *args), execlp(file, *args), execlpe(file, *args), execv(...), execve(...), execvp(file, args), execvpe(file, args, env) 등이 있다. 각 함수에 대한 내용은 help(os) 에서 exec 로 검색하면 확인할 수 있다. execl(file, *args)
Execute the executable file with argument list args, replacing the
current process.
예제1 ) os.fork 를 이용해서 자식 프로세스 생성
#!/usr/bin/python
# -*- coding: utf-8 -*-
# makeChildProcess.py : 자식 프로세스 생성
import os
print " Original Process ID : %s" % os.getpid()
pid = os.fork() # 프로세스가 복제 된다. pid 는 자식의 프로세스 ID
if pid :
print 'I am Parent Process(PID: %s), My Child Process(PID: %s)' % (os.getpid(), pid)
else :
print 'I am Child Process(PID: %s), My Parent Prcess(PID: %s)' % (os.getpid(), os.getppid())
print 'Finish : PID=%s' % os.getpid()
※ fork() 가 성공하면 부모프로세스는 자식PID를 받으며, 자식프로세스는 0 을 리턴 받는다. # -*- coding: utf-8 -*-
# makeChildProcess.py : 자식 프로세스 생성
import os
print " Original Process ID : %s" % os.getpid()
pid = os.fork() # 프로세스가 복제 된다. pid 는 자식의 프로세스 ID
if pid :
print 'I am Parent Process(PID: %s), My Child Process(PID: %s)' % (os.getpid(), pid)
else :
print 'I am Child Process(PID: %s), My Parent Prcess(PID: %s)' % (os.getpid(), os.getppid())
print 'Finish : PID=%s' % os.getpid()
(실행결과)
[citylock@nmsfile thread]$ ./makeChildProcess.py
Original Process ID : 26753
I am Child Process(PID: 26754), My Parent Prcess(PID: 26753)
Finish : PID=26754
I am Parent Process(PID: 26753), My Child Process(PID: 26754)
Finish : PID=26753
Original Process ID : 26753
I am Child Process(PID: 26754), My Parent Prcess(PID: 26753)
Finish : PID=26754
I am Parent Process(PID: 26753), My Child Process(PID: 26754)
Finish : PID=26753
- 두개의 부모/자식 프로세는 동일한 환경 및 코드를 가지고 실행되며 단지 pid 에 의해서 구분된다.
- 두 프로세스는 분리된 스택, 자료공간을 갖는다.
fork() 의 경우는 자신의 프로세스를 복제하는 역할만 한다. 그래서 현재 프로세스를 다른 프로세스를 대치하고자 한다면 os.exec 를 이용한다.
예제2) os.exec 를 이용해서 현재 프로세스를 다른 프로세스 대치
import os
print 'ls를 실행하기 직전. 내 pid = %s' % os.getpid()
os.execl('/bin/ls', 'ls', '-l') # 다른 프로세스로 전환된다. 이후 코드는 진행하지 않는다.
print ' 절대로 실행되지 않는 코드 '
print 'ls를 실행하기 직전. 내 pid = %s' % os.getpid()
os.execl('/bin/ls', 'ls', '-l') # 다른 프로세스로 전환된다. 이후 코드는 진행하지 않는다.
print ' 절대로 실행되지 않는 코드 '
(실행결과)
[citylock@nmsfile thread]$ ./useExecFunc.py
ls를 실행하기 직전. 내 pid = 26867
합계 8
-rwxr-xr-x 1 citylock citylock 467 8월 25 16:34 makeChildProcess.py
-rwxr-xr-x 1 citylock citylock 250 8월 25 16:38 useExecFunc.py
★ 현재 실행중인 프로세스가 os.execl() 에 의해 전환되면서 나머지 코드가 실행되지 않는다. ls를 실행하기 직전. 내 pid = 26867
합계 8
-rwxr-xr-x 1 citylock citylock 467 8월 25 16:34 makeChildProcess.py
-rwxr-xr-x 1 citylock citylock 250 8월 25 16:38 useExecFunc.py
예제3) 참고로, 일반적인 유닉스 시스템에서 새로운 프로세스를 생성할때는 fork() → exec() 이런 과정으로 진행한다.
import os
print 'ls를 실행하기 직전. 내 pid = %s' % os.getpid()
if os.fork() == 0 :
os.execl('/bin/ls', 'ls', '-l') # 자식 프로세스만 실행됨
print '부모 프로세스만 이 글을 출력한다. '
print 'ls를 실행하기 직전. 내 pid = %s' % os.getpid()
if os.fork() == 0 :
os.execl('/bin/ls', 'ls', '-l') # 자식 프로세스만 실행됨
print '부모 프로세스만 이 글을 출력한다. '
(실행 결과)
[citylock@nmsfile thread]$ ./useExecFuncTwo.py
ls를 실행하기 직전. 내 pid = 26891
부모 프로세스만 이 글을 출력한다.
합계 12
-rwxr-xr-x 1 citylock citylock 467 8월 25 16:34 makeChildProcess.py
[citylock@nmsfile thread]$ -rwxr-xr-x 1 citylock citylock 250 8월 25 16:42 useExecFunc.py
-rwxr-xr-x 1 citylock citylock 311 8월 25 16:45 useExecFuncTwo.py
ls를 실행하기 직전. 내 pid = 26891
부모 프로세스만 이 글을 출력한다.
합계 12
-rwxr-xr-x 1 citylock citylock 467 8월 25 16:34 makeChildProcess.py
[citylock@nmsfile thread]$ -rwxr-xr-x 1 citylock citylock 250 8월 25 16:42 useExecFunc.py
-rwxr-xr-x 1 citylock citylock 311 8월 25 16:45 useExecFuncTwo.py