nysus has asked for the wisdom of the Perl Monks concerning the following question:

A few months back, when a colleague/host provider granted me telnet access so I could learn Perl, he advised me to be careful not to write code that produced infinite loops:

while (1) { }
He said they could bring down the server. Of course, like any newbie, I spawned a few of these beasts on accident (this was quite awhile ago). I was able to frantically hit CTRL-D or CTRL-Z to stop the program on a couple such occasions but on a couple of others I was forced to close out the telnet window completely. The server didn't seem to be affected but to this day I wonder if those Perl programs aren't still running and I'm still a little worried about creating infinite loops. So what's the scoop? How dangerous are they?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot";
$nysus = $PM . $MCF;

Replies are listed 'Best First'.
Re: Do infinite loops ever die?
by Eradicatore (Monk) on Jun 13, 2001 at 04:30 UTC
    Interesting. I just ran a test on my shell account at my isp. I telneted in twice from windows, and in one of the windows I started the following program:
    #!/usr/local/bin/perl while(1) { sleep 5; }
    Then, In the other telnet window, I did the "ps -ef | grep perl" and saw the process just fine. I then killed the first telnet window that was running the perl program. I did the ps command again, and the while loop was still going!

    I guess I was going to tell you that if you don't put the process in the background like "test_program.pl&" (the & at the end means put the process in the background for most shells on unix I think), the process would die with the telnet session but this test proved me wrong. Anyway, any good sys admin would be keeping tabs on the server and kill your process if it were causing problems. They can tell when the owner put something in the background or stranded a process, and if the process is just sucking resources and not doing anything useful. You have nothing to worry about with infinite loops. Promise! ;)

    And no_slogan definitely has a good point. There is always some freshmen in college that knows a little too much about fork and recursion and wants to "see what it will do to that nice workstation over there". I know, because my dad is the head of a college engineering computer network, and I hear lots of fun stories.

    Justin Eltoft

    "If at all god's gaze upon us falls, its with a mischievous grin, look at him" -- Dave Matthews

      The sleep command in your example puts this script rather firmly into the "Safe" category: sleeping is the precise method you use when you want something like this NOT to consume CPU resources. The sleep command does precisely what it implies: it sleeps in a very CPU friendly way.

      A better test (minus the fork bomb mentioned below) would be:

      $__++ while 1;
      But even that is bland. Any decent operating system will run unhindered with this executing.

      mr.nick ..

        My goal though was just to have a "nice" infinite loop, if there is such a thing. "nicer" at the very least. I didn't want to suck resources, but rather just test to see if the process died. I was on FreeBSD/i386.

        Justin Eltoft

        "If at all god's gaze upon us falls, its with a mischievous grin, look at him" -- Dave Matthews

      The term for this is "fork-bomb".

      --
      $you = new YOU;
      honk() if $you->love(perl)

      What is susposed to happen is that closing your telnet session notifies the server which then sends HUP (hang-up -- gee, can you tell that Unix is old) to the process group for that session which kills all processes that haven't arranged to ignore or catch SIGHUP/$SIG{HUP}. Putting it into the background shouldn't prevent it from getting killed (that is why there is a "nohup" command).

      The reason that this didn't happen right away in your case is probably because your session wasn't sending any output. In the face of silence, TCP doesn't notice a broken connection for a long time. Actually, killing the telnet client should cause the operating system to close the socket. You would think that close would do the equivalent of shutdown(2), but this doesn't seem to always happen.

      Wait 8 minutes and see if the loop is still running.

              - tye (but my friends call me "Tye")
      I've seen this on Solaris 2.6,7,8 and *BSD and when you close the terminal all processes started by that user at that terminal are terminated. What *NIX did you perform your test on? Simple curiosity.
Re: Do infinite loops ever die?
by no_slogan (Deacon) on Jun 13, 2001 at 03:42 UTC
    It'll probably just make things run a little more slowly on the server, unless the loop body is really, really resource-hungry. To see if there are any rogue perls out there, just do a top or ps -ef|grep perl. If your tty is configured properly, the way to stop a runaway program is to hit ^C.

    Update: That being said, there are certain system calls that can suck up resources a lot faster than you might think, so do be careful out there.

Re: Do infinite loops ever die?
by MeowChow (Vicar) on Jun 13, 2001 at 05:11 UTC
      Well, if the loop dies, it wouldn't be infinite would it?

      It would be if it had been started at some infinite time in the past. ;-)

      John.
      --

Re: Do infinite loops ever die?
by tachyon (Chancellor) on Jun 13, 2001 at 06:05 UTC

    These will die when they use up all the ram and crash the server :-(

    push @a,1 while 1; # this is worse as it is geometric rather than linear @a = qw(1); for(;;) {push @a,@a}

    These are examples of dangerous infinite loops because with each iteration they consume resources. In practice you are unlikely to strike this. By accident anyway.

    cheers

    tachyon

Re: Do infinite loops ever die?
by xphase_work (Pilgrim) on Jun 13, 2001 at 16:15 UTC
    I know that if your infinite loop starts using up way too much resources, and it is not owned by root, then most flavors of unix will kill the process. This is a security measure to ensure that a non-root user cannot kill the server by using up all available resources.

    Now, this doesn't mean that you should leave an infinite loop running, but most well configured servers will protect themselves against that problem.

    -xPhase

Re: Do infinite loops ever die?
by mattr (Curate) on Jun 13, 2001 at 16:06 UTC
    I have seen a hundred hung ftp sessions sitting on an unmanaged unix box in a company. It would seem to have been a bug in the ftp server since it didn't time out the sessions.

    But I've also seen the same thing with jobs lasting for a while even after logging out. In practice, don't worry. They'll die eventually. But nobody is going to appreciate you trying to make infinite loops or anything close to it (oxymoron I know) on their system so why not do it on your own home machine? Everything just gets slower and then halts.

    By the way the nohup command is what you would use (if you have permission) to run a script beyond shell exit. For a limited number of iterations..