从pid获得优先权和正常运行时间? (iOS)

时间:2012-07-11 18:05:46

标签: iphone objective-c ios process

这是针对应用商店的应用。

使用代码from here,我可以获得正在运行的进程及其pid的列表。但是,我在appstore(like this one)中找到了几个应用程序,这些应用程序还检索了每个进程的优先级开始时间。

(注意:我不关心它是正常运行时间,进程激活的时间,还是进程开始的挂钟日期/时间)。

有没有记录的方法可以做到这一点?

1 个答案:

答案 0 :(得分:1)

以下是获取所需的所有与流程相关的信息的代码,例如Name,Priority,StartDate,ParentID,Status。 Here是使用demo获取完整资源的链接。

// List of process information including PID's, Names, PPID's, and Status'
+ (NSMutableArray *)processesInformation {
    // Get the list of processes and all information about them
    @try {
        // Make a new integer array holding all the kernel processes
        int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};

        // Make a new size of 4
        size_t miblen = 4;

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

        // Set up the processes and new process struct
        struct kinfo_proc *process = NULL;
        struct kinfo_proc *newprocess = NULL;

        // do, while loop rnning through all the processes
        do {
            size += size / 10;
            newprocess = realloc(process, size);

            if (!newprocess) {
                if (process) free(process);
                // Error
                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 *processPriority = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_priority];
                        NSDate   *processStartDate = [NSDate dateWithTimeIntervalSince1970:process[i].kp_proc.p_un.__p_starttime.tv_sec];
                        NSString       *processParentID = [[NSString alloc] initWithFormat:@"%d", [self parentPIDForProcess:(int)process[i].kp_proc.p_pid]];
                        NSString       *processStatus = [[NSString alloc] initWithFormat:@"%d", (int)process[i].kp_proc.p_stat];
                        NSString       *processFlags = [[NSString alloc] initWithFormat:@"%d", (int)process[i].kp_proc.p_flag];

                        // Check to make sure all values are valid (if not, make them)
                        if (processID == nil || processID.length <= 0) {
                            // Invalid value
                            processID = @"Unkown";
                        }
                        if (processName == nil || processName.length <= 0) {
                            // Invalid value
                            processName = @"Unkown";
                        }
                        if (processPriority == nil || processPriority.length <= 0) {
                            // Invalid value
                            processPriority = @"Unkown";
                        }
                        if (processStartDate == nil) {
                            // Invalid value
                            processStartDate = [NSDate date];
                        }
                        if (processParentID == nil || processParentID.length <= 0) {
                            // Invalid value
                            processParentID = @"Unkown";
                        }
                        if (processStatus == nil || processStatus.length <= 0) {
                            // Invalid value
                            processStatus = @"Unkown";
                        }
                        if (processFlags == nil || processFlags.length <= 0) {
                            // Invalid value
                            processFlags = @"Unkown";
                        }

                        // Create an array of the objects
                        NSArray *ItemArray = [NSArray arrayWithObjects:processID, processName, processPriority, processStartDate, processParentID, processStatus, processFlags, nil];

                        // Create an array of keys
                        NSArray *KeyArray = [NSArray arrayWithObjects:@"PID", @"Name", @"Priority", @"StartDate", @"ParentID", @"Status", @"Flags", nil];

                        // Create the dictionary
                        NSDictionary *dict = [[NSDictionary alloc] initWithObjects:ItemArray forKeys:KeyArray];

                        // Add the objects to the array
                        [array addObject:dict];
                    }

                    // Make sure the array is usable
                    if (array.count <= 0) {
                        // Error, nothing in array
                        return nil;
                    }

                    // Free the process
                    free(process);

                    // Successful
                    return array;
                }
            }
        }

        // Something failed
        return nil;
    }
    @catch (NSException * ex) {
        // Error
        return nil;
    }
}

// Parent ID for a certain PID
+ (int)parentPIDForProcess:(int)pid {
    // Get the parent ID for a certain process
    @try {
        // Set up the variables
        struct kinfo_proc info;
        size_t length = sizeof(struct kinfo_proc);
        int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };

        if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)
            // Unknown value
            return -1;

        if (length == 0)
            // Unknown value
            return -1;

        // Make an int for the PPID
        int PPID = info.kp_eproc.e_ppid;

        // Check to make sure it's valid
        if (PPID <= 0) {
            // No PPID found
            return -1;
        }

        // Successful
        return PPID;
    }
    @catch (NSException *exception) {
        // Error
        return -1;
    }
}