Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 515
dsimcha Avatar asked Sep 23 '10 18:09

dsimcha


1 Answers

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

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

fs_struct has a field called pwd, which stores information about the current working directory (the filesystem it is on, and the details of the directory itself).

like image 147
Richard Fearn Avatar answered Oct 06 '22 09:10

Richard Fearn