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
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:
#!/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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |