Pages: [1] 2   Go Down
Author Topic: exit() seems premature  (Read 1069 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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().
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4056
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want your code to run once put it in the setup() function.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12541
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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().
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Cincinnati, OH
Offline Offline
God Member
*****
Karma: 42
Posts: 687
I'm not bossy...I just know what you should be doing.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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()?
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 64
Posts: 2102
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Nothing useful.
Code:
while(true);
Logged


Cincinnati, OH
Offline Offline
God Member
*****
Karma: 42
Posts: 687
I'm not bossy...I just know what you should be doing.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#define ever ;;

// some code...

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

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

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Nothing useful.
Code:
while(true);
it's actually
Code:
while(1) cli();
The actual source is
Code:
_U(exit):
cli
XJMP _U(_exit)

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

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 99
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12541
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 64
Posts: 2102
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Pages: [1] 2   Go Up
Jump to: