Threads After Fork

Discussion in 'Ruby' started by James Gray, Jan 20, 2009.

  1. James Gray

    James Gray Guest

    Am I understanding this example right:

    #!/usr/bin/env ruby -wKU

    printer = Thread.new do
    10.times do
    sleep 1
    puts "Thread running in #{Process.pid}..."
    end
    end

    fork do
    p [Process.pid, printer.status]
    printer.join
    end

    p [Process.pid, printer.status]
    printer.join
    # >> [457, "sleep"]
    # >> [458, false]
    # >> Thread running in 457...
    # >> Thread running in 457...
    # >> Thread running in 457...
    # >> Thread running in 457...
    # >> Thread running in 457...
    # >> Thread running in 457...
    # >> Thread running in 457...
    # >> Thread running in 457...
    # >> Thread running in 457...
    # >> Thread running in 457...

    __END__

    Are all other threads stopped as part of a process fork?

    James Edward Gray II
    James Gray, Jan 20, 2009
    #1
    1. Advertising

  2. James Gray

    Tim Pease Guest

    On Jan 20, 2009, at 8:55 AM, James Gray wrote:

    > Am I understanding this example right:
    >
    > #!/usr/bin/env ruby -wKU
    >
    > printer =3D Thread.new do
    > 10.times do
    > sleep 1
    > puts "Thread running in #{Process.pid}..."
    > end
    > end
    >
    > fork do
    > p [Process.pid, printer.status]
    > printer.join
    > end
    >
    > p [Process.pid, printer.status]
    > printer.join
    > # >> [457, "sleep"]
    > # >> [458, false]
    > # >> Thread running in 457...
    > # >> Thread running in 457...
    > # >> Thread running in 457...
    > # >> Thread running in 457...
    > # >> Thread running in 457...
    > # >> Thread running in 457...
    > # >> Thread running in 457...
    > # >> Thread running in 457...
    > # >> Thread running in 457...
    > # >> Thread running in 457...
    >
    > __END__
    >
    > Are all other threads stopped as part of a process fork?
    >


    =46rom the fork documentation ....

    "The thread calling fork is the only thread in the created child =20
    process. fork doesn=92t copy other threads."

    And I modified your script slightly so things are a little more =20
    clear ...


    #!/usr/bin/env ruby -wKU

    p "parent: #{Process.pid}"

    printer =3D Thread.new do
    10.times do
    sleep 1
    puts "Thread running in #{Process.pid}..."
    end
    end

    fork do
    p "child: #{Process.pid}"
    p [Process.pid, printer.status]
    printer.join
    end

    p [Process.pid, printer.status]
    printer.join


    # >> "parent: 9409"
    # >> [9409, "sleep"]
    # >> "child: 9410"
    # >> [9410, false]
    # >> Thread running in 9409...
    # >> Thread running in 9409...
    # >> Thread running in 9409...
    # >> Thread running in 9409...
    # >> Thread running in 9409...
    # >> Thread running in 9409...
    # >> Thread running in 9409...
    # >> Thread running in 9409...
    # >> Thread running in 9409...
    # >> Thread running in 9409...


    So when the child is forked, only the parent thread is copied over and =20=

    the "printer" thread has been killed in the child. Hence, you get the =20=

    "false" response for the status. And I'll stop being pedantic now ;)

    Blessings,
    TwP=
    Tim Pease, Jan 20, 2009
    #2
    1. Advertising

  3. James Gray

    James Gray Guest

    On Jan 20, 2009, at 1:42 PM, Tim Pease wrote:

    > On Jan 20, 2009, at 8:55 AM, James Gray wrote:
    >
    >> Am I understanding this example right:
    >>
    >> #!/usr/bin/env ruby -wKU
    >>
    >> printer =3D Thread.new do
    >> 10.times do
    >> sleep 1
    >> puts "Thread running in #{Process.pid}..."
    >> end
    >> end
    >>
    >> fork do
    >> p [Process.pid, printer.status]
    >> printer.join
    >> end
    >>
    >> p [Process.pid, printer.status]
    >> printer.join
    >> # >> [457, "sleep"]
    >> # >> [458, false]
    >> # >> Thread running in 457...
    >> # >> Thread running in 457...
    >> # >> Thread running in 457...
    >> # >> Thread running in 457...
    >> # >> Thread running in 457...
    >> # >> Thread running in 457...
    >> # >> Thread running in 457...
    >> # >> Thread running in 457...
    >> # >> Thread running in 457...
    >> # >> Thread running in 457...
    >>
    >> __END__
    >>
    >> Are all other threads stopped as part of a process fork?
    >>

    >
    > =46rom the fork documentation ....
    >
    > "The thread calling fork is the only thread in the created child =20
    > process. fork doesn=92t copy other threads."


    Man, I swear I've read that documentation 400 times now and just keep =20=

    missing that. Thanks for the reading lesson Tim!

    James Edward Gray II=
    James Gray, Jan 20, 2009
    #3
  4. Hi,

    Am Mittwoch, 21. Jan 2009, 04:48:14 +0900 schrieb James Gray:
    > On Jan 20, 2009, at 1:42 PM, Tim Pease wrote:
    >> On Jan 20, 2009, at 8:55 AM, James Gray wrote:
    >>
    >>> printer =3D Thread.new do
    >>> 10.times do
    >>> sleep 1
    >>> puts "Thread running in #{Process.pid}..."
    >>> end
    >>> end
    >>>
    >>> fork do
    >>> p [Process.pid, printer.status]
    >>> printer.join
    >>> end
    >>>
    >>> Are all other threads stopped as part of a process fork?

    >>
    >> fork doesn=E2=80=99t copy other threads.

    >
    > Man, I swear I've read that documentation 400 times now and just keep=20
    > missing that.


    A soothing fact that even the well-experienced Ruby programmer
    sometimes detects new aspects.

    I think the amazing strike is that the variable containing the
    Thread instance still exists whilst the Thread will surely be
    dead. Joining a dead thread does just nothing. I reduced it to
    these two lines:

    t =3D Thread.new do sleep 100 end
    fork do puts t.inspect end

    Output:

    #<Thread:0x28456bcc dead>

    Bertram


    --=20
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
    Bertram Scharpf, Jan 21, 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. =?ISO-8859-1?Q?Andreas_M=FCller?=

    fork or threads

    =?ISO-8859-1?Q?Andreas_M=FCller?=, May 10, 2005, in forum: C++
    Replies:
    3
    Views:
    6,395
    Kanenas
    May 18, 2005
  2. Ajay Bakhshi
    Replies:
    0
    Views:
    405
    Ajay Bakhshi
    May 3, 2004
  3. Eric Snow

    os.fork and pty.fork

    Eric Snow, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    571
    Eric Snow
    Jan 8, 2009
  4. grocery_stocker
    Replies:
    12
    Views:
    412
  5. Tomasz Pajor

    fork, threads and proper closing

    Tomasz Pajor, Jun 28, 2009, in forum: Python
    Replies:
    5
    Views:
    251
    Lawrence D'Oliveiro
    Jun 30, 2009
Loading...

Share This Page