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

When a program accesses files, uses system() , etc., how and where is the current working directory of this program physically known / stored? Since the programโ€™s working directory is logically similar to a global variable, it should ideally be thread-local, especially in languages โ€‹โ€‹such as D, where global variables are by default streaming. Is it possible to make the current working directory a software thread-local?

Note. If you are not particularly familiar with D, even an agnostic answer may be helpful.

+4
source share
2 answers

The current directory is supported by the OS, not the language or framework. For more information, see GetCurrentDirectory WinAPI .

From the description:

Multithreaded applications and common library code should not use the GetCurrentDirectory function, and relative path names should be avoided. The current state of the directory written by the SetCurrentDirectory function is stored as a global variable in each process, so multi-threaded applications cannot reliably use this value without possible damage to data from other streams that can also be read or set this value.

+7
source

On Linux, each process is represented by a process handle - a task_struct . This structure is defined in include / linux / sched.h in the kernel source.

One of the fields task_struct is a pointer to fs_struct , which stores information related to the file system. fs_struct defined in include / linux / fs_struct.h .

fs_struct has a field called pwd , which stores information about the current working directory (the file system in which it is included, and information about the directory itself).

+7
source

All Articles