到底是什么拥有“当前工作目录”?
我知道工作目录(wd)是什么以及它的用途(至少用于编写软件)。
我不明白的是 wd 的所有权。此外,我想了解操作系统之间的答案可能有何不同,因此对特定操作系统上的异常行为的任何澄清将不胜感激。
那么首先,wd 在哪里体现出来?是否在一个进程内,并且该进程创建的所有线程共享相同的wd?如果 wd 被线程“A”修改,则该更改是否立即对线程“B”(也是由线程“A”进程生成)可见?
其次,wd 最初是如何定义的?如果您从命令提示符启动某项操作,那么很容易看出如何启动,但是如果一个进程又生成了多个进程呢?
注意:Process Explorer 在其属性窗口中显示每个进程的 wd。
I'm aware what a working directory (wd) is and it's purpose (for writing software at least).
What I don't understand is the ownership of the wd. Furthermore, I want to understand how the answer may vary between operating systems so any clarification on unusual behaviour on a particular OS would be appreciated.
So firstly, where does the wd manifest itself? Is it within a process, and all threads created by that process share the same wd? If the wd gets modified by thread 'A', is the change instantly visible to thread 'B' which was also spawned by thread 'A's process?
And secondly, how is the wd initially defined? If you start something from a command prompt its fairly easy to see how, but what about a process which spawns several more processes?
Note: Process Explorer shows each process' wd in it's Properties window.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在大多数现代操作系统上,工作目录是进程的一个属性。当父进程派生子进程时,它将(默认情况下)具有相同的工作目录。通常可以通过显式指定工作目录来覆盖此行为。
一旦分叉,子级的工作目录字段就独立于父级。父进程工作目录的更改不应改变子进程。复制与工作目录相关的任何句柄或安全令牌的行为高度依赖于操作系统。
在 Windows 上,
CreateProcess
添加RTL_USER_PROCESS_PARAMETERS
进程内存的结构体,其中包含UNICODE_STRING CurrentDirectoryPath
和HANDLE当前目录句柄
。在现有 NT 版本的 Windows 上,该结构始终加载到0x20000
处,但这在将来可能会发生变化。On most modern operating systems, the working directory is a property of the process. When a parent process forks a child process, it will (by default) have the same working directory. This behaviour can usually be overriden by explicitly specifying a working directory.
Once forked, the child's working directory field is independant of the parent. A change of the parent's working directory should not alter the child process. The behaviour of duplicating any handles or security tokens related to the working directory is highly dependant on the operating system.
On Windows,
CreateProcess
adds anRTL_USER_PROCESS_PARAMETERS
structure to the memory of the process, which containsUNICODE_STRING CurrentDirectoryPath
andHANDLE CurrentDirectoryHandle
. The structure is always loaded at0x20000
on existing NT versions of Windows, but this may change in future.通常,当前工作目录是每个进程的构造,因此进程中的所有线程共享一个 PWD 和一个
chdir
立即传播到其他线程。 (在 Linux 上,可以使用低级clone
系统调用。)PWD 是从进程的父进程继承的。有多少个兄弟进程并不重要;重要的是。它们都将共享其初始 PWD。
Commonly, the present working directory is a per-process construct, so all threads within a process share a single PWD and a
chdir
instantly propagates to the other threads. (On Linux, it is possible to create threads with their own PWD using the low-levelclone
system call.)The PWD is inherited from the parent of a process. How many sibling processes there are doesn't matter; they'll all share their initial PWD.