Question

Is there a way to write-open files on Linux in C/C++ application in such a way that if the application is killed the open files will be discarded?

The application can be killed by SIGKILL so I cannot use signal handler.

Was it helpful?

Solution

The typical trick for temporary files is to open/create a file, then unlink it without closing the file descriptor.

You end up with a file descriptor to a file which no longer exists. You can still read from it and write to it, but as soon as you close the file descriptor (or are killed) the file will be removed and the space it occupied is freed.

Of course, that doesn't really work if you decide you want to keep the file afterwards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top