2010-04-20 41 views
3

我想每使用代碼linux/rbtree.h的task_struct實現在Linux中紅/黑樹。我可以得到一個紅/黑樹在內核的獨立空間,正確地插入,如模塊,但是當我試圖讓同一代碼在任何task_structtask_struct->files_struct宣佈rb_root發揮作用,我得到了SEGFAULT每次我嘗試了插。Linux內核 - 紅/黑樹

下面是一些代碼:

在task_struct中我創建了一個rb_root結構爲我的樹(不是指針)。 在init_task.h,宏INIT_TASK(tsk),我設置這等於RB_ROOT。 執行插入,我用這個代碼:

rb_insert(&(current->fd_tree), &rbnode); 

這就是問題發生。

我插入命令是所有RBTree文檔中記載爲內核的標準插入:

int my_insert(struct rb_root *root, struct mytype *data) 
    { 
    struct rb_node **new = &(root->rb_node), *parent = NULL; 

    /* Figure out where to put new node */ 
    while (*new) { 
     struct mytype *this = container_of(*new, struct mytype, node); 
     int result = strcmp(data->keystring, this->keystring); 

     parent = *new; 
     if (result < 0) 
      new = &((*new)->rb_left); 
     else if (result > 0) 
      new = &((*new)->rb_right); 
     else 
      return FALSE; 
    } 

    /* Add new node and rebalance tree. */ 
    rb_link_node(&data->node, parent, new); 
    rb_insert_color(&data->node, root); 

    return TRUE; 
    } 

有我丟失的東西?

一些原因,如果我做了一個樹根task_struct之外,這將很好地工作?如果我在模塊內部製作rb_root,此插入工作正常。但是,一旦我將實際的樹根置於task_struct甚至task_struct->files_struct中,我會得到一個SEGFAULT。不能在這些結構中添加根節點?

任何尖端不勝感激。我嘗試了幾乎所有我能想到的。


編輯:

我在下面的行獲得SEGFAULT試圖打印和訪問樹中的任何線的時候。通過這一行,您應該瞭解我如何處理指針。內核中已有的方法是rb_entryrb_firstcurrent是一個指向任務結構(當前工作進程)的指針,樹是我的根節點(不是指針),它是任務結構(我添加的)的成員。 rb_first需要通過指針*rb_root。我做錯了。

printk(KERN_CRIT "node=%d\n", rb_entry(rb_first(&(current->tree)), struct rb_tree_struct, node)->fd_key); 
+0

它可能與我添加新節點的方式有任何關係。我只是結構mytype __name__分配空間,然後使用上面的插入添加它。我是否需要使用malloc(或者kmalloc),或者我可以像我正在做的那樣分配一個新節點? – CodeRanger 2010-04-20 22:42:37

回答

0

難道它是根和/或數據的指針值是不是你所期望的?在while()循環之前添加

printk("%s: root=%p data=%p\n", __func__, root, data); 

可能會有用。

+0

當試圖打印和任何訪問樹的行時,我在下面的行上得到一個SEGFAULT。通過這一行,您應該瞭解我如何處理指針。 rb_entry和rb_first是內核中已有的方法。 current是指向任務struct(當前工作進程)的指針,tree是我的根節點(不是指針),它是task結構的成員。 rb_first需要傳遞一個指針* rb_root。我做錯了。(rn_first(&(current-> tree)),struct rb_tree_struct,node) - > fd_key); – CodeRanger 2010-04-20 23:54:32