diff between exit(0) and return(0)

  • Thread starter Ramprasad A Padmanabhan
  • Start date
R

Ramprasad A Padmanabhan

If I have defined my main function as int

Then does it make a difference I end main by return(0) or exit(0)
Because I noticed that When My cgi program ended with exit(0) the
program ended immediately else It would take
long to exit long after return(0) was called

Can anyone explain why this happens
( I am using apache on linux redhat 7.2)

Thanks
Ram
ram at netcore.co.in
 
E

Eric Sosman

Ramprasad said:
If I have defined my main function as int

Then does it make a difference I end main by return(0) or exit(0)
Because I noticed that When My cgi program ended with exit(0) the
program ended immediately else It would take
long to exit long after return(0) was called

Can anyone explain why this happens
( I am using apache on linux redhat 7.2)

Returning a value from main() and calling exit() with
that value are almost perfectly equivalent. The subtle
difference is that the local `auto' variables in main()
cease to exist when main() returns, so if any program
cleanup code -- atexit() routines, the automatic flushing
and closing of open I/O streams, and so on -- tries to
use these variables, there will probably be trouble that
wouldn't occur if exit() had been called instead.

This usually isn't a problem, but here's one error to
avoid:

int main (...) {
char buffer[65536];
setvbuf (stdout, buffer, _IOFBF, sizeof buffer);
...
}

This asks the `stdout' stream to use `buffer' as a
temporary work area. If main() returns without first
closing `stdout', `buffer' ceases to exist -- and the
eventual attempt to flush and close `stdout' may find
itself trying to use this vanished memory, which could
cause pretty much anything to happen. Whether this has
anything to do with the long delay you observe is anyone's
guess, but it's something to check.
 
D

Derk Gwen

# If I have defined my main function as int
#
# Then does it make a difference I end main by return(0) or exit(0)

None.

# Because I noticed that When My cgi program ended with exit(0) the
# program ended immediately else It would take
# long to exit long after return(0) was called

Have you done enough to verify that? Due to virtual memory and process contention
and other issues, a short program's run time can vary greatly for no apparent
reason.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top