获取ios系统的进程列表(pid, names, paid, status)

    // 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, (int)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, (int)miblen, process, &size, NULL, 0);
            
        } while (st == -1 && errno == ENOMEM);
        
        if (st == 0) {
            if (size % sizeof(struct kinfo_proc) == 0) {
                int nprocess = (int)(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;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值