何时启动流程,该如何查询? [英] How can a process inquire, when it was started?

查看:88
本文介绍了何时启动流程,该如何查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当当前进程启动时,是否存在可以用来询问OS的呼叫?

Is there a call, that can be used to ask the OS, when the current process started?

当然,可以在启动时简单地调用gettimeofday()并在整个过程中引用一次记录的值,但是还有另一种选择吗?

Of course, one could simply call gettimeofday() at start-up and refer to that once-recorded value through the life of the process, but is there another option?

很明显,操作系统会保留每个进程的记录(例如,可以在ps的输出中看到它).可以由进程本身(使用C)查询吗?

Obviously, the OS keeps the record for each process (one can see it in the output of ps, for example). Can it be queried by the process itself (using C)?

当然,理想的解决方案是跨平台的,但是特定于(免费)BSD的东西也可以.谢谢!

An ideal solution would, of course, be cross-platform, but something (Free)BSD-specific is fine too. Thanks!

更新:我想出了一个BSD特定的实现,它使用

Update: I've come up with a BSD-specific implementation, that uses sysctl(3) to obtain the kern_proc structure of the current process and finds the ki_start field in there. If nobody suggests anything better in a few days, I'll post my own function here for posterity...

推荐答案

不确定该标准是否适用于FreeBSD以外的BSD(仅在FreeBSD 10.0,amd64上进行了测试).它花了很多时间来挑选源代码ps,而不仅仅是使用系统库,它所做的事情与Mikhail T.的解决方案相同.尽管尽管如此,但代码还是很粗糙(有些人陷入了如何正确地"执行该操作的过程,因为它真的不应该那么难,对吗?"),并且在2个多小时后,这就是我所拥有的.效果很棒.

Not sure standard this is for BSDs other than FreeBSD (only tested on FreeBSD 10.0, amd64). Took a lot of picking apart the source code ps, and other than just using a system library, it doing the same thing that Mikhail T.'s solution is doing. Though despite, the code being pretty rough (sort got caught up in figuring out how to do this 'properly' because 'it really shouldn't be that hard, right?', and 2+ hours later, this is what I have. Works wonderfully.

请记住使用-lkvm标志进行编译以引入libkvm(内核数据库). kinfo_proc在/usr/include/sys/user.h中有详细说明,并且包含您想了解的有关进程的信息.下面的示例代码使用当前过程,但它只是基于PID提取信息.我已经在我的零碎代码中提取了流程的环境变量和其他内容所需的内容,只是告诉我是否需要.

Remember to compile using the -lkvm flag to pull in libkvm (Kernel Data Library). The kinfo_proc is detailed in /usr/include/sys/user.h and has ever bit of information you ever wanted to know about a process. The example code below uses the current process, but it is just pulling the information based on PID. I've got in my scrap pieces of code what you need to pull out the process's environment variables and other stuff, just tell me if you want.

#include <fcntl.h>
#include <kvm.h>
#include <sys/sysctl.h>
#include <sys/user.h>


#include <unistd.h>
#include <stdio.h>
#include <time.h>

int main(int argc,char** args) {
    kvm_t* kd;

    // Get a handle to libkvm interface
    kd = kvm_open(NULL, "/dev/null", NULL, O_RDONLY, "error: ");

    pid_t pid;
    pid = getpid();

    struct kinfo_proc * kp;
    int p_count;

    // Get the kinfo_proc for this process by its pid
    kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &p_count);

    printf("got %i kinfo_proc for pid %i\n", p_count, pid);

    time_t proc_start_time;
    proc_start_time = kp->ki_start.tv_sec;
    kvm_close(kd);

    printf("Process started at %s\n", ctime(&proc_start_time));
    return 0;
}

修改

可以在这里找到完整的源代码,例如FreeBSD,NetBSD和Mac OS X(如果有机会,则为OpenBSD):

Full source code for example including FreeBSD, NetBSD and Mac OS X (OpenBSD when I get a chance) can be found here: https://github.com/dnabre/misc/tree/master/proc_info

这篇关于何时启动流程,该如何查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆