如果有人能够请帮助我,那就太好了:)
这似乎很难。从流程ID开始,我需要能够抓住:
这需要用Cocoa / Objective-C或C编写。它还需要通过Snow Leopard处理Tiger。
谢谢!
答案 0 :(得分:1)
粗略的方法是生成popen
命令并从ps
获取一些输出。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void get_process_info(int pid) {
char ps_cmd[256];
sprintf(ps_cmd, "ps -O %%cpu -p %d", pid); // see man page for ps
FILE *fp = popen(ps_cmd, "r");
if (fp) {
char line[4096];
while (line == fgets(line, 4096, fp)) {
if (atoi(line) == pid) {
char dummy[256];
char cpu[256];
char time[256];
// PID %CPU TT STAT TIME COMMAND
// 32324 0,0 s001 S+ 0:00.00 bc
sscanf(line, "%s %s %s %s %s", dummy, cpu, dummy, dummy, time);
printf("%s %s\n", cpu, time); // you will need to parse these strings
pclose(fp);
return;
}
}
pclose(fp);
}
}
int main() {
get_process_info(32324);
return 0;
}