Pages

Using the function list_is_first for finding if a node is the first node in kernel linked list

In the post Create a linked list in linux kernel 5.3 we learnt the details of creating a linked list in a linux kernel. There are number of functions available to help us manage the linked list easily once created, all of them defined in the file "linux/list.h" Let us look at the function list_is_first, which is used to find if the given node is the first node of the linked list or not.



Where list is the node which is to be checked if the it is the first node of the list or not.
head is the head node of the list.

The function list_is_first returns true of the node being checked is the head node, else it will return false. In the example below we will create a linked list with 3 nodes , one,two and three. One being the first node of the list and three being the last node. The nodes two and one both are passed to the list_is_first function and in the output we will see that when the node two is passed as the entry to be checked, the function returns false



When the node one is passed it returns true because node one is the first node of the list.

The full module code is shown below.



Save the file as linked_list_is_first.c . Use the following makefile to compile the module .



Compile and insert the module using



To see the output run the command

The output should look as shown below



We can see that for node two, the message printed is Not the first node where as for node one the function returns true and prints the message It is first node

No comments:

Post a Comment