Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

You're probably still breeding zombies, which happen if the child exits before an unprepared parent does. You have placed waitpid in the wrong position. It must be called in the parent, not the child, so line 27 should be moved to within the block on line 24.

if( $pid = fork ) { alarm $timeout; waitpid( $pid, 0 ); } else { exec( $command ) || die( "Couldn't exec $command" ) }
Once you call exec successfully, nothing else you write matters. exec does not return.

I think that you could benefit by first studying fork and exec in detail. Every perl command that fires an external command uses fork in some way, except exec which is metamorphosis, not cloning.

In studying fork you will need to also learn wait and waitpid. What you should aim for is an understanding of unix processes. Advanced Programming in the Unix Environment, by W. Richard Stevens, is the bible on that.

As for kill, that is the simplest form of ipc. You should also learn about signals to use it, and the Stevens book is just as good for that. You're already using signals with your alarms, but knowledge of signals would have shown you that the parent can enforce timeouts on a child by setting its own alarm and firing off SIGINT at it. Avoid rolling your own fancy signal handlers at first. The default handlers and 'IGNORE' are enough while learning.

(Added) You're right that your code and my correction are equivalent, I missed the left brace in the else clause, If I was interested in style, I'd have written

defined( my $cpid = fork) or die $!; $cpid or exec $command; alarm $timeout; waitpid( $pid, 0 ); alarm 0;
I urge you to reflect on why you asked the question. The code was working, but you didn't know why. You reject advice on saving time and worry every time you need this kind of code.

'man 7 signal' will inform you of what the default SIGINT handler does.

After Compline,
Zaxo


In reply to Re: The 'ol shell timeout question by Zaxo
in thread The 'ol shell timeout question by xiper

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-25 02:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found