Course contentsShow
← Computer Science
Lesson 1417 of 2,87232. File SystemsFree lesson

Current Working Directory

Understanding how each process maintains a current directory for resolving relative paths efficiently.

Current Working Directory

You'll learn how every process keeps track of a "current" directory to make relative path resolution fast and convenient.

The Process Perspective

When you run a program, the operating system creates a process. That process doesn't just exist in isolation—it has context, including where it currently "is" in the file system tree. This location is called the current working directory (often abbreviated as CWD).

Think of it like reading a book with a bookmark. You can reference pages relative to your bookmark ("three pages ahead") without stating the absolute page number every time. Similarly, when your process has a CWD, you can refer to files using relative paths that are resolved starting from that directory.

Why Each Process Maintains Its Own CWD

Every process maintains its own current working directory independently. When you type ../config.txt in your shell or your program opens a file with a relative path, the system uses the process's CWD to resolve that path efficiently.

For example, if your CWD is /home/alice/projects, and you reference data/file.txt, the system resolves it to /home/alice/projects/data/file.txt automatically. Without a CWD, you'd need to type absolute paths constantly.

The CWD is stored in the process's metadata (similar to how file descriptors are tracked). When a process spawns a child, the child typically inherits the parent's CWD, though it can change its own later using system calls you've learned about in directory operations.

Performance Benefits

Maintaining a CWD in each process means the kernel doesn't repeatedly search from the root for every file access. It's an optimization that makes relative path resolution both convenient and efficient.

Key Takeaway: Each process maintains its own current working directory as part of its state, enabling efficient resolution of relative paths without requiring absolute paths for every file operation.