Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Im I forking properly ?

by Anonymous Monk
on Apr 15, 2016 at 13:40 UTC ( [id://1160523]=note: print w/replies, xml ) Need Help??


in reply to Im I forking properly ?

Your script is horrible to read, it's prone to creating zombie processes and it doesn't even compile.

So, no, you're not doing it properly.

I was hoping that some other monk would tell you that more politely, but no one did, so I'm just telling you like it is.

As for what is wrong: $output and $fver are not declared anywhere, so use strict aborts compilation. wait in a SIGCHLD handler is just wrong, because it waits only for one child, but:

... when the signal is delivered to the process by the system (to the C code that implements Perl) a flag is set, and the handler returns immediately.
    Then at strategic "safe" points in the Perl interpreter (e.g. when it is about to execute a new opcode) the flags are checked and the Perl level handler from %SIG is executed.
...
        As the Perl interpreter looks at signal flags only when it is about to execute a new opcode, a signal that arrives during a long-running opcode (e.g. a regular expression operation on a very large string)  will not be seen until the current opcode completes.
        If a signal of any given type fires multiple times during an opcode  (such as from a fine-grained timer), the handler for that signal will be called only once, after the opcode completes; all other instances will be discarded.
In addition, the flag-setting handler in perl blocks SIGCHLD signal while it's executing; and only one blocking signal can be pending, so, if two or more children die during that, you'll get at least one zombie.

Recommended fix:

$SIG{CHLD} = 'IGNORE';
Recommended reading:
perldoc perlstyle perldoc strict perldoc perlipc man 7 signal man 2 wait man 2 sigaction perldoc -f wait perldoc -v '%SIG'
Also, see "The Linux Programming Interface", chapters 20-22 (applicable to all Unix-like systems, not only Linux).

Replies are listed 'Best First'.
Re^2: Im I forking properly ?
by leostereo (Beadle) on Apr 18, 2016 at 14:45 UTC

    I really apreciate your honesty!!! and thanks for the suggested read material!

Re^2: Im I forking properly ?
by leostereo (Beadle) on Apr 18, 2016 at 14:50 UTC

    just one thing ... Why is it so horrible to read??

      Hello leostereo,

      Why is it so horrible to read??

      Because the haphazard indentation makes it almost impossible to tell, at a glance, where each logical block of code ends. Here’s the original version of sub write_register, with a single comment added:

      Now, quickly: which block of code is terminated by the right brace named X?

      Here’s a preliminary clean-up of the code, with consistent indentation:

      Easier to follow now? Yes, and that makes it easier to spot where it can be improved:

      1. As noted by Anonymous Monk++, the variables $output and $fver are never declared. In addition, the variable $bsid is neither declared nor initialized anywhere in the script.
      2. Perl allows you to return, exit, or die at any point in the code. Since these statements are forms of flow control, the code can often be simplified when these statements are used. For example, this:
        if (condition) { return; } else { ... }
        is logically equivalent to the shorter and simpler:
        return if condition; ...
      3. It’s bad practice to exit from a subroutine. You should die (i.e., throw an exception) instead: that way, the subroutine’s clients have the option to either handle the exception locally or allow it to propagate upwards and so terminate the script.

      Here’s an improved version of the subroutine:

      As a rule, the shorter and simpler the code, the easier it is to debug and maintain. Disclaimer: I haven’t looked at the code’s logic, only its form.

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1160523]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2025-06-24 04:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.