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

Hello, I would like to have two parallel processes. The first process which terminates should cause the other to be killed immediatly. A simple example is getting two files using the 'wget' command. The first process which terminate the download will cause the second wget to be stopped. Can you please advise how should i write this code as well ?

Replies are listed 'Best First'.
Re: kill thread or fork processes
by blazar (Canon) on May 16, 2006 at 13:37 UTC
      It would be a bit difficult for two childred to signal each other as they wouldn't know the other's PID. Better to have child and parent handle the two tasks: The parent gets the child's PID from the fork() call, and the child can get the parent's PID using ppid().

      Remember: There's always one more bug.

        That's what I meant. I guess... ;-)

Re: kill thread or fork processes
by Zaxo (Archbishop) on May 16, 2006 at 13:44 UTC

    I agree with blazar's basic approach.

    One thing about your problem. Why burn bandwidth on parallel downloads when you're going to give up on the slower one? I don't understand what that design gets you.

    After Compline,
    Zaxo

      this is only to simplify my question, i have 2 complex process and i don't know how to use the thread method and to monitor the status and kill
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: kill thread or fork processes
by BrowserUk (Patriarch) on May 16, 2006 at 17:46 UTC

    With threads, just run each process in it's own thread and then use exit to terminate. Whichever finishes first will terminate the other thread automatically.

    #! perl slw #use 5.008008; For "no warnings 'threads'" ## Doesn't work? use strict; use threads; no warnings 'threads'; async { no warnings 'threads'; sleep rand(10); ## One longe running process print 'Thread 2 finished first'; exit; }; sleep rand(10); ## another long running process print 'thread 1 finished first'; exit;

    The code will issue a warning A thread exited while 2 threads were running., but killing a thread this way is no worse that killing a process. From 5.8.8, no warnings 'threads'; is documented ... but not for this particular message it seems.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.