0
votes

How do I add a field to the struct task_struct in the sched.h file, the field consists of a linked list with a special non primitive node?

say i have this structure:

struct Node{
 int counter;
 char* buffer;
 int sum;
} 

and so on, and I want to add a list of such nodes? I don't want to re-implement the list with

struct Node{
 int counter;
 char* buffer;
 int sum;
 Node* next;
} 

but I want to use struct list_head with the generic kernel list and not my implementation. But adding struct list_head to the task_struct just gives me another instance of task_struct while I want to add my special list with my specific fields.

In terms of C++, i want to add List to the struct_task so the file scehd.h will look something like

struct task_struct {
.
.
.
List<Node> myList;
.
.
}

How do i do that and how do I then how do initialize my node in the INIT_TASK macro?

1

1 Answers

1
votes

You say "adding struct list_head to the task_struct just gives me another instance of task_struct". Which is not true, unless I'm misunderstanding you.

Adding a struct list_head to the task_struct just gives you a struct list_head in the task_struct, nothing more nothing less. It's up to you to decide what you use the list_head for.

To implement what you have in mind, you would need something like:

struct task_struct {
    ...
    struct list_head node_list;
};

struct your_node {
    struct list_head list;
    int counter;
    char* buffer;
    int sum;
};

When the task_struct is created you initialise node_list to be an empty list. When you create an instance of your_node you do likewise and then add it to the node_list with:

list_add_tail(&your_node->list, &your_task->node_list);

For a real example see perf_event_list in the task_struct, and the code that iterates over it in kernel/events/core.c.