结束标志遍历祖先在C中的内核空间 [英] The ending flag to traversing ancestors in the kernel space in C

查看:88
本文介绍了结束标志遍历祖先在C中的内核空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图穿越过程的所有祖先来存储他们的信息由用户通过一个静态数组,而我使用的是NULL指针结束标志结束穿越。然而,这似乎不工作,并且将继续循环,直到由用户空间传递的大小号码(阵列的容量)匹配num_filed数在所有情况下(数组中元素的数目),即使我有很进程数少跑。所以,表里如一是遍历祖先结束的标志?这是我穿越环code:

  current_process =电流;
INT I = 0;
而(current_process = NULL&放大器;!&安培; current_num_filled<大小){
    temp_info_array [I] = get_process_info(current_process);
    ++ current_num_filled;
    ++我;
    current_process = current_process->父母;
}


解决方案

您可以看到用于初始化结构task_struct中的 INIT_TASK() /include/linux/init_task.h init进程。特别是:

 的#define INIT_TASK(TSK)\\
    {\\
            .STATE = 0,\\
            .STACK =安培; init_thread_info,\\
                                                   \\
            / * ... * / \\
                                                   \\
            .real_parent =安培; TSK,\\
            .parent =安培; TSK,\\
                                                   \\
            / * ... * / \\
    }

因此​​,大家可以看到 - 在 init的结构的task_struct 设置点本身的成员,而不是空指针。

I am trying to traverse all the ancestors of a process to store their info in a static array passed by the user, and I am using the NULL pointer end flag to end traversing. However, this does not seem to work, and will continue looping until the size number (the capacity of the array) passed by the user space matches num_filed number (the number of elements in the array) in all cases, even if I have very little number of processes running. So, what seem to be the ending flag for traversing ancestors? Here is my traversing loop code:

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;
}

解决方案

You can see the INIT_TASK() macro used to initialize the struct task_struct for the init process in /include/linux/init_task.h. In particular:

    #define INIT_TASK(tsk)  \
    {                                              \
            .state          = 0,                   \
            .stack          = &init_thread_info,   \
                                                   \
            /* ... */                              \
                                                   \
            .real_parent    = &tsk,                \
            .parent         = &tsk,                \
                                                   \
            /* ... */                              \
    }

So as you can see - the parent member of init's struct task_struct is set point to itself, not the null pointer.

这篇关于结束标志遍历祖先在C中的内核空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆