Search

Using the function list_del for deleting a node from the 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_del, which is used for deleting one of the nodes of the list. The syntax for calling the function is



Where entry is the pointer to the list_head data in the node which we want to delete. In the sample code below we first create a linked list with three nodes ,one,two and three and print each element of the list. Then we use the function list_del to delete one of the entries.



When we print the elements of the list again, we will see in the output that only two nodes are printed and the node that was deleted is no longer printed, hence making it clear that the node did get deleted.

The full module code is shown below.



Save the file as linked_list_del.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 as we passed the first node for deletion, only 20 and 30 are printed as part of the new list.

No comments:

Post a Comment