Making Files Temporary

Once a temporary file is created, a program must release it when finished with it. Otherwise, the temporary file directory will fill with many abandoned files over time. Calling unlink(2) is trivial, but making sure it is done when the program prematurely exits is more of a challenge.

Using unlink(2) to Make Files Temporary

One way to make sure that the temporary file is released is to release it immediately after it is created and opened. This looks illogical to those who are new to UNIX, but a UNIX file can exist after it has been unlinked, as long as the file remains open. When the last open file descriptor for the file is closed, the disk space is reclaimed by the UNIX kernel.

Recall function tmpfile(3), which creates ...

Get Advanced UNIX Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.