Hacker News new | ask | show | jobs
by matheusmoreira 3026 days ago
When a process X creates another process Y, what happens is X becomes the parent of Y and Y becomes a child of X. All processes are created this way. They form a tree structure with PID 1 as the root. The very first process is spawned by Linux itself. Processes are isolated by default and thus cannot modify each other's state.

If we have a shell and we use it to start up a separate chdir program, we get two processes: shell (parent) and chdir (child). The kernel isolates them from each other, so they both have completely distinct working directories. The chdir executes the relevant system call, changes its own working directory and exits. The shell is not affected by the system calls performed by its children, so the chdir process effectively does nothing.

By implementing chdir as a built-in function, the shell is able to change the state of its own process.