-2

The only computer I can access is one among all in my class, so I was not able to try this program

while (1)
    printf ("I am Printing\n");

by myself for such long time. But I am very curious to know, when an infinite loop terminates if we let it run forever.

Is will the OS dump the program or will it run till the power supply goes down(by clearing the old output), or something like that?

Snigdh
  • 61
  • 11
  • I don't see why the OS would stop this program in particular. The OS may complain about it being unresponsive, and if your shell tries to store all the text, it will run of out memory (but they usually don't). – Blaze Nov 15 '19 at 12:30
  • It will never.. – S.S. Anne Nov 15 '19 at 12:34
  • @Blaze Yes, I exactly want to know what will happen if my shell tries to store all the text and will run out if memory. – Snigdh Nov 15 '19 at 12:35
  • That depends on the shell, then. Most of them store a certain maximum of lines. After that they start discarding the old lines. In that case the program will not be stopped by the shell running out of memory. – Blaze Nov 15 '19 at 12:38
  • You could always swap `while(1)` with `while(fork())`. – Lundin Nov 15 '19 at 12:39

2 Answers2

3

It will run until something stops it. If you don't stop it, it will stop when there's a power outage, or when a truck crashes into your building and breaks the computer, or when your dog trips over the power cable and pulls it out of the wall.

Note: You can create loops that will eventually stop, e.g.:

while (1)
{ 
    char *p = malloc(128); // allocate some memory
    *p = 1;
}

will eventually run out of memory and crash (malloc will return NULL, and *NULL will generally crash [though there are some kinds of computers where it won't]). You could also write:

int counter = 0;
while (1)
{
    counter++;
    if (counter == 10000)
        break;
}

which isn't really infinite because it will only loop 10000 times, even though it has while(1).

Your program is neither of these, though. It's a real infinite loop which won't crash. (Note that printing to the screen will not run out of memory, because old text gets discarded)

Your program could stop if the user sets it up that way. If they're on Linux, they could run ./your_program | head -n10, which will print the first 10 lines, then kill the program when it tries to print more lines. Or they could run timeout 10 ./your_program which will kill the program after 10 seconds.

But if there's nothing that makes your program stop (not in the program, and not done by the user), it won't stop.

user253751
  • 57,427
  • 7
  • 48
  • 90
2

It will run until the power supply goes down (by clearing the old output), or something like that.

If your terminal stores all of the text on screen in memory, it will run until the shell runs out of memory and is killed by the OOM killer.

If your terminal stores the text on the disk, it will probably run until the terminal runs out of disk space (and then probably will still keep going).

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76