是否有可能得到PID的发射时间? [英] Is it possible to get the launch time of PID?

查看:109
本文介绍了是否有可能得到PID的发射时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code是浮在这里一段时间(的code为我工作):

I have a code that is floating around here for a while (the code is working for me):

- (NSArray *) runningProcesses {
    //CTL_KERN,KERN_PROC,KERN_PROC_ALL
    int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL ,0};
    size_t miblen = 4;

    size_t size;
    int st = sysctl(mib, miblen, NULL, &size, NULL, 0);

    struct kinfo_proc * process = NULL;
    struct kinfo_proc * newprocess = NULL;
    do {
        size += size / 10;
        newprocess = realloc(process, size);
        if (!newprocess) {
            if (process) {
                free(process);
                process = NULL;
            }
            return nil;
        }
        process = newprocess;
        st = sysctl(mib, miblen, process, &size, NULL, 0);
    } while (st == -1 && errno == ENOMEM);
    if (st == 0) {
        if (size % sizeof(struct kinfo_proc) == 0) {
            int nprocess = size / sizeof(struct kinfo_proc);
            if (nprocess) {
                NSMutableArray * array = [[NSMutableArray alloc] init];
                for (int i = nprocess - 1; i >= 0; i--) {
                    NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];
                    NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];
                    NSString * proc_CPU = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_estcpu];
                    double t = [[NSDate date] timeIntervalSince1970] - process[i].kp_proc.p_un.__p_starttime.tv_sec;
                    NSString * proc_useTiem = [[NSString alloc] initWithFormat:@"%f",t];
//                    NSLog(@"process.kp_proc.p_stat = %c",process.kp_proc.p_stat);
                    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
                    [dic setValue:processID forKey:@"ProcessID"];
                    [dic setValue:processName forKey:@"ProcessName"];
                    [dic setValue:proc_CPU forKey:@"ProcessCPU"];
                    [dic setValue:proc_useTiem forKey:@"ProcessUseTime"];
                    [array addObject:dic];
                }
                free(process);
                process = NULL;
                NSLog(@"runningProcesses is === %@",array);
                return array;
            }
        }
    }
    return nil;
}

我不是一个C语言的开发人员,所以我试着读了一下关于PID的等,但我的问题还没有看到一个办法知道当PID首次推出,上一次使用。

I'm not a C language developer so I tried to read a bit the questions regarding PID's etc but I haven't seen a way to know when a PID first time launched and used last time.

任何想法怎么办呢?

推荐答案

看起来你想要的是:

process[i].kp_proc.p_un.__p_starttime.tv_sec

这让你在几秒钟的过程一辈子的日期,你可以从制定开始时间。

That gives you the process lifetime to date in seconds, and you can work out the start time from that.

本code搞乱不知道C的不会是任何乐趣可言,虽然。

Messing with this code without knowing C is not going to be any fun at all, though.

这篇关于是否有可能得到PID的发射时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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