Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

I am not able to join a thread when another child process is running which was forked using open.

by ashishyadavlko1 (Initiate)
on Jan 26, 2012 at 07:21 UTC ( [id://950034]=perlquestion: print w/replies, xml ) Need Help??

ashishyadavlko1 has asked for the wisdom of the Perl Monks concerning the following question:

I want to do following things from main thread/process: 1. Communicate to another process using pipe. 2. Create threads to do certain task. 3. Wait for all the threads to complete. Following is the pseudo code I am trying:

use threads; use IO::Handle; sub dummy { print "\n!!!!". $$; return 0; } open($handle, "| cat -v") || die "Unable to open connection to BT Dri +ver: $!\n"; $handle->autoflush(1); #close $handle; If I uncomment this, threads can be joined. But I don +'t want to terminate this child process. $thr2 = threads->create(\&dummy); sleep 2; print "\n$thr2"; foreach $thr (threads->list(threads::joinable)) { print "\nIam here"; print "\n!!!". $thr; $thr->join(); }

Code gets stuck when I try to join the thread even though it is joinable. Am I doing something fundamentally wrong here? I am using Perl 5.10.0

  • Comment on I am not able to join a thread when another child process is running which was forked using open.
  • Download Code

Replies are listed 'Best First'.
Re: I am not able to join a thread when another child process is running which was forked using open.
by choroba (Cardinal) on Jan 26, 2012 at 09:36 UTC
    Crossposted at StackOverflow. It is more polite to inform people about the fact so they do not have to spend time on a problem already solved somewhere else.
Re: I am not able to join a thread when another child process is running which was forked using open.
by Anonymous Monk on Jan 26, 2012 at 08:19 UTC
Re: I am not able to join a thread when another child process is running which was forked using open.
by BrowserUk (Patriarch) on Jan 26, 2012 at 14:49 UTC

    Hm. Your code, with a couple of minor tweaks works fine for me:

    #! perl -slw use strict; use threads; use IO::Handle; sub dummy { print "\n!!!!". $$; return 0; } open( my $handle, "| cat -v") || die "Unable to open connection to BT +Driver: $!\n"; $handle->autoflush(1); #close $handle; If I uncomment this, threads can be joined. But I don' +t want to terminate this child process. my $thr2 = threads->create(\&dummy); sleep 2; print "\n$thr2"; for my $thr (threads->list(threads::joinable)) { print "\nIam here"; print "\n!!!". $thr; print $thr->join(); } __END__ C:\test>junk72 !!!!288 threads=SCALAR(0x37b820) Iam here !!!threads=SCALAR(0x367440) 0

    Try it. Then try and work out why.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: I am not able to join a thread when another child process is running which was forked using open.
by thargas (Deacon) on Jan 26, 2012 at 14:19 UTC
    from perlthrtut:
    Thinking of mixing "fork()" and threads? Please lie down and wait until the feeling passes. Be aware that the semantics of "fork()" vary between platforms. For example, some UNIX systems copy all the current threads into the child process, while others only copy the thread that called "fork()". You have been warned!
Re: I am not able to join a thread when another child process is running which was forked using open.
by zentara (Archbishop) on Jan 26, 2012 at 19:20 UTC
Re: I am not able to join a thread when another child process is running which was forked using open.
by sundialsvc4 (Abbot) on Jan 26, 2012 at 14:35 UTC

    Here is what I recommend:

    1. First, get your thoughts in order.   Devise what you want the end result to be, and avoid getting distracted by how to get to those results.
    2. Now, as for “how to get to those results,” start hauling out the big-guns like Parallel::ForkManager.

    What I am really trying to say here is that, “the real important thing about the Perl language is:   http://search.cpan.org.   Where, at this particular moment in time, you will find 75424 Uploads, 24,169 Distributions; 103,218 Modules; 9,487 Uploaders, literally about every conceivable subject or task including rocket-science.   Therefore, no matter what it is that you are doing, you do a lot less “original programming” and a lot more “picking and choosing.”   And so, that is what I think you should be doing here.

    When you install almost any of those packages onto your system, it subjects itself to hundreds or even thousands of self-tests.   It has been subjected (via the so-called CPANTS testing-service) to test-runs on a myriad of actual environments.   Links to this, and to bug and issue reports, are all there on the same page.   So, you have just been handed a magic bridge that runs over many stinking swamps of wasted time.

      And my recommendation to you is to take up knitting and avoid eating the yellow snow.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://950034]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-25 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found