2

由于某些原因,我无法从 task_for_pid() 中得到任何信息相同的(操作系统/内核)失败错误。

#include <stdio.h>
#include <mach/mach_traps.h>
#include <mach/mach_init.h>

int main(int argc, char* argv[])
{
mach_port_name_t task;
printf("%d\n", argv[1]);
int pid = atoi(argv[1]);
printf("%d\n%d\n", pid, current_task());
int error = task_for_pid(2055, 24269, &task);
printf("%x\n", task);
if (error)
{
printf("task_for_pid return error:\n %s\n", mach_error_string(error));
} else {
printf("Get the process %d's task port : %x\n", pid, task);
}
return 0;
}

输出如下所示:

gcc -o test test.c;./test 24269
803206115
24269
2055
0
task_for_pid return error:
 (os/kern) failure

关于我为什么没有得到任务的任何想法?我以root身份运行它。

正如亚当罗森菲尔德所说,它确实在标题中说它已经过时了,但如果那是真的,我还能用旧版本的工具链编译和运行它吗?或者它被什么取代了?有人知道吗?

4

1 回答 1

2
  1. 你确定你是以root身份运行的吗?
  2. 您确定进程 24269 仍在运行吗?

我在 Mac OS X 10.6.8 上使用任何正在运行的进程运行此代码(使用 sudo)都没有问题:

#include <stdio.h>
#include <stdlib.h>
#include <mach/mach_traps.h>
#include <mach/mach_init.h>
#include <mach/mach_error.h>

int main(int argc, char* argv[])
{
    task_t task;
    pid_t pid = argc >= 2 ? atoi(argv[1]) : 1;
    kern_return_t error = task_for_pid(current_task(), pid, &task);
    printf("%d -> %x [%d - %s]\n", pid, task, error, mach_error_string(error));
    return error;
}

例如,这是我使用 pid 182 (Dock) 的结果

$ sudo ./task_for_pid 182
182 -> 413 [0 - (os/kern) successful]
于 2011-07-13T15:31:19.033 回答