Thread: how to break an infinity loop from external?

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    39

    how to break an infinity loop from external?

    hi...

    let's say i have an infinty loop:

    Code:
    int main()
    {
          while(true)
          {
               //... blah blah
           }
    
    }
    how to break of the loop externally?

    using interrupt?

    thanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Have a variable, like quit, which is usually zero. When you want to quit, set it to one.

    Or else you can just check the return values of your functions.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    If something inside your loop blocks, you will need to use signals.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    39
    Code:
    void t1()
    {
    	while(true)
    	{
    		//point b
    	}
    }
    
    int main()
    {
                bool bk = false;
    	char c;
    
    	while(!bk)
    	{
    		//point a
    		scanf("%c", &c)
    		if(c == 'y' || c == 'Y')
    			bk = true;
    		
    		t1();
    	}
    	
    }

    based on the code basically when it come to point b... it will be forever stuck there...... now my question i s how to break it.. if point b is not implemented any signal handler or etc etc....?

    Thanks

  5. #5
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    You must have a break in the point b loop or it will run forever, it is that simple. How to break it is entirely up to you.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You know what mitakeet means, right?

    Code:
    while(1) {
        //...
        c = getinput();
        if(c == 'x') break;
        //...
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    39
    Quote Originally Posted by dwks
    You know what mitakeet means, right?

    Code:
    while(1) {
        //...
        c = getinput();
        if(c == 'x') break;
        //...
    }
    ya i know....... but how do we break something like this:

    Code:
    while(1) {
        //...
        //...
    }
    that's the problem......

  8. #8
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Why not post some real code?

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by draggy
    how to break of the loop externally?
    I may be reading this wrong, but is this what you are asking: "Let's say we have the following code. How do I stop it; how do I break out of running the program?"
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       for ( ;; )
       {
          char line[80];
          fputs("enter some text: ", stdout);
          fflush(stdout);
          if ( fgets(line, sizeof line, stdin) )
          {
             char *newline = strchr(line, '\n');
             if ( newline )
             {
                *newline = '\0';
             }
             printf("line = \"%s\"\n", line);
          }
       }
       return 0;
    }
    
    /* my output
    enter some text: hello world
    line = "hello world"
    enter some text: okay, now what?
    line = "okay, now what?"
    enter some text: hey -- how do I stop this thing?
    line = "hey -- how do I stop this thing?"
    enter some text: ^C
    */
    Here in Windows I pressed Ctrl+C. Ctrl+Break works as well.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    39
    sorry for the late reply.... a bit busy over here..
    ok ...

    example:
    Code:
    int main()
    {
    	while(true)
    	{
    		//point b
    		//without break or loop terminator
    	}
    }
    ok above is a infinity loop, which can't be terminate unless using Crtl + c....

    As we can see most OS have the ability to manage and terminate any task by send a message or command or etc etc.....

    My question is how to terminate the infinty loop, where we at outside the boundry of that infinty loop or program......

    thanks

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about:
    Code:
    volatile int terminate = 0;
    
    while( !terminate )
    {
        ...
    }
    Now you just have 'terminate' modified by something else, and you're all set. Otherwise:
    My question is how to terminate the infinty loop, where we at outside the boundry of that infinty loop or program......
    This doesn't make any sense. If you're "outside the boundry of that ... loop", then how again do you need to terminate it? If you're not within the bounds of said loop, there's nothing to terminate. Your entire program doesn't run every single instruction all at the same time. It happens sequentially.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Perhaps exit()?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Optionally you could use a thread?

    e.g. The infinity loop is in the thread:

    Code:
    while(1)
    {
      // stuff
    }
    Main wants to stop the loop, so it kills the thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  4. Reducing Code size from ridiculous length
    By DanFraser in forum C# Programming
    Replies: 10
    Last Post: 01-18-2005, 05:50 PM
  5. debug to release modes
    By DavidP in forum Game Programming
    Replies: 5
    Last Post: 03-20-2003, 03:01 PM