Windows 中的 PID 分配策略 / 使用 PID 命名临时文件

发布于 2024-07-24 16:20:52 字数 638 浏览 4 评论 0原文

  1. Windows 中的 PID 分配策略是什么? 重复运行调用 _getpid() 的进程表示非顺序分配(3548,3344,3628,2748,4872,2360)。

  2. 鉴于观察到的 PID 的非顺序分配,具有相同 pid 的两个不同进程是否有可能在彼此接近的时间上执行? 显然,这些进程不会同时运行。

  3. 在临时文件命名中使用PID是否特别不明智?

我正在编写一个程序,该程序通过生成所需的输入文件、调用可执行文件并从所述可执行文件读取生成的输出文件来与另一个可执行文件交互。 CLEAN 终止后,中间文件将被删除。

我担心的是,如果临时文件没有被清理,并且 PID 被重复使用,那么未清理的旧临时文件和新文件之间可能会存在歧义。 如果可执行文件由于错误而未生成新的输出文件,则旧文件可能会像新创建的文件一样出现,因此可能无法捕获错误。

还有其他方法可以增加稳健性,例如对临时文件使用 GUID、仅对临时文件使用干净的目录,或者验证不存在与目标输出文件同名的文件。 应采用其中一些技术,因为 PID 肯定会在计算机重新启动或计算机运行足够长的时间后重复出现。

这个问题主要源于我对为什么 Windows 以与 *nix 不同的方式分配 PID 的好奇心。

  1. What is the PID assignment policy in Windows? Repeated runs of a process calling _getpid() indicates non sequential assignment (3548,3344,3628,2748,4872,2360).

  2. Given the observed non sequential assignment of PIDs, is it possible for two different process with the same pid to be executed closely in time from one another? Obviously the processes would not be running concurrently.

  3. Is it particularly unwise to use a PID in the naming of temporary files?

I'm writing a program that interacts with another executable by generating the required input files, calling the executable, and reading the generated output files from said executable. Upon CLEAN termination the intermediary files are deleted.

My concern is that if the temporary files are not cleaned up and if PIDs are reused there could be an ambiguity between old temporary files that have not been cleaned up and new files. If the executable does not generate a new output file due to an error the old file could appear like the newly created file, thus the error may not be caught.

There are other ways to add robustness such as using a GUID for the temporary files, only using clean directories for temp files, or verifying that there are no files that share the same name as the target output file. Some of these techniques should be employed since PIDs are certain to be repeated upon machine reboot or if the machine is left running for a sufficient amount of time.

The question stems mostly from my curiosity regarding why Windows allocate PIDs in a different manner then *nix.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

獨角戲 2024-07-31 16:20:52

Windows 上的进程 ID 和线程 ID 来自同一池。 系统将立即重用线程和进程ID。

Process IDs and Thread IDs come from the same pool on Windows. The system will reuse thread and process IDs immediately.

飘逸的'云 2024-07-31 16:20:52

以这种方式使用 PID 是有问题的。 我见过一些程序在命名文件时将 PID 与当前 UTC 时间结合使用,因此最终会得到名为 foo_55145_4a3667d3.log 的文件。 另一个选项使用 dwFlagsAndAttributes 参数中的 FILE_ATTRIBUTE_TEMPORARYFILE_FLAG_DELETE_ON_CLOSE 选项来 CreateFile()

Using a PID in this manner is problematic. I've seen programs that use the PID in conjunction with the current UTC time when naming files so you end up with files named foo_55145_4a3667d3.log. The other option use the FILE_ATTRIBUTE_TEMPORARY and FILE_FLAG_DELETE_ON_CLOSE options in the dwFlagsAndAttributes parameter to CreateFile().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文