Search code examples
multithreadingoperating-systemfilesystemsdthread-local

How/where is the working directory of a program stored?


When a program accesses files, uses system(), etc., how and where is the current working directory of that program physically known/stored? Since logically the working directory of a program is similar to a global variable, it should ideally be thread-local, especially in languages like D where "global" variables are thread-local by default. Would it be possible to make the current working directory of a program thread-local?

Note: If you are not familiar with D specifically, even a language-agnostic answer would be useful.


Solution

  • Current directory is maintained by the OS, not by language or framework. See description of GetCurrentDirectory WinAPI function for details.

    From description:

    Multithreaded applications and shared library code should not use the GetCurrentDirectory function and should avoid using relative path names. The current directory state written by the SetCurrentDirectory function is stored as a global variable in each process, therefore multithreaded applications cannot reliably use this value without possible data corruption from other threads that may also be reading or setting this value.