我正在尝试遍历一个进程的所有祖先,以将它们的信息存储在用户传递的静态数组中,并且我正在使用 NULL 指针结束标志来结束遍历。但是,这似乎不起作用,并且会继续循环,直到用户空间传递的大小数(数组的容量)在所有情况下都匹配 num_filed 数(数组中的元素数),即使我有很运行的进程数量很少。那么,穿越祖宗的终结标志似乎是什么呢?这是我的遍历循环代码:
current_process = current;
int i = 0;
while (current_process != NULL && current_num_filled < size) {
temp_info_array[i] = get_process_info(current_process);
++current_num_filled;
++i;
current_process = current_process->parent;
}
最佳答案
您可以在/include/linux/init_task.h
中看到INIT_TASK()
宏用于为init进程初始化struct task_struct
>。特别是:
#define INIT_TASK(tsk) \
{ \
.state = 0, \
.stack = &init_thread_info, \
\
/* ... */ \
\
.real_parent = &tsk, \
.parent = &tsk, \
\
/* ... */ \
}
正如您所见 - init 的 struct task_struct
的 parent
成员被设置为指向自身,而不是空指针。
关于c - C中内核空间遍历祖先的结束标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36540595/