in reply to Threads, bash, and networking

You might try using an arp ping instead of your more complicated setup. The arping utility does this. arping sends pings over ethernet asking "which mac address owns this ip address". This would eliminate the complicated step of running tcpdump.

Edit: arping would only work if your VMs are on the local network. This might be a silly assumption for me to make what with cloud computing and all. If your VMs aren't on the local network I would suggest using a piped open and select and maybe using Net::Ping instead of the ping command.

Here is a test I created for my own curiosity. The summary of my findings are:

Rather confusing. So basically, if you insist on using threads detach the thread after you set the alarm handler and alarm delay in the parent thread. This is kind of silly because you could detach the thread and just sleep, then kill the thread or whatever.

Lose the threads, lose tcpdump and ping, and use: `arping -c 1 $host`. Easy.

#!/usr/bin/perl use warnings; use strict; use threads; sub set_alarm { $SIG{'ALRM'} = sub { printf "Alarm went off in thread #%d\n", threads->tid(); print qq{"Get up, get up!", says the clock.\n}; exit 1; }; alarm shift; } # If you set the SIGALRM handler HERE and ... # If you join the thread, the alarm handler goes off after 10 se +conds # ( The 'sleep' delay set inside the child thread. ) # If you detach the thread, the alarm handler goes off after 3 sec +onds # ( The 'alarm' delay set inside the thread ) # Or If you don't set 'alarm' inside the thread, the outer 'al +arm' # delay is used. set_alarm( 5 ); my $sleepy = threads->create ( sub { # If the SIGALRM handler is used in + this thread # (comment out the set_alarm above) # it simply prints "Alarm clock" ! +hahahaha set_alarm( 3 ); sleep 10; print "Well, well, are you joining +me in bed?\n"; } ); printf "Main thread # is %d.\nChild thread # is %d.\n", threads->tid(), $sleepy->tid(); # Try commenting out detach/sleep and uncommenting join # $sleepy->join; $sleepy->detach; sleep 15; # you only need to sleep when detach-ing print "*Fart*\n"; # Luckily this never gets reached... print "Oh crap I'm late for work!\n";

Edit: To be clearer, SIGALRM handlers inside the child thread are apparently never reached. I also only ran this on Mac OS X, who knows you could get different results on different machines (Joy!).

Replies are listed 'Best First'.
Re^2: Threads, bash, and networking
by BrowserUk (Patriarch) on Oct 23, 2010 at 05:10 UTC
    To be clearer, SIGALRM handlers inside the child thread are apparently never reached.

    By that, I assume you mean that alarms raised in one thread are not caught in other threads.

    Why would you expect that they would be? Signals don't cross fork boundaries, so why expect they might cross thread boundaries?

    Remember, there is no parent-child relationship between threads, so if an alarm raised in one thread could be caught in a thread it spawned, it would also be caught by every other thread in the program.

    There would simply be no way to reason about a system that meant that every thread received every signal raised in any other thread.


    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.
      By that, I assume you mean that alarms raised in one thread are not caught in other threads.

      I mean if you comment out the top set_alarm() statement in my code, it will print "Alarm clock" and exit after 3 seconds. From this I am inferring that the created thread is successfully setting the alarm for 3 seconds. $SIG{ALRM} is set in the same created thread. The problem is the value of $SIG{ALRM} is never used. Instead the magic "Alarm clock" appears and terminates the program!

      This was also the first bullet-point of my conclusions:

      SIGALRM handlers inside threads are adorably useless. Put the SIGALRM handler in the main thread.

      So again, that wasn't my origin message. But since you mentioned it... alarms raised in the created thread were indeed caught by the main thread. In fact, according to my test program anyways, this is the only way alarm signals used in threads are functional at all.

      I would avoid using alarm signals in threads altogether. After trial and error, tweaking the code I posted, I have come to understand several nuances of alarm signals and threading. That is what I was trying to share. Hopefully from all the unpredictable behavior I have mentioned, readers can conclude that using alarm inside threads is not worth the headache.

        alarms raised in the created thread were indeed caught by the main thread.

        Which versions of Perl & threads?

        I ask, because that is not what I see:

        perl -Mthreads -E" $SIG{ALRM}=sub{die qq[main:Awooga!\n]}; async{ local $SIG{ALRM} = sub{ die qq[thread:Awooga\n] }; alarm 3; sleep 5 }->detach; sleep 7;" Thread 1 terminated abnormally: thread:Awooga

        The alarm goes off, the thread-local signal handler catches it and executes and dies, the main thread then completes in the normal way.