954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How does exit() work?

I was just wondering how exit worked? Does it use some lower-level language feature in its implementation? Is it compiler-specific? What is it? (I am talking about the function in stdlib.h btw)

Labdabeta
Posting Pro in Training
489 posts since Feb 2011
Reputation Points: 27
Solved Threads: 18
 

When you run a program, it creates a process for that program. So when you call exit within that process, the os kills that process and cleans up memory. As for its actual implementation, I do not know. It is most likely implementation specific, that is I don't think there would be one set of implementation that all would follow. But as homework you can create your own exit function using winAPI, by manually deleting the associated process. Other than that, I don't believe you should worry about its details too much.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

exit(2) and exit(3) each have a good description.

L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 

Whenever a function is called , it is pushed on to a stack (called as a frame). Whenever a function returns, it's frame is popped out. This is how flow is maintained. Now , suppose you call a function inside main() , and that function calls exit() , which is also a function, exit is responsible for clearing (popping) stack frames for all functions below it in stack (including main()), and thus process is killed.

I do not have much idea about how it is actually implemented though. I have explained it to you in a very abstracted way, there are many more details involved

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

Thank you, that was very informative.

Labdabeta
Posting Pro in Training
489 posts since Feb 2011
Reputation Points: 27
Solved Threads: 18
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: