How do temporary folders work?

4

I've been questioning myself about it today. On both Unix and Windows operating systems, we have a specific location where temporary files of all sorts are played.

Obviously, if the folder is temporary, the data will stay there for only a certain amount of time.

But now several questions arise:

  • How and when are these files removed? Is there any configuration in the operating system for this?

  • Does it have anything to do with rebooting the system? If it is, as is the case with servers, which are usually rarely restarted?

  • Is there any way to flag in the operating system (at least Windows and Linux) that other folders will also be considered temporary?

asked by anonymous 19.07.2017 / 14:05

1 answer

3

This depends on the operating system.

  

How and when are these files removed? Is there any configuration in the operating system regarding this?

In Windows there is nothing, it must be manual. Cleaning up what is temporary should be responsibility of the application . Placing something there only means that it can disappear at any time without control of the application. Obviously if the file is in use nobody can remove it.

On Linux depends on where you place and distribution. In /tmp , although it is recommended to delete when there is boot , nothing guarantees that it is done in your distribution. In%% of the time it takes until some intervention of the application or the "owner" of the machine, just like Windows.

If you do not add a specific application to take care of this and your application does not care, it usually fills up.

This is just a convention for not leaving temporary files scattered everywhere and you know that erasing there gets rid of them. Too bad many programmers ignore this recommendation and scatter files everywhere.

  

Does it have anything to do with restarting the system? If it is, as is the case with servers, which are usually rarely restarted?

It has in Linux, but anyway it's your problem to solve this, so it does not matter if it's a server.

  

Is there any way to flag in the operating system (at least Windows and Linux) that other folders will also be considered temporary?

As far as I know, it does not have a standard way, but on Linux it can always be tweaked if the person wants it very much and is trained. And it does not matter if there is something like that. Windows allows you to change the folder location.

  

How to use this folder in my application?

This was the most important question that was not asked in a Q & A programming and not a user question.

On Linux it is safe to always use var/tmp and /tmp to place your temporary files.

In Windows the correct thing is to check where the folder is since it can be configured by the user (there can only be one), through the API available in the language that you are using or by the environment variable /var/tmp .

If you do not do it and think you should put it somewhere else, at least ensure it is cleaned under any circumstances, even when the application crashes.

    
19.07.2017 / 15:56