Hi all,

Going through several posts here and thanks to BrowserUK esp., I managed to get a daemon working (implemented with version 5.8.4 and threads-v1.03 )

While trying to implement a clean "exit" in the daemon, I ran into some trouble as the version of Perl threads did not support calls like $thr->exit()/kill()

So what happens now is the I get the dreaded "..exited with 2 threads running" message.

Unfortunately I cannot post code as is but a sketch has been provided which gives a good idea of the implementation.

The box is a Linux OS and shutdown/reboot sends a SIGTERM to the process So what I did is to use the sigtrap handler and defined a function which traps the call. Now, the function should

  • a) Write a timestamp to the log file so that the time it was stopped is recorded.
  • b) Close all the threads excluding the main thread
  • c) CLose all filehandles
  • b) Call exit.

    Version 5.8.4 of Perl does not have the features to send kill or use the async thread object to call an exit.

    Question:

  • Why does "top" show me three process running even though no Login threads are running>
  • How can I ensure the main thread shuts down other threads before shutting itself down with exit.

    Thanks/Merci/Gracie/Shukriyah!
    #!/usr/bin/perl -w use sigtrap 'handler' => \&shutdown, 'INT', 'ABRT', 'QUIT', 'TERM'; sub shutdown{ print logTime." INT|ABRT|QUIT|TERM received....shutting down servic +e.\n"; # # HOW DO I KILL MY ASYNC BLOCK / LOGIN THREADS FROM HERE # # close(LOOK_FOR_LEASE) if fileno(LOOK_FOR_LEASE); close(OUTPUT) if fileno(OUTPUT); close(ERROR) if fileno(ERROR); threads->exit(); } # Opening log file. # Redirecting OUTPUT and ERR to log file for all threads open OUTPUT, '>>', "/usr/local/xxx/log/lease.log" or die $!; open ERROR, '>>', "/usr/local/xxx/log/lease.log" or die $!; STDOUT->fdopen( \*OUTPUT, 'w' ) or die $!; STDERR->fdopen( \*ERROR, 'w' ) or die $!; # Declare shared variables here ... ... $sharedMAChash :shared; ################ MAC watch Thread1################## $thread300 = async { while(sleep 300){ # Checking done here. # Contact server. Get status of all macs from server # Note : Hash value 1 = Authenticated device. 0 = Failed Login devic +e. # Parse server response here. # Is online ? $sharedMAChash{$mac} = 1 # No change # Is online && $sharedMAChash{$mac} = 0? # Del from hash # Is offline? # Del from hash + } }; ################## Main Thread ################## while(usleep 250000){ while(<LOOK_FOR_LEASE>){ # Found a new IP lease . Parse MAC # Launch Login thread $allmac{$mac} = threads->create(\&loginmac); } } ################## Login thread ################## sub loginmac{ # Use HTTP::Request and contact server and Auth this device. # Login Success? $sharedMAChash{$mac} = 1; # Authentication Success # Login Fail $sharedMAChash{$mac} = 0; # Authenticatication Fail.Hold MAC in hash + until Timeout threads drops it from hash. }

    In reply to Perl5.8.4 , threads - Killing the "async" kid by MonkeyMonk

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.