Go Down

Topic: exit() seems premature (Read 1 time) previous topic - next topic

I don't want my code to run forever, just once. So I am ending the main loop().

void setup(){Serial.begin(9600);}
void loop(){
  Serial.println("abcdef");
  exit(0);
  Serial.println("zyxwvuts");
}

This code shows, in the serial monitor, just "ab"  I expected "abcdef"
Anyone know why?
I start the serial monitor as soon as I can after compiles, but is that small lapse the problem?

WizenedEE

Serial.print doesn't actually print, it just puts the text in a buffer and arranges for it to be printed later.

Call Serial.flush() before exit().

UKHeliBob

If you want your code to run once put it in the setup() function.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PeterH

It is never, ever correct to call exit() within an Arduino sketch. Your sketch does not run as a process within an operating system so it makes no sense to exit the process; you can't return control to the bootlloader.

If you want your sketch to simply stop, call an infinite loop; it will remain in the loop until the Arduino is reset. Alternatively, you could put the Arduino to sleep - although the extra effort is pointless unless you are trying to save power or something. If you only want the sketch to do something once then follow UKHelliBob's suggestion and put the code to do it in setup().
I only provide help via the forum - please do not contact me for private consultancy.

econjack

It is never, ever correct to call exit() within an Arduino sketch. Your sketch does not run as a process within an operating system so it makes no sense to exit the process; you can't return control to the bootlloader.


What does happen to control when there is a call to exit()?

pYro_65


What does happen to control when there is a call to exit()?


Nothing useful.
Code: [Select]
while(true);

econjack

Quote
Nothing useful.

I was just curious, as it does seem to end the loop.

To the same end, I once saw this infinite loop in code:

Code: [Select]
#define ever ;;

// some code...

for(ever) {
   // more code...
}


Lots of people out there with way too much time on their hands...

retrolefty


As already stated if you want something only performed once just place it in the setup function and leave the loop() function empty.

Code: [Select]

void setup()
  {
    Serial.begin(9600);
    Serial.println("abcdef");
    Serial.println("zyxwvuts");
  }
void loop() {}


Thanks for answers. I appreciate both the code and the theory behind it.

WizenedEE



What does happen to control when there is a call to exit()?


Nothing useful.
Code: [Select]
while(true);

it's actually
Code: [Select]

while(1) cli();

The actual source is
Code: [Select]

_U(exit):
cli
XJMP _U(_exit)


With your version, the code posted in the OP would work.

crimony

I wholeheartedly endorse this thread.

johnkauffman: <posts code that calls exit()>
PeterH: "Don't ever use exit(), the correct way to halt is to call an infinite loop"
econjack: "What does exit() do anyway?"
pYro_65, WizenedEE: "calls an infinite loop"

Comedy gold fellas.

retrolefty


I wholeheartedly endorse this thread.

johnkauffman: <posts code that calls exit()>
PeterH: "Don't ever use exit(), the correct way to halt is to call an infinite loop"
econjack: "What does exit() do anyway?"
pYro_65, WizenedEE: "calls an infinite loop"

Comedy gold fellas.


Well they are different in that calling a infinite loop method still allows interrupts to continue thus updating millis() counter, any active PWM outputs continue refreshing, any active servo outputs continue refreshing, and any incoming serial data will still be received and stored into a ring buffer. The second method callin exit() will stop all those processes because it disables all interrupts.

Lefty

PeterH

When you enter an infinite loop, the behaviour is obvious and predictable.

When you call exit(), the conventional semantics of exit make no sense in this context and the actual behaviour is not at all obvious, and not defined anywhere except in the source code of the AVR library. I stand by my comment that there is no situation where it is appropriate to call exit() in an Arduino sketch. The concept of exiting simply doesn't translate to an Arduino environment; there is nothing to exit to.
I only provide help via the forum - please do not contact me for private consultancy.

WizenedEE

exit() on a PC means "everything in this program stops"

exit() on the arduino means "everything in the program stops"

I fail to see any confusion, except when people assume that things are synchronous when they are not (and the same bug in the OP could have appeared on a PC if it relied on another thread doing stuff)

pYro_65


With your version, the code posted in the OP would work.


Yes, you are correct interrupts are disabled, I assume the OP got the general gist of my comment though.

Quote
The concept of exiting simply doesn't translate to an Arduino environment; there is nothing to exit to.


Not going to name names, but I use an operating system that occasionally likes to exit out to nowhere by showing a lovely blue screen when it can't cope any more.

Go Up