如何判断哪个文件首先创建?

发布于 2024-11-23 15:34:41 字数 213 浏览 4 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(2

一个人练习一个人 2024-11-30 15:34:41

实际上,在具有现代文件系统的现代 Unices 上,文件修改时间存储在 timespec 中。详细信息:

标准stat看起来像这样的 WRT 时间:

struct timespec st_atim Last data access timestamp. 
struct timespec st_mtim Last data modification timestamp. 
struct timespec st_ctim Last file status change timestamp.

timespec

time_t  tv_sec    seconds
long    tv_nsec   nanoseconds

因此,在我的 Linux 2.6.39 上进行统计:

Access: 2011-07-14 15:38:20.016666721 +0300
Modify: 2011-06-10 03:06:12.000000000 +0300
Change: 2011-06-17 11:01:35.416667110 +0300

总之,我认为如果硬件提供了足够的精度,那么您就已经获得了足够的精度。

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:

struct timespec st_atim Last data access timestamp. 
struct timespec st_mtim Last data modification timestamp. 
struct timespec st_ctim Last file status change timestamp.

And a timespec

time_t  tv_sec    seconds
long    tv_nsec   nanoseconds

So, doing a stat on my Linux 2.6.39:

Access: 2011-07-14 15:38:20.016666721 +0300
Modify: 2011-06-10 03:06:12.000000000 +0300
Change: 2011-06-17 11:01:35.416667110 +0300

In conclusion, I think you've got enough precision there if the hardware is supplying it.

清秋悲枫 2024-11-30 15:34:41

您可以尝试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.

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