C: Linux is built in a linked list using kernel data structures

I am trying to add a system call to my linux kernel, and it would be useful to use the built-in linked list as I modify task_struct (by adding a linked list), and task_struct already has quite a few struct list_head there for other purposes. For consistency, I would like to adhere to this data structure.

My problem is that I really don't quite understand how to use this structure. I see that they have a "struct list_head children", for example. However, the implementation of this structure is simple: "* next" and "* last".

I looked at examples on the Internet, and each of them says do well

struct node{
    int data;
    struct list_head list;
}; 

But does this mean that the data structure that I should include in my task_struct should be

struct node list;

?

, , , , , list_head.

, char * ( ).

... , , task_struct.

: , :

, . char *. task_struct, .

'abc' printf ~ > "WriteScreen" getchar ~ > "ReadKey"

, . , ( ), "" .

task_struct 'abc'

abc- > task_struct- > tag_list

"WriteScreen" "ReadKey".

, WriteScreen, ReadKey .. , , , .

+4
3

, (task_struct) , , - .

, , .. .

/ , ( ).

node :

struct my_node {
    struct list_head list;
    char data[100]; // arbitrarily set to 100; could be also char*
}

_ :

struct task_struct {
  // many members that contains info about a process
  ...

  struct list_head my_list;
}

. , ( , ), ; .

, , ( ):

INIT_LIST_HEAD(&new_process.my_list);

node (, , ):

struct my_node *node; 
struct task_struct *a_process;

[... my_node initialized ...]
[... a_proccess obtained somehow ...]

list_add_tail(&node->list, &a_process->my_list);

:

struct my_node *p;
struct task_struct *a_process

// list is the member name (yes, the member name) of your list inside my_node
list_for_each_entry(p, &a_process->my_list, list) {
  // do whatever you want with p
}

EDIT:

, , , , .

, char char (, ..). :

"WriteScreen,ReadKey\0"

, . , .

+4

Linux - http://www.makelinux.net/ldd3/chp-11-sect-5.

:

struct node node_var = {
    .data = 0,
    .list = LIST_HEAD_INIT(node_var.list)
}

:

struct list_head *phead;
list_for_each(phead, node_var.list)
{
   struct node * pnode = list_entry(phead, struct node node_var, list);
   // Do what you may with the pnode.
}

, . struct node struct list_head . container_of, list_entry.

, .

+2

EDIT. , OP , / .

task_struct :

struct task_struct {
  // many members that contains info about a process
  ...
  // then come the lists that a process may participate
  ...
  // then you amend with your new list
  struct list_head my_list;
}

, .

(, ), addind (task_struct) .

LIST_HEAD(my_list_head); // this will declare, define and initialize a new variable:
                         // an empty list.

Linux .

task_struct *a_given_process; // assigned elsewhere, maybe passed as parameter to current function
list_add_tail(&a_given_process->my_list, my_list_head); // an example

EDIT:

:

struct task_struct *p;

// my_list_head is the head of your list (declared with LIST_HEAD)
// my_list is the member name (yes, the member name) of your list inside task_struct
list_for_each_entry(p, my_list_head, my_list) {
  // do whatever you want with p
}
+2

All Articles