如何判断哪个文件首先创建?
在 Linux 系统上(我面前的是 Ubuntu 10.04,但这并不重要),我如何判断同一秒内创建的两个文件中哪一个是先创建的?我控制的过程本身既不创造任何东西,也不创造任何东西。我认为,在所有其他方面,ctime 都可以达到目的,但 1 秒的分辨率是一个问题。
作为背景,我试图可靠地确定可能过时的 pid 文件是否引用具有该 pid 的当前进程。如果有更好的方法可以做到这一点,我洗耳恭听。
On a Linux system (the one in front of me is an Ubuntu 10.04, but that shouldn't matter), how can I tell which of two files created within the same second was created first? The process I control creates neither itself; in all other respects the ctime would, I think, do the trick, but the 1 second resolution is a problem.
For background, I'm trying to reliably determine whether a potentially stale pidfile refers to the current process with that pid. If there's a better way to do that, I'm all ears.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,在具有现代文件系统的现代 Unices 上,文件修改时间存储在 timespec 中。详细信息:
标准说
stat
看起来像这样的 WRT 时间:和
timespec
因此,在我的 Linux 2.6.39 上进行统计:
总之,我认为如果硬件提供了足够的精度,那么您就已经获得了足够的精度。
Actually, on modern Unices with modern filesystems, the file modification time is stored in a timespec. Details:
The standard says
stat
looks like this WRT times:And a
timespec
So, doing a stat on my Linux 2.6.39:
In conclusion, I think you've got enough precision there if the hardware is supplying it.
您可以尝试
ls -rt
按时间对文件进行排序,希望文件头比默认列表时间格式显示的精度更高。但如果文件系统没有这些信息,就没有办法做到这一点。其他选择?您可以向文件添加一个 ID 并始终递增它,但是一旦您尝试从文件系统加载此 ID(当您创建新进程时),您就会遇到锁定问题。
那么如何确保 PID 文件没有过时呢?答案:使用
daemon
脚本。它在后台运行一个进程,并确保进程退出后立即删除 PID 文件。You can try
ls -rt
to sort the files by time on the hope that the file header has more precision than the default list time format displays. But if the file system doesn't have the information, there is no way to do this.Other options? You could add an ID to the file and always increment it but as soon as you try to load this ID from the file system (when you create a new process), you'll run into problems with locking.
So how can you make sure the PID file is not stale? Answer: Use the
daemon
script. It runs a process in the background and makes sure the PID file gets deleted as soon as the process exits.