Hacker News new | ask | show | jobs
by _zachs 3030 days ago
Not one character in the article is actually devoted to how `cd` works...it's just the story of how the author found out where to find the code for it.

An article much more worthy of the title "How does `cd` work?" would maybe, you know, actually go through the code that makes `cd` work.

2 comments

I don't know if the "Update" by Julia Evans was in the article when you posted this. But I'd point to that as a piece of useful insight about how it works at the syscall level.
>SO!! If you had a /usr/bin/cd program that ran chdir, that would be fine, but when you started it it would change its own working directory and exit which is not very helpful. It wouldn’t change the working directory of you (the parent process)

I am a little confused. What is the parent process and why does that matter here?

The parent is the shell that called the theoretical /user/bin/cd. It means that a cd program would get launched by the shell, change its own working directory and then terminate, which is completely useless.

What you want cd to do is change the caller's (or parent's) working directory, or in this case the shell's working directory.

There are hacks to do it that you can find elsewhere in this thread (process injection and debug APIs to name two) but frankly the shell changing its own working directory via a builtin is a much simpler and more reliable solution that is also cross-platform.

Ah. This makes sense. Thanks. I will look into the hacks.
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.

Huh? The author’s explanation may not be thorough but it does talk about the use of a system call and ‘chdir’