Idan Moshe
Idan Moshe

Reputation: 1533

Is it possible to get the launch time of PID?

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;
}

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.

Any ideas how to do it?

Upvotes: 4

Views: 1671

Answers (2)

Philipp Hofmann
Philipp Hofmann

Reputation: 3478

This returns the start time of the current process:

#import "sys/sysctl.h"

- (NSDate *)getProcessStartTime {
    size_t len = 4;
    int mib[len];
    struct kinfo_proc kp;
    
    sysctlnametomib("kern.proc.pid", mib, &len);
    mib[3] = getpid();
    len = sizeof(kp);
    sysctl(mib, 4, &kp, &len, NULL, 0);
    
    struct timeval processStartTime = kp.kp_proc.p_un.__p_starttime;
    return [NSDate dateWithTimeIntervalSince1970:processStartTime.tv_sec + processStartTime.tv_usec / 1e6];
}

Upvotes: 1

Tom Harrington
Tom Harrington

Reputation: 70976

It looks like what you want is:

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.

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

Upvotes: 8

Related Questions