Skip to content

Instantly share code, notes, and snippets.

@quiver
Created November 23, 2012 06:15
Show Gist options
  • Save quiver/4134221 to your computer and use it in GitHub Desktop.
Save quiver/4134221 to your computer and use it in GitHub Desktop.
[python]demo of os.wait3/4:with wait3/4 you can retrieve resource usage information about the child processes.
# RESOURCE
- WAIT4(2)
http://www.kernel.org/doc/man-pages/online/pages/man2/wait4.2.html
- GETRUSAGE(2)
http://www.kernel.org/doc/man-pages/online/pages/man2/getrusage.2.html
- The Linux Programming Interface
§26.1.6 The wait3() and wait4() System Calls
- Python Documentation
http://docs.python.org/2/library/os.html
http://docs.python.org/2/library/resource.html
# USAGE
arguments are time to sleep in second for each child process.
$ python demo_wait4.py 10 30 &
[1] 26114
forked child process PID=26120
forked child process PID=26119
child process=>pid:pgid=26119:26114
resource.struct_rusage(ru_utime=0.0, ru_stime=0.0, ru_maxrss=4376, ru_ixrss=0, ru_idrss=0, ru_isrss=0, ru_minflt=192, ru_majflt=0, ru_nswap=0, ru_inblock=0, ru_oublock=0, ru_msgsnd=0, ru_msgrcv=0, ru_nsignals=0, ru_nvcsw=2, ru_nivcsw=2)
WIFEXITED
$ kill -STOP 26120
resource.struct_rusage(ru_utime=0.0, ru_stime=0.0, ru_maxrss=4196, ru_ixrss=0, ru_idrss=0, ru_isrss=0, ru_minflt=182, ru_majflt=0, ru_nswap=0, ru_inblock=0, ru_oublock=0, ru_msgsnd=0, ru_msgrcv=0, ru_nsignals=0, ru_nvcsw=2, ru_nivcsw=2)
WIFSTOPPED
$ kill -CONT 26120
resource.struct_rusage(ru_utime=0.0, ru_stime=0.0, ru_maxrss=4196, ru_ixrss=0, ru_idrss=0, ru_isrss=0, ru_minflt=182, ru_majflt=0, ru_nswap=0, ru_inblock=0, ru_oublock=0, ru_msgsnd=0, ru_msgrcv=0, ru_nsignals=0, ru_nvcsw=3, ru_nivcsw=2)
WIFCONTINUED
child process=>pid:pgid=26120:26114
resource.struct_rusage(ru_utime=0.0, ru_stime=0.0, ru_maxrss=4376, ru_ixrss=0, ru_idrss=0, ru_isrss=0, ru_minflt=191, ru_majflt=0, ru_nswap=0, ru_inblock=0, ru_oublock=0, ru_msgsnd=0, ru_msgrcv=0, ru_nsignals=0, ru_nvcsw=4, ru_nivcsw=2)
WIFEXITED
[1]+ Done python demo_wait4.py 10 30
# vim: set fileencoding=utf8
# WAIT4(2)
# wait3, wait4 - wait for process to change state, BSD style
#
# usage
# $ python demo_wait4.py 1 2 5
import argparse
import os
import sys
import time
def main():
parser = argparse.ArgumentParser()
parser.add_argument('sleep_times', metavar='N', type=int,
nargs='+', help='time to sleep')
args = parser.parse_args()
pgid = os.getpgrp()
for sleep_time in args.sleep_times:
pid = os.fork()
if pid < 0:
sys.exit(-1)
elif pid == 0: # child
print 'forked child process PID=%d' % os.getpid()
# do something
time.sleep(sleep_time)
print 'child process=>pid:pgid=%d:%d' % (os.getpid(), os.getpgrp())
os._exit(0)
else: # parent
os.setpgid(pid, pgid)
options = os.WUNTRACED | os.WCONTINUED
while True:
try:
#########################################
# wait3(2)
#########################################
pid, status, resource = os.wait3(options) # reap any child process
print resource
# is equivalent to
#pid, status = os.waitpid(-1, options) # pid=-1 means any child process
#########################################
# wait4(2)
#########################################
#pid, status, resource = os.wait4(-pgid, options) # reap child process specified with process id
#print resource
# is equivalent to
#pid, status = os.waitpid(-pgid, options)
if status == -1:
sys.stderr('waitpid')
sys.exit(1)
elif os.WIFEXITED(status):
print 'WIFEXITED'
elif os.WIFSTOPPED(status):
print 'WIFSTOPPED'
elif os.WIFCONTINUED(status):
print 'WIFCONTINUED'
except OSError, err:
# [Errno 10] No child processes
break
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment