我正在尝试以编程方式调用“top”命令。以下是使用的代码:
char buffer [128];
char* threadsPointer;
char* procPointer;
NSString* numberOfThreadsString;
NSString* numberOfProcString;
FILE* output = popen("/usr/bin/top", "r");
while (fgets(buffer, sizeof(buffer), output) != NULL)
{
if ((procPointer = strstr(buffer, "Processes:")) != NULL)
{
procPointer += strlen("Proceses: ");
strcpy(buffer, procPointer);
numberOfProcString = [NSString stringWithUTF8String: buffer];
}
if ((threadsPointer = strstr(buffer, "sleeping,")) != NULL)
{
threadsPointer += strlen("sleeping, ");
strcpy(buffer, threadsPointer);
numberOfThreadsString = [NSString stringWithUTF8String: buffer];
}
}
NSLog(@"Proc: %@\nThreads: %@\n\n\n", numberOfProcString, numberOfThreadsString);
我没有提供有效的输出,而是不断收到错误消息:“错误打开终端:未知”。我推荐了整段代码来识别问题,并意识到它的行:FILE* output = popen ("/usr/bin/top", "r");
那是导致错误的原因。
有谁知道我做错了什么?注意我在 Mountain Lion OS X 上为 OSX 而不是 iOS 构建应用程序。