I'm reading "The Unix Programming Environment", but I don't comprehend the concept current directory of process.
Each process, has a current directory, and all filenames are
implicitly assumed to start with the name of that directory, unless
they begin directly with a slash.
Does it mean that every process has a sign in which directory it was created?
For example, if program /bin/sed
was invoked from /home/rene
, then process which was created from invoking sed has the current directory /home/rene
?
Best Answer
Conceptual level
When you start a process from your shell, the current working directory of the process is the same as the current working directory of your shell. In the context of the shell, the current working directory is the location you are currently "at."
The current working directory of any process is used to interpret relative paths. For example, if your shell's current working directory was
/home/rene
and you ranls ..
from the shell, the process's current working directory,/home/rene
, would be used to resolve..
to/home
.You can see the current working directories of all of the processes running on your system with
lsof | grep '\scwd\s'
(note that you'll probably need to be root to see other users' processes.) This can give you an idea of how current working directories relate to the processes running on your system.Program level
The current working directory of the shell is the directory you see and modify with the shell built-ins
pwd
andcd
respectively. These commands call system calls such asgetcwd
andchdir
that work with the current working directory of your shell.Using
bash
as an example, thecd
built-in eventually runs this line:and the
pwd
built-in eventually runs this line:The shell is just a convenient example of the current working directory's use; these same system calls are used by other programs as well. A program can change its current working directory to
/usr
and any relative paths that the program uses will start from the/usr
directory,Kernel level
The current working directory of a process is stored by the kernel. Linux stores it in the
pwd
member of afs_struct
pointed to by thefs
member of atask_struct
. Thepwd
member is apath
struct, which points to information about the mount (vfsmount
) and the directory's location in the filesystem (dentry
). Each process has atask_struct
associated with it.The
chdir
andgetcwd
system calls modify and retrieve information inpwd
.