confused with os.fork()

Discussion in 'Python' started by Sandy, Nov 25, 2009.

  1. Sandy

    Sandy Guest

    Hi all,
    I am a little bit confused about os.fork().
    Say I have the following code.

    import os
    a = ['a','b','c','d','e']

    for i in xrange(len(a)):
    pid = os.fork()
    if not pid:
    print a
    os._exit(0)

    From most of the tuts and examples I saw online, I expect it to print
    a,b,c,d,e.
    Sometimes (very rare) it prints something like this:
    ab
    c
    d
    e
    I thought there is no way a parent process can enter the 'if'.
    Can anyone explain this behaviour? Is it the case where parent is
    forking a child even before the previous child is printing? In that
    case is there a way to prevent that? I can use os.wait(), but I don't
    want to wait till the child is finished, just don't want to mix the
    child processes, that's it.

    - dksr
    Sandy, Nov 25, 2009
    #1
    1. Advertising

  2. Sandy schrieb:
    > Hi all,
    > I am a little bit confused about os.fork().
    > Say I have the following code.
    >
    > import os
    > a = ['a','b','c','d','e']
    >
    > for i in xrange(len(a)):
    > pid = os.fork()
    > if not pid:
    > print a
    > os._exit(0)
    >
    > From most of the tuts and examples I saw online, I expect it to print
    > a,b,c,d,e.
    > Sometimes (very rare) it prints something like this:
    > ab
    > c
    > d
    > e
    > I thought there is no way a parent process can enter the 'if'.
    > Can anyone explain this behaviour? Is it the case where parent is
    > forking a child even before the previous child is printing? In that
    > case is there a way to prevent that? I can use os.wait(), but I don't
    > want to wait till the child is finished, just don't want to mix the
    > child processes, that's it.


    Yes, that's the case - you have a race-condition here. Two childs at the
    same time write, interleaving their data.

    To prevent this, you can use file-locking.

    http://docs.python.org/library/fcntl.html#fcntl.lockf

    Diez
    Diez B. Roggisch, Nov 25, 2009
    #2
    1. Advertising

  3. Sandy

    MRAB Guest

    Sandy wrote:
    > Hi all,
    > I am a little bit confused about os.fork().
    > Say I have the following code.
    >
    > import os
    > a = ['a','b','c','d','e']
    >
    > for i in xrange(len(a)):
    > pid = os.fork()
    > if not pid:
    > print a
    > os._exit(0)
    >
    >>From most of the tuts and examples I saw online, I expect it to print

    > a,b,c,d,e.
    > Sometimes (very rare) it prints something like this:
    > ab
    > c
    > d
    > e
    > I thought there is no way a parent process can enter the 'if'.
    > Can anyone explain this behaviour? Is it the case where parent is
    > forking a child even before the previous child is printing? In that
    > case is there a way to prevent that? I can use os.wait(), but I don't
    > want to wait till the child is finished, just don't want to mix the
    > child processes, that's it.
    >

    The parent isn't entering the 'if' statement. os.fork() simply forks the
    process and then both the parent and the child processes continue.

    All of the child processes run independently and there's no guarantee as
    to the order in which they'll print, and there's nothing to stop the
    printouts from being mixed together (you would need to protect the
    'print' with a mutex to do that).
    MRAB, Nov 25, 2009
    #3
  4. Sandy

    Aahz Guest

    In article <>,
    Dennis Lee Bieber <> wrote:
    >On Wed, 25 Nov 2009 13:52:09 -0800 (PST), Sandy <>
    >declaimed the following in gmane.comp.python.general:
    >>
    >> Sometimes (very rare) it prints something like this:
    >> ab
    >> c
    >> d
    >> e

    >
    > Not
    >
    >ab
    >
    >c
    >d
    >e
    >
    >?


    That's what I would guess, too.

    > Since all the forked processes are sharing the same output device,
    >it is quite possible that their output is getting interleaved.


    Actually, I am a tiny bit surprised; I would have expected that a single
    output call at the C level would not get interleaved. Of course, it's
    quite likely that the string and the \n are separate API calls.
    --
    Aahz () <*> http://www.pythoncraft.com/

    The best way to get information on Usenet is not to ask a question, but
    to post the wrong information.
    Aahz, Dec 3, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CwK

    fork

    CwK, Dec 22, 2003, in forum: Perl
    Replies:
    2
    Views:
    917
    Misha Gale
    Jan 4, 2004
  2. Josh Denny

    fork in perl 5.8.3 on windows

    Josh Denny, Mar 2, 2004, in forum: Perl
    Replies:
    2
    Views:
    6,633
    Jim Gibson
    Mar 2, 2004
  3. Patrick
    Replies:
    1
    Views:
    509
  4. xchris
    Replies:
    5
    Views:
    4,114
  5. Eric Snow

    os.fork and pty.fork

    Eric Snow, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    560
    Eric Snow
    Jan 8, 2009
Loading...

Share This Page