我是使用xcode开发mac osx应用程序的新手。我试图获取所有运行的应用程序列表与他们的内存使用情况。 在这种情况下,任何人都可以帮助我。
请帮帮我。
提前致谢。
答案 0 :(得分:2)
它将帮助您获取正在运行的应用程序列表:
for (NSRunningApplication *app in [[NSWorkspace sharedWorkspace] runningApplications]) {
NSLog(@"%@",[app localizedName]);
}
答案 1 :(得分:2)
您可以将cpu用法视为:
- (NSString*) get_process_usage:(int) pid
{
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/ps"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-O",@"%cpu",@"-p",[NSString stringWithFormat:@"%d",pid], nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSString* temp = [string stringByReplacingOccurrencesOfString:@"PID %CPU TT STAT TIME COMMAND" withString:@""];
NSMutableArray* arr =(NSMutableArray*) [temp componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[arr removeObject:@""];
[string release];
[task release];
if([arr count]>=2)
return [arr objectAtIndex:1];
else
return @"unknown";
}