Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Recently I found this in a powerpoint presentation:

When a program is compiled and linked, the linker inserts some extra code in the program. It is this code that calls the main function about the same way a user calls "lower functions".

The extra code has two functionalities:

  • make sure that the C program runs in the right environment
  • cleanup after succesfull termination

Prior to that I thought that cleanup was kernel's job. What is the right story?

share|improve this question

Both the kernel and the C runtime do some of the work.

Some of the things that the C runtime does which the kernel doesn't do: it runs handlers previously registered with atexit() and it arranges for the integer return value from main() to be returned to the system as if with exit().

Of course in the end the kernel will reap all resources (files, memory) regardless of what userland does or doesn't do.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.