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.Now I run the executable, while I am under a different dir path2. Theexecutable 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?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?
The current path of a process
process;cwd
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)).