Thursday, December 1, 2016

will the operating system clean up after process exit

From time to time there are people asking "if I allocate memory and exit without freeing it, will the kernel clean up?". I recently encountered a variant with file descriptors and getting rid of the file after exit without explicit close.

While the answer for most cases on systems like Linux or *BSDs is a firm yes, nobody elaborates on the issue.

In short this has to be done for reliability reasons - without it malicious users and buggy programs would crash the system constantly due to resource exhaustion.

A hard requirement for this to be doable is that the kernel needs a way to iterate over the resources allocated for given process. In this post we will see why the ability is forced by user-visible functionality regardless of reliability concerns.

We will take file descriptors as a simple example.

Files when open are identifiable by so-called "file descriptor" numbers. That is, the kernel returns an integer which then can be used to do things with the file. Said integeres are private to the process.

An example program can:

fd = open("crap", O_RDONLY);
read(fd, buf, sizeof(buf));

Somewhere in memory there is an object which represents the file and there must be a way to go from the fd number to the address of the object for this work in the first place.

There is a reliability concern here as well: what if the process passes an incorrect value? Say it has file descriptors 0, 1, 2 and passes 7563. The kernel must not crash nor return junk.

There is also an important function: fork(2). It will create a new process which has certain things copied over from the caller. This includes file descriptors. If our process has 0, 1, 2 open, the child will also have 0, 1, 2 open and they will "point" to the same files.

But if the new process has to have the same file descriptors, the kernel already needs a way to iterate over descriptors installed for the old one.

And there you go - all open file descriptors are explicitly known, so there is no problem iterating over them on process exit.

Memory handling has similar reasoning.

As a final note, most real-world programs already exit without closing some file descriptors or freeing up all of the memory allocated.

No comments:

Post a Comment