473,386 Members | 1,766 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,386 software developers and data experts.

fork, exec, and disown

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
def fork_exec_disown(cmd, dir="~"):

if os.fork()==0:

os.chdir(os.path.expanduser(dir))
os.setsid()
os.umask(0)

if os.fork():
sys.exit(0)

cmd = cmd.split()
os.execvp(cmd[0], cmd)

but i am not sure that this have the same behaviour.

is the second fork needed ? i think so to detach the spawn process
i have found in the os doc, os.setsid, i think i have to use it.
am i going wrong or is this simple
def fork_exec_disown(cmd, dir="~"):

if os.fork()==0:
os.chdir(os.path.expanduser(dir))
cmd = cmd.split()
os.execvp(cmd[0], cmd)

a good replacement for my os.system call

thank you
Jul 18 '05 #1
4 5876
>>>>> "Benoit" == Benoit Dejean <bn**@ifrance.com> writes:

Benoit> hello, i have a question about forking processes atm, i have
Benoit> some code which i want to rewrite

Benoit> os.system("cd ~ && exec " + cmd + " & disown")

Benoit> i want to remove this os.system call

Benoit> def fork_exec_disown(cmd, dir="~"):
Benoit> if os.fork()==0:
Benoit> os.chdir(os.path.expanduser(dir)) os.setsid() os.umask(0)
Benoit> if os.fork(): sys.exit(0)
Benoit> cmd = cmd.split() os.execvp(cmd[0], cmd)

Benoit> but i am not sure that this have the same behaviour. is the
Benoit> second fork needed ? i think so to detach the spawn process i
Benoit> have found in the os doc, os.setsid, i think i have to use it.

Benoit> am i going wrong or is this simple

Benoit> def fork_exec_disown(cmd, dir="~"):
Benoit> if os.fork()==0: os.chdir(os.path.expanduser(dir)) cmd =
Benoit> cmd.split() os.execvp(cmd[0], cmd)

Benoit> a good replacement for my os.system call

You can do that, but if you run ps you'll notice that all you processes get
into a Z (zombie), <defunct> state. Sooner or later you'll have the fork()
giving you error that "resource temporarily not available" because all
process numbers are used up. With the "double fork" technique you can avoid
this by adding a wait at the end:

def fork_exec_disown(cmd, dir="~"):
if os.fork() == 0:
if os.fork():
sys.exit(0)
os.chdir(os.path.expanduser(dir))
cmd = cmd.split()
os.execvp(cmd[0], cmd)
os.wait()

The wait will wait only for the child, not the grand-child. If you don't
have that you'll start accumulating zombies. In some OS you can avoid the
double forking overhead by manipulating the signal handler of SIGCHLD (so
that it has the flag SA_NOCLDWAIT and has handler SIG_IGN), but that is more
platform dependent than fork().

Regards,
Isaac.
Jul 18 '05 #2
Le Sun, 08 Feb 2004 22:22:37 +0800, Isaac To a écrit*:

You can do that, but if you run ps you'll notice that all you processes
get into a Z (zombie), <defunct> state. Sooner or later you'll have the
fork() giving you error that "resource temporarily not available"
because all process numbers are used up.
you're right, i haven't noticed that
With the "double fork" technique you can avoid this by adding a wait at
the end:

def fork_exec_disown(cmd, dir="~"):
if os.fork() == 0:
if os.fork():
sys.exit(0)
os.chdir(os.path.expanduser(dir))
cmd = cmd.split()
os.execvp(cmd[0], cmd)
os.wait()
ok, i use this
The wait will wait only for the child, not the grand-child. If you
don't have that you'll start accumulating zombies. In some OS you can
avoid the double forking overhead by manipulating the signal handler of
SIGCHLD (so that it has the flag SA_NOCLDWAIT and has handler SIG_IGN),
but that is more platform dependent than fork().


could you tell me more about that ?
because i am really interested in. i want to have the best/fastest
replacement for os.system

thank you
Jul 18 '05 #3
>>>>> "Benoit" == Benoit Dejean <bn**@ifrance.com> writes:

Benoit> Le Sun, 08 Feb 2004 22:22:37 +0800, Isaac To a écrit*:
You can do that, but if you run ps you'll notice that all you
processes get into a Z (zombie), <defunct> state. Sooner or later
you'll have the fork() giving you error that "resource temporarily
not available" because all process numbers are used up.
Benoit> you're right, i haven't noticed that
With the "double fork" technique you can avoid this by adding a wait
at the end:

def fork_exec_disown(cmd, dir="~"): if os.fork() == 0: if os.fork():
sys.exit(0) os.chdir(os.path.expanduser(dir)) cmd = cmd.split()
os.execvp(cmd[0], cmd) os.wait()
Benoit> ok, i use this
The wait will wait only for the child, not the grand-child. If you
don't have that you'll start accumulating zombies. In some OS you
can avoid the double forking overhead by manipulating the signal
handler of SIGCHLD (so that it has the flag SA_NOCLDWAIT and has
handler SIG_IGN), but that is more platform dependent than fork().


Benoit> could you tell me more about that ? because i am really
Benoit> interested in. i want to have the best/fastest replacement for
Benoit> os.system

E.g., in my Linux box, the following won't leave any zombie:

import os
import time
import signal
signal.signal(signal.SIGCHLD, signal.SIG_IGN)
def fork_exec_disown(cmd, dir="~"):
if os.fork() == 0:
os.chdir(os.path.expanduser(dir))
cmd = cmd.split()
os.execvp(cmd[0], cmd)
for i in xrange(1000):
fork_exec_disown("true")
time.sleep(100)

On the other hand, the effect is global, so once you do that you cannot
expect you can wait for process that have already been dead. E.g., the
following no longer work:
def f(): .... pid = os.fork()
.... if pid == 0:
.... os.execlp("true", "true")
.... time.sleep(1)
.... os.wait()
.... f()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 6, in f
OSError: [Errno 10] No child processes

So double forking has its merits, since it is per-child rather than
per-parent. And modern OS actually do fork() rather efficiently anyway.

Regards,
Isaac.
Jul 18 '05 #4
> So double forking has its merits, since it is per-child rather than
per-parent. And modern OS actually do fork() rather efficiently anyway.


thank you, i'll keep the double fork.

Jul 18 '05 #5

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

Similar topics

3
by: Lingyun Yang | last post by:
Hi, I want to use python as a "shell like" program, and execute an external program in it( such as mv, cp, tar, gnuplot) I tried: os.execv("/bin/bash",("/usr/bin/gnuplot",'-c "gnuplot <...
2
by: blah | last post by:
i m using Windows XP, and by tomorrow i will have have fedora core installed too. the problem is, when i use these "fork() and exec()" my windows doesnt do anything, python gives an error about the...
1
by: Alexander N. Spitzer | last post by:
I am trying to write a program that will fork a process, and execute the given task... the catch is that if it runs too long, I want to clean it up. this seemed pretty straight forward with a...
3
by: thrillseekersforever | last post by:
The questions(A&B) are to fine no# of process running from the below codes. However, I couldn't decipher the solution. Will someone please throw some light on this? Thanks a lot!! A] void...
0
by: rahulthathoo | last post by:
Hi, I have to call a perl program from within PHP, but since that program takes a long time to finish, i fork a process in my php code and then call the perl code, using exec. But I am not able...
2
by: Rafael Giannetti Viotti | last post by:
Hi, I am working with the subprocess.py module in Python 2.4.4 and I am confused about it's functionality. It uses the standard pipe-fork-exec method to start a subprocess: # create pipes ...
3
by: Dan Upton | last post by:
I have a Python script that does a fork/exec, so the parent process can get the child's PID and monitor /proc/PID/stat (on a CentOS system). Most of my processes' command lines are straightforward...
5
by: Jatek | last post by:
Hi i have learned how fork.pl and exec.pl work as a starting point,but i want to create a program called shell.pl that will repeatedly read a line of user inpu until the user enters CTRL-D (end of...
5
by: boddah | last post by:
Hi all, I'm writing a manager application (Linux/C++) that'll spawn 2 instances of a program (myApp). The myApp program needs to stay alive after being spawned (both instances) so a 3rd party...
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.