Systems Concept

The Current Working Directory of a Process

Each process carries its own current working directory in kernel state, and relative paths are resolved from it, so the same path string can name different files in different processes.

The short answer

A relative path such as data/input.txt has no meaning by itself. The kernel resolves it from the calling process's current working directory (CWD), so two processes that hold different CWDs can open different files from the same string. The CWD is kernel state that belongs to the process: chdir() changes it, fork() copies it, and execve() preserves it.

The environment variable PWD is not the CWD. Shells maintain PWD as a convenience, and it can be unset or stale after a directory is renamed. getcwd() asks the kernel for the real path.

A minimal example

mkdir -p /tmp/cwd-check/left /tmp/cwd-check/right
printf 'left\n'  > /tmp/cwd-check/left/data.txt
printf 'right\n' > /tmp/cwd-check/right/data.txt

(cd /tmp/cwd-check/left  && cat data.txt)   # left
(cd /tmp/cwd-check/right && cat data.txt)   # right

Both commands run the same cat data.txt; only the inherited CWD differs. A program started with execve() keeps the CWD, which is why a child launched from a shell starts where the shell was:

import os, subprocess, sys

os.chdir("/tmp/cwd-check/left")
subprocess.run([sys.executable, "-c", "import os; print(os.getcwd())"])

The child prints /tmp/cwd-check/left.

Why this happens

Linux keeps the CWD in the process's fs_struct together with the root directory. Path lookup begins at that directory object, and each component is resolved in turn, so the starting point is part of the process, not part of the string. On Windows the equivalent is a per-process current directory set by SetCurrentDirectory, and CreateProcess can give a child a different starting directory.

Event Effect on the CWD under POSIX
chdir("/srv/app") The process CWD becomes /srv/app
fork() The child gets a copy; later changes do not affect the parent
execve() Unchanged; the new program inherits it
Process exit The kernel drops the process's reference

On Linux, threads created with clone(CLONE_FS) share the file-system context, including the CWD; this is how pthreads behave. unshare(CLONE_FS) gives a thread its own copy.

When the directory disappears

On Linux, another process can remove a directory that is your CWD if the directory is empty. Your process keeps a reference to the unlinked directory, so relative paths such as data.txt or ../sibling still resolve through it. getcwd() can then fail with ENOENT, and pwd -P reports an error, because no path from the root reaches that directory any more. This is one route to the failures described in ENOENT on relative paths. POSIX allows rmdir() to reject a directory in use with EBUSY, and Windows does not permit deleting or renaming a directory that is any process's current directory, so behavior differs across systems.

Practical checks

Print the physical path with pwd -P, or inspect /proc/<pid>/cwd on Linux. In Python, os.getcwd() raises FileNotFoundError when the directory has been unlinked, while os.path.abspath() only combines strings and does not touch the file system. When a relative path opens an unexpected file, log the CWD and the resolved absolute path first. Code that must tolerate concurrent renames can resolve paths once with Path.resolve() or anchor lookups to an open directory descriptor with openat() and fchdir().

Sources

Revision history

Published
Updated
— Initial publication.