Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 9 down vote accepted

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 ran ls .. 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 and cd respectively. These commands call system calls such as getcwd and chdir that work with the current working directory of your shell.

Using bash as an example, the cd built-in eventually runs this line:

if (chdir (nolinks ? newdir : tdir) == 0)

and the pwd built-in eventually runs this line:

the_current_working_directory = getcwd (0, PATH_MAX);

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 a fs_struct pointed to by the fs member of a task_struct. The pwd member is a path struct, which points to information about the mount (vfsmount) and the directory's location in the filesystem (dentry). Each process has a task_struct associated with it.

The chdir and getcwd system calls modify and retrieve information in pwd.

share|improve this answer

Every process has a current working directory (CWD) that it's assigned to when it starts up. You can do the following to find out the working directory for a process.

Run ps aux to find out PID of a process:

$ pgrep cupsd
24532

To find out current working directory of a PID: 24532

$ sudo pwdx 24532
24532: /

So process (PID: 24532) has a CWD of /.

Here's another example:

$ sleep 1000 &
[1] 15988

$ pwdx 15988
15988: /home/sam

This time the sleep process (PID: 15988) has it's CWD set to my home directory, /home/sam.

You can also run the ls -l command:

$ sudo ls -l /proc/24532/cwd
lrwxrwxrwx 1 root root 0 Apr 30 19:18 /proc/24532/cwd -> /

-or-

$ ls -l /proc/15988/cwd
lrwxrwxrwx 1 sam sam 0 Apr 30 19:23 /proc/15988/cwd -> /home/sam

Here you can again see the CWD for those processes, / and /home/sam.

References

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.