Suppose I write a program, which outputs a file under a relative path (as opposed to a full/absolute path) - let's say, the current path.

Then I compile it and store the executable under some dir path1.

  1. Now I run the executable, while I am under a different dir path2. The executable will output a file under path2 instead of path1.

    I wonder why the executable doesn't write a file under path1 instead? In other words, why is the "current path" path2 not path1?

  2. If during run-time, the executable opens a file stored in path3, why does the current path become path3, although I run the executable from path2 and the executable was stored in path1?

How does the OS assign and change the current path for a process, during it's run-time?

  • I'm not really sure what do you mean by current path. Take a look at chdir(2), perhaps it's what you look for.
    – UVV
    Commented Jul 10, 2014 at 6:11
  • Can you give the code you have try?
    – cuonglm
    Commented Jul 10, 2014 at 6:22
  • The initial directory a program runs in its father's current directory. For example, if you launch a program from a shell, the shell's current directory will be the initial directory of the program. Does that answer (partially) your question or are you looking for something else?
    – lgeorget
    Commented Jul 10, 2014 at 6:50
  • @lgeorget:thanks, that answer the first question. The second question, for example, when you run an emacs process, and open in emacs a file in path3, why is the current dir changes to path3?
    – Tim
    Commented Jul 10, 2014 at 6:59
  • Actually, it might not be changed. A process can create or open a file inother place than its current directory. I'll post a proper answer.
    – lgeorget
    Commented Jul 10, 2014 at 7:03

1 Answer 1


Question 1 : Why is the directory where a program is installed not the initial directory of the process when running the program?

Actually, the installation path of a program is irrelevant. What matters is the current path of the father process. In case of a program launched from a shell, the father process is the shell itself so the initial current directory of the new process is the shell's current directory.

Question 2 : How can a process create a file outside from its current directory?

There are two ways to give the path of a file: absolute path and relative path. An absolute path is interpreted from the root of the filesystem (/) and start with a slash ("/"). A relative path is interpreted from the current directory of the process. So if you have two directories, for example /path2 and /path2/path3, and a process whose current directory is path2, it can open a file path3/file. This path is relative (it doesn't start with a slash) so it's computed from the current directory path2. And finally, the new file's complete path is /path2/path3/file. So a process running in a given directory may create file outside of it.

Question 3 : How does the OS assign and change the current path for a process, during its running?

A process can ask the OS for changing its current directory by the mean of the chdir(2) system call (provided that it has needed permissions on the new directory for it, etc. etc.). That's a different mechanism which has nothing to do with opening files. Opening files is done through another system call (namely open(2)).

  • thanks. for Q2, I am not asking about creating a new file. I ask about opening an existing file, and then the current path of the process changes to the path for the file.
    – Tim
    Commented Jul 10, 2014 at 7:19
  • Creating and opening are actually quite the same. Creating is opening a new file. About changing the current directory, that's another operation done by the process. By no mean, you can change your current directory by opening a file.
    – lgeorget
    Commented Jul 10, 2014 at 7:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .