我正在寻找一个过程可以用来弄清楚自己创建的时间戳的API。 “创建时间戳”是指流程“诞生”的时间点,即诞生时间戳。
我的情况的具体情况:
我已经搜索了一段时间,但找不到能解决我问题的东西。下面列出了两个最有希望的候选人:
在
starttime
中查找/proc/PID/stat
看来/proc/PID/stat
文件提供了一个名为starttime
的字段(条目22)。引用文档:https://man7.org/linux/man-pages/man5/proc.5.html这样,我仍然需要:
starttime
从jiffies转换为微秒; 这并非不可能,但我希望有一条通向我所需信息的简便方法。到目前为止,我还不知道如何获得系统启动时间(至少具有微秒的精度)。我知道start time of a process on linux-但是此计算使用的
btime
仅具有第二个精度,因此不够精确,无法满足要求:|stat
函数根据https://man7.org/linux/man-pages/man2/stat.2.html,
stat
函数可以为我提供各种与文件相关的时间戳,尤其是:struct timespec st_atim; /* Time of last access */
struct timespec st_mtim; /* Time of last modification */
struct timespec st_ctim; /* Time of last status change */
但是,如https://man7.org/linux/man-pages/man7/inode.7.html中所述,似乎所有这些时间戳实际上都不能保证与出生时间戳相等,因为所有这些时间戳都可以通过某些操作(读/写等)进行更新。看来我想要的是
File creation (birth) timestamp (btime)
。 linux手册页的内容如下:File creation (birth) timestamp (btime)
(not returned in the stat structure); statx.stx_btime
The file's creation timestamp. This is set on file creation
and not changed subsequently.
The btime timestamp was not historically present on UNIX
systems and is not currently supported by most Linux
filesystems.
所以...我不确定获得流程创建时间的最佳方法是什么。感谢您阅读本文,您真棒!
最佳答案
POSIX stat
并未定义此字段,但是您可以从 statx
中获取它。
它是在2019年初添加的,因此您需要相当新的内核和glibc版本才能利用它。
关于c++ - 检索流程创建时间的API(Linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63603266/