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?


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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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