Arduino Forum

Using Arduino => Programming Questions => Topic started by: n46130 on Feb 07, 2014, 03:58 am

Title: use of exit(0)
Post by: n46130 on Feb 07, 2014, 03:58 am
I am reading Jack Purdum's book "Beginning C for Arduino" and he used the statement  exit(0). I cannot find this term in the Arduino control statement list.    Is exit (0)  a valid term to use to exit a loop?
Title: Re: use of exit(0)
Post by: pYro_65 on Feb 07, 2014, 04:03 am
No, exit is a system function that exits your app ( on arduino interrupts are disabled and an infinite loop locks the system ).

Use the keyword break to stop a loop. To jump back to the top of a loop prematurely use the keyword continue.
Title: Re: use of exit(0)
Post by: econjack on Feb 07, 2014, 04:08 am
That guy's an idiot...oh, wait...that's me! The call to exit(0) is a standard way to end a program and it works on the Arduino, but it's not a good idea since there's really no op system to take the call, so to speak. The call was never intended to break out of some form of loop structure. On Unix and other op systems, it's a graceful way to terminate a program as it often cleans up resources and returns them to the op system. There's no real underlying op system for an Arduino.
Title: Re: use of exit(0)
Post by: michinyon on Feb 07, 2014, 05:38 am
What they said.

exit(0) will end the program,  not a loop.

On an arduino,  with no operating system and only one program,   then "ending the program" doesn't really make sense.       

On the arduino,  calling exit(0) really isn't a useful thing to do.   It should teach you to beware that,  just because someone wrote a book about it,   doesn't mean they are an expert.

In fact,  if you want to "end the program" on an arduino,  without turning it off or resetting it,  you want to do the opposite,  put it into an infinite loop that doesn't do anything.
Title: Re: use of exit(0)
Post by: Tom Carpenter on Feb 07, 2014, 09:55 am
On Arduino, the statement:
Code: [Select]
exit(0);
Compiles to be:
Code: [Select]
cli(); //disable interrupts
while(1); //forever loop


It basically stops your program running, but leaves the CPU running in an infinite loop. Personally I think it would be better if the exit() function put the chip into deep sleep mode.
Title: Re: use of exit(0)
Post by: Grumpy_Mike on Feb 07, 2014, 10:09 am
Quote
Personally I think it would be better if the exit() function put the chip into deep sleep mode.

That is a very good idea.
Title: Re: use of exit(0)
Post by: Coding Badly on Feb 07, 2014, 10:23 am

But not a new idea...
http://forum.arduino.cc/index.php?topic=58956.msg424464#msg424464
;)
Title: Re: use of exit(0)
Post by: Tom Carpenter on Feb 07, 2014, 10:18 pm
Great minds think alike :).
Title: Re: use of exit(0)
Post by: Coding Badly on Feb 07, 2014, 11:09 pm

8) :)
Title: Re: use of exit(0)
Post by: SirNickity on Feb 08, 2014, 02:56 am
I love this forum.  <3
Title: Re: use of exit(0)
Post by: Coding Badly on Feb 08, 2014, 03:36 am
Quote
I love this forum.  <3


No doubt!

I thought of two caveats with that code...

1. To truly be "die" the watchdog needs to be disabled.  This should do it...

Code: [Select]
#include <avr/sleep.h>
#include <avr/wdt.h>

void die( void )
{
  wdt_disable();
  set_sleep_mode( SLEEP_MODE_PWR_DOWN );

  while ( true )
  {
    sleep_enable();
    cli();
    sleep_cpu();
  }
}


2. The output pins will retain their state.  Any pins configured OUTPUT / LOW will continue sinking and any pins configured OUTPUT / HIGH will continue sourcing.

I believe everything else will be completely shutdown.
Title: Re: use of exit(0)
Post by: Tom Carpenter on Feb 08, 2014, 01:09 pm
You could add the following for an Arduino Uno - this would reset all the pins to inputs with pullups disabled.
Code: [Select]
DDRC=0;
DDRB=0;
DDRD=0;
MCUCR |= _BV(PUD);
Title: Re: use of exit(0)
Post by: Coding Badly on Feb 09, 2014, 09:52 am

So, is there any interest in a "die" function or is this just an academic exercise?
Title: Re: use of exit(0)
Post by: retronet_RIMBA1ZO on Feb 09, 2014, 12:00 pm

...
...
In fact,  if you want to "end the program" on an arduino,  without turning it off or resetting it,  you want to do the opposite,  put it into an infinite loop that doesn't do anything.

would an alternative be something like;
Code: [Select]

void setup()
{
  while(1)
  {
    // main "loop" code
    goto exit; // instead of exit(0)
  }

exit:
// shutdown/cleanup process

}

void loop() {}
Title: Re: use of exit(0)
Post by: Tom Carpenter on Feb 09, 2014, 04:38 pm
not really.
For one millis() and Serial interrupts will still be firing.
Secondly SerialEvent() is still being called by loop.
Thirdly why on earth would you do it that way?
Title: Re: use of exit(0)
Post by: retronet_RIMBA1ZO on Feb 09, 2014, 04:47 pm

not really.
For one millis() and Serial interrupts will still be firing.
Secondly SerialEvent() is still being called by loop.

which it would still do in the example suggested by this person ?
Quote
put it into an infinite loop that doesn't do anything




Thirdly why on earth would you do it that way?

to show my n00bness...
Title: Re: use of exit(0)
Post by: Tom Carpenter on Feb 09, 2014, 05:44 pm
In our examples you will notice the cli() instruction which disables all interrupts. Also, we don't let loop() keep running (so nor will SerialEvent()).
Title: Re: use of exit(0)
Post by: PaulS on Feb 09, 2014, 07:28 pm
Quote
Secondly SerialEvent() is still being called by loop.

Only if the function is defined, and only if there is serial data to process, and only if the end of loop() is actually reached. Calls to exit() make the last part the most unlikely event to actually happen..
Title: Re: use of exit(0)
Post by: Tom Carpenter on Feb 10, 2014, 12:28 pm

Only if the function is defined, and only if there is serial data to process, and only if the end of loop() is actually reached. Calls to exit() make the last part the most unlikely event to actually happen..

You don't know that its not defined. The whole point in a function that is supposed to stop everything is that it stops everything and doesn't leave open the possibility that the user has left executable code that can still be run.
You'll also notice that in that example exit() is not called, but rather he has used a goto; statement to break out of a loop.