This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

how to realize exit(0) function in keilC51

HI: We can use exit(0) instruction to exit a normally running routine in C language. But exit(0) is not being in the KEILC51.Now how can I realize the same function as exit(0) instruction with other instructions?

Parents Reply Children
  • Of course it is possible to implement an exit() function.

    But an embedded system has no need for any program return code since the program will not return to something making use of it.

    So an exit() function would then probably have to either reboot the system, or cut the power. But the individual developer must make the decision exactly what exit(0) should do before making such an implementation.

    So if the end result is a function that reboots the hw target, it's better to call that function reboot(). And if the end result is a function that cuts the power, it's better to call that function cut_power().

    In the end, even if it is possible to implement a function with the name exit() and that takes one integer parameter, it's normally better to not implement such a function since the meaning of the function will not be the same as the exit() function known from any standard.

  • "But an embedded system has no need for any program return code since the program will not return to something making use of it."

    One possible reason to do it would be to give a reason for the exit, store it to non-vol and then report it on the next power-up.

    Done a similar thing myself where the function could be called from anywhere (including an ISR), and report the assertion fault on the restart.

    I'm not condoning the use of the name exit, just giving an alternate view.

  • I think lots of embedded systems have methods to implement different abort() solutions where a failure may be emitted to flash/EEPROM/LEDs/UART/...

    But then we are normally talking about an abort or shutdown or forced reboot - not a normal "exit". The function exit() is expected to make a controlled exit - close open files etc. A problem when you really do find such a bad problem that you need to abort the program run, is how to know that you are enough in control that you may dare to do the last touch-up of a flash file system before a restart? And more interestingly - what should/could a compiler vendor do?

  • "We can NOT use exit(0) instruction to exit a normally running routine in EMBEDDED C"

    Yes we can. We just have to define ourselves what should happen. It's just that the most likely decisions would lead to a similar function but with a very different name - something like:
    halt()
    shutdown()
    reboot()
    reset()
    restart()
    power_off()
    power_cycle()
    lock_up_until_manual_reset()
    return_to_bootloader()

    If we do have a boot loader, then a running program _can_ exit() to another program that will pick up the execution. But since it isn't as obvious in an embedded device that there are anything that may possibly pick up the pieces after an exit, and since a boot loader may not be implemented so that it is entough to just run past the last closing brace of main(), any jump to a boot loader will normally have to be very explicit, giving a reason for a more explicit name than just the generic exit().

    So the concept of an exit() can be had. But a generic implementation can't exist without a minimalistic OS or application laucher, and without an application launcher the meaning/result of such a call is not obvious. Hence, the name exit() should not exist in any device that does not have a generic application launcher.

  • Of course you can; as I explained previously.

    Use of the function name exit may be misleading, but it can be done.

  • Wherever you would like it to exit to.

    "What's in a name? That which we call a rose ..."

  • It is possible to realize the function exit(0).

    No, it's not, because like with all functions defined by the standard,

    and decide what you want it to do.

    is not an option. The C Standard defines what exit() does. If you're going to implement a function that does something else, you are for practical intents and purposes forbidden to call that function exit().

    And there's quite exactly no way to fulfill the standard's requirement for exit() on an 8051, of all things. C on a '51 is, pretty much necessarily, a free-standing implementation, as opposed to a hosted one. As such, a C51 program is forbidden to call exit() because <stdlib.h> is not among the headers listed in C99, chapter 4, paragraph 6. And because there is no "host environment" to return to.

  • "No, it's not, because like with all functions defined by the standard"

    It is a rule of a standard, it should not be confused with some law of physics.

    A sign saying "no entry" does not do anything to physically stop someone driving up a road the wrong way.

    
    #define BEGIN }
    #define END   {
    
    void main(void)
    END
       ;
    BEGIN
    
    

    Looks crazy, stupid etc, but you can still do it.