in reply to Do infinite loops ever die?

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

Replies are listed 'Best First'.
Re: Re: Do infinite loops ever die?
by mr.nick (Chaplain) on Jun 13, 2001 at 05:54 UTC
    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

Re: Re: Do infinite loops ever die?
by extremely (Priest) on Jun 13, 2001 at 05:27 UTC
    The term for this is "fork-bomb".

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

(tye)Re: Do infinite loops ever die?
by tye (Sage) on Jun 13, 2001 at 18:45 UTC

    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")
Re: Re: Do infinite loops ever die?
by TeKk9 (Scribe) on Jun 13, 2001 at 05:30 UTC
    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.