In the post " " we saw how to create as linked list using the built in functions of linux. Now let us see how we can delte the nodes
of the linked list.
The deltetion of nodes in a linked list has to be done carefully to make sure that even after the deletion of a node the list retains its structure. That is no matter from which position the node is deleted, the head, the tail or in between, the link between the remaining nodes should be retained.
If we delete the node at the head, then the node to which it was pointing to becomes the new head. If we delete the node from the tail, then the node to before it becomes the tail and if we delete the node from in between, the node previous to the delted node should be made to point to the node to which the deleted node was pointing to, making sure that the link between the nodes are maintained.
The kernel developers have made the above process very easy for us by providing the function
entry is the pointer to the node which we want to delete.
We need not be bothered whether it is the head,tail or in between the kernel takes function takes care of it.
Let us take the code that we used to create the list in the post "Creating a linked list"
In the init part of the module we have create three nodes in the linked list. Now let us add the deletion of nodes to the same init function, and print the linked list after every deletion.
For example to delete the "one" we need to call the function
and after every deletion we will iterate over the list using list_for_each
delete_list.c
To compile the module use the make file.
Compile and insert the module into the kernel
To see the output use the command demsg
In the above output the first three lines are creation of the linked list. Then after deleting each node we iterated through the list and see the that each call to list_de deletes one node from the linked list.
The deltetion of nodes in a linked list has to be done carefully to make sure that even after the deletion of a node the list retains its structure. That is no matter from which position the node is deleted, the head, the tail or in between, the link between the remaining nodes should be retained.
If we delete the node at the head, then the node to which it was pointing to becomes the new head. If we delete the node from the tail, then the node to before it becomes the tail and if we delete the node from in between, the node previous to the delted node should be made to point to the node to which the deleted node was pointing to, making sure that the link between the nodes are maintained.
The kernel developers have made the above process very easy for us by providing the function
entry is the pointer to the node which we want to delete.
We need not be bothered whether it is the head,tail or in between the kernel takes function takes care of it.
Let us take the code that we used to create the list in the post "Creating a linked list"
In the init part of the module we have create three nodes in the linked list. Now let us add the deletion of nodes to the same init function, and print the linked list after every deletion.
For example to delete the "one" we need to call the function
and after every deletion we will iterate over the list using list_for_each
delete_list.c
To compile the module use the make file.
Compile and insert the module into the kernel
To see the output use the command demsg
In the above output the first three lines are creation of the linked list. Then after deleting each node we iterated through the list and see the that each call to list_de deletes one node from the linked list.
Why kfree() not used to free allocated memory by kmalloc()?
ReplyDeleteit's inplicit called free i guess
Delete