Conceptual level
When you start a process from your shell, the current working directory of the process is the same as the current working directory of your shell. In the context of the shell, the current working directory is the location you are currently "at."
The current working directory of any process is used to interpret relative paths.
For example, if your shell's current working directory was /home/rene
and you ran ls ..
from the shell, the process's current working directory, /home/rene
, would be used to resolve ..
to /home
.
You can see the current working directories of all of the processes running on your system with lsof | grep '\scwd\s'
(note that you'll probably need to be root to see other users' processes.) This can give you an idea of how current working directories relate to the processes running on your system.
Program level
The current working directory of the shell is the directory you see and modify with the shell built-ins pwd
and cd
respectively. These commands call system calls such as getcwd
and chdir
that work with the current working directory of your shell.
Using bash
as an example, the cd
built-in eventually runs this line:
if (chdir (nolinks ? newdir : tdir) == 0)
and the pwd
built-in eventually runs this line:
the_current_working_directory = getcwd (0, PATH_MAX);
The shell is just a convenient example of the current working directory's use; these same system calls are used by other programs as well. A program can change its current working directory to /usr
and any relative paths that the program uses will start from the /usr
directory,
Kernel level
The current working directory of a process is stored by the kernel. Linux stores it in the pwd
member of a fs_struct
pointed to by the fs
member of a task_struct
. The pwd
member is a path
struct, which points to information about the mount (vfsmount
) and the directory's location in the filesystem (dentry
). Each process has a task_struct
associated with it.
The chdir
and getcwd
system calls modify and retrieve information in pwd
.