473,385 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Odd behavior with os.fork and time.sleep

Yin
I am writing a script that monitors a child process. If the child
process dies on its own, then the parent continues on. If the child
process is still alive after a timeout period, the parent will kill the
child process. Enclosed is a snippet of the code I have written. For
some reason, unless I put in two time.sleep(4) commands in the parent,
the process will not sleep. Am I forgetting something? Any reasons
for this strange behavior?

Linux Fedora Core 4
Python 2.4.1

Thanks.

#!/usr/bin/env python

import os, time
import signal
import sys

def chldhandler(signum, stackframe):
while 1:
try:
result = os.waitpid(-1, os.WNOHANG)
except:
break
print "Reaped child process %s" % result[0]
signal.signal(signal.SIGCHLD, chldhandler)

signal.signal(signal.SIGCHLD, chldhandler)

pid = os.fork()
if pid: # parent
print "Parent. Child is %d" % pid

try:
os.kill(pid, 0)
print "Killed child"
os.kill(pid, signal.SIGKILL)
except OSError, err:
print "Child is dead"

print "In parent again"
time.sleep(4)
time.sleep(4)

print "Parent awake"
else: # child
print "Child sleeping 5 seconds..."
time.sleep(5)
print "Child awake"

Sep 16 '05 #1
1 2771
In article <11*********************@f14g2000cwb.googlegroups. com>,
"Yin" <yi*******@yahoo.com> wrote:
I am writing a script that monitors a child process. If the child
process dies on its own, then the parent continues on. If the child
process is still alive after a timeout period, the parent will kill the
child process. Enclosed is a snippet of the code I have written. For
some reason, unless I put in two time.sleep(4) commands in the parent,
the process will not sleep. Am I forgetting something? Any reasons
for this strange behavior?
.... signal.signal(signal.SIGCHLD, chldhandler)


If you can possibly revise your design to avoid the need
for this, by all means do so.

The SIGCHLD signal interrupts functions like sleep(), and
that's what you're seeing: the parent process didn't return
to its sleep after handling the signal. What's worse, it
affects other functions in a similar way, such as I/O. Try
to read some input from the terminal, instead if sleeping,
and you should crash with an EINTR error. Makes it harder
to write a reliable program, when you're inviting such trouble.

So far this is a low level UNIX issue that isn't peculiar to
Python, but Python adds to the difficulties just in the general
awkwardness of signal handling in an interpreted language,
where handlers may execute somewhat later than you would expect
from experience with lower level languages. And then if you
decide to add threads to the mix, there are even more issues
as signals may be delivered to one thread and handled in another,
etc.

If you're dispatching on I/O, for example with select, you
can use an otherwise unused pipe to notice the child fork's
exit -- close the parent's write end right away, and then
when the pipe becomes readable it must be because it closed
on child exit.

Donn Cave, do**@u.washington.edu
Sep 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jane Austine | last post by:
Running Python 2.3 on Win XP It seems like socket is working interdependently with subprocesses of the process which created socket. ------------------------------------ #the server side >>>...
4
by: Benoit Dejean | last post by:
hello, i have a question about forking processes atm, i have some code which i want to rewrite os.system("cd ~ && exec " + cmd + " & disown") i want to remove this os.system call
4
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
4
by: lasek | last post by:
Hi all, i have made a simple script that use fork, waitpid and sigaction for SIGCHLD. #include<sys/types.h> #include<sys/wait.h> #include<stdio.h> #include<errno.h> #include<unistd.h>...
1
by: Gvs | last post by:
Hi, I'm just playing around with forking processes and was wondering how can you keep a child alive as long as the parent is alive. To give an example here is my code. ...
2
by: Andy Carlson | last post by:
I have a strange problem. I am trying to use a global variable, but I can't get the second routine to recognize changes to the globabl. I finally realize, that I was forking, so the global was...
3
by: CMorgan | last post by:
Hi everybody, I am experiencing an annoying problem with fork() and execv(). In my program I need to launch the "pppd" from a thread, so, I create a new process with fork and then in the child...
3
by: RedWiz | last post by:
Hi i have to develop a multihreaded php application on linux, then through pcntl_fork and wait. I tried it, but there something going wrong, i think. The difference whit other languages like c...
1
by: 3srt | last post by:
Hello, I created a simple 'server' script that will accept input from a cgi script. The server will handle multiple connections on the same port, also. In a nut shell, this 'server' script will...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.