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)

Recommended Answers

All 4 Replies

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.

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

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

Thank you, that was very informative.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.