Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Detecting problems in a forked process

Reply
Thread Tools

Detecting problems in a forked process

 
 
James Colannino
Guest
Posts: n/a
 
      12-29-2005
Hey everyone. I'm writing a small application in Python that uses
os.fork() to create a separate process in which another application is
run in the background. The problem is that I need to know whether or
not that separate application managed to start and return from within
the parent appropriately. Here's, roughly, a pseudo example of what I
want to do (I know the code itself is not correct):

def function():

pid = os.fork()

if pid:
do parent stuff
else:
try:
execute the application
exeption:
somehow return -1 from within the parent process

#try to start another application in the background from within a forked
process
if (function() == -1):
fail

Is there a way that I can do this? I thought, only for a *VERY* brief
second, about cheating my way around this with a global variable, but
then I realized that this wouldn't work due to the fact that I will have
multiple forks doing the same thing at the same time. Thanks in advance

James

--
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/

"If Carpenters made houses the way programmers design programs, the first woodpecker to come along would destroy all of civilization." --Computer Proverb

 
Reply With Quote
 
 
 
 
Jim Segrave
Guest
Posts: n/a
 
      12-29-2005
In article <(E-Mail Removed)>,
James Colannino <(E-Mail Removed)> wrote:
>Hey everyone. I'm writing a small application in Python that uses
>os.fork() to create a separate process in which another application is
>run in the background. The problem is that I need to know whether or
>not that separate application managed to start and return from within
>the parent appropriately. Here's, roughly, a pseudo example of what I
>want to do (I know the code itself is not correct):
>
>def function():
>
> pid = os.fork()
>
> if pid:
> do parent stuff
> else:
> try:
> execute the application
> exeption:
> somehow return -1 from within the parent process
>
>#try to start another application in the background from within a forked
>process
>if (function() == -1):
> fail
>
>Is there a way that I can do this? I thought, only for a *VERY* brief
>second, about cheating my way around this with a global variable, but
>then I realized that this wouldn't work due to the fact that I will have
>multiple forks doing the same thing at the same time. Thanks in advance


options:

Have the child set it's exit code to indicate success or failure and
use one of the various os.wait functions in the parent to retrieve it.

or

create a pipe before forking and you can pass data back and forth
between the parent and child

or

look at one of the shared memory libraries



--
Jim Segrave ((E-Mail Removed))

 
Reply With Quote
 
 
 
 
Donn Cave
Guest
Posts: n/a
 
      12-30-2005
Quoth Jim Segrave <(E-Mail Removed)>:
| In article <(E-Mail Removed)>,
| James Colannino <(E-Mail Removed)> wrote:
|> Hey everyone. I'm writing a small application in Python that uses
|> os.fork() to create a separate process in which another application is
|> run in the background. The problem is that I need to know whether or
|> not that separate application managed to start and return from within
|> the parent appropriately.
....
|> Is there a way that I can do this? I thought, only for a *VERY* brief
|> second, about cheating my way around this with a global variable, but
|> then I realized that this wouldn't work due to the fact that I will have
|> multiple forks doing the same thing at the same time. Thanks in advance

If your thought there had continued for another second or two, you
probably would have wondered whether variables are shared between
forks. (Actually they are, but only as an optimization - changes
are not shared.)

| options:
|
| Have the child set it's exit code to indicate success or failure and
| use one of the various os.wait functions in the parent to retrieve it.

This is the simplest, as long as it's easy to wait long enough to reliably
catch the failure case.

| create a pipe before forking and you can pass data back and forth
| between the parent and child

If you want the parent to wait for errors right up to the successful
execve(), and then continue, you can set the 'close on exec' flag on
the pipe write end file descriptor and make sure no process holds
this open but the child fork. See the subprocess module for example
code (or just use the subprocess module, if it's supported in the
deployed Python version.)

Donn Cave, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dbx/gdb Choosing Which Forked Process To Follow clusardi2k@aol.com C++ 2 04-13-2005 02:19 PM
dbx/gdb Choosing Which Forked Process To Follow clusardi2k@aol.com C Programming 2 04-13-2005 02:19 PM
fork(): how can I kill forked process and all ITS children, but leaveparent alive? Alexander N. Spitzer C Programming 1 11-12-2004 01:25 PM
Win32 - forked process doesn't create a Window RL Perl 2 02-06-2004 04:41 AM
How to tell if a forked process is done? John Lin Python 19 09-25-2003 04:22 PM



Advertisments