为索赔时间设定 PID?

为索赔时间设定 PID?

是否可以通过stat进程 ID 来获取其被声明的时间?

答案1

也许您可以查看与 PID 相关的cmdline内容:exe

# stat /proc/2529/{exe,cmdline}
  File: ‘/proc/2529/exe’ -> ‘/bin/bash’
  Size: 0           Blocks: 0          IO Block: 1024   symbolic link
Device: 3h/3d   Inode: 16136       Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-09-26 07:46:05.435479000 +0530
Modify: 2014-09-26 07:41:05.447479000 +0530
Change: 2014-09-26 07:41:05.447479000 +0530
 Birth: -
  File: ‘/proc/2529/cmdline’
  Size: 0           Blocks: 0          IO Block: 1024   regular empty file
Device: 3h/3d   Inode: 16117       Links: 1
Access: (0444/-r--r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-09-26 07:41:05.447479000 +0530
Modify: 2014-09-26 07:41:05.447479000 +0530
Change: 2014-09-26 07:41:05.447479000 +0530
 Birth: -

我已经使用了stat命令,但我不认为stat当应用于这些文件时,函数会做出不同的响应。


一个例子:

另存为test.c

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int main(int argc, char *argv[])
{
    struct stat stats;
    char exe_path[32] = "/proc/";
    char cmdline_path[32] = "/proc/";
    char *exe = "/exe", *cmdline = "/cmdline";

    if (argc < 2)
        return -1;
    if (lstat (strcat (strcat (exe_path, argv[1]), exe), &stats) != -1)
    {
        printf("exe: %s\n%s%s%s", exe_path, ctime (&stats.st_atime), ctime (&stats.st_mtime), ctime (&stats.st_ctime));
    }
    else perror ("stat error: ");
    if (lstat (strcat (strcat (cmdline_path, argv[1]), cmdline), &stats) != -1)
    {
        printf("cmdline: %s\n%s%s%s\n", cmdline_path, ctime (&stats.st_atime), ctime (&stats.st_mtime), ctime (&stats.st_ctime));
    }
    else perror ("stat error: ");

    return 0;
}

测试一下:

$ gcc -o test test.c
$ ./test $$               
exe: /proc/1811/exe
Fri Sep 26 09:02:59 2014
Fri Sep 26 09:02:59 2014
Fri Sep 26 09:02:59 2014
cmdline: /proc/1811/cmdline
Fri Sep 26 09:02:59 2014
Fri Sep 26 09:02:59 2014
Fri Sep 26 09:02:59 2014

$ date
Fri Sep 26 09:34:03 IST 2014

统计cmd数据可能更可靠。

相关内容