Search

Implementing stack using the list functions in kernel -2

In the post "Implementing stack using the list fuctions in kernel" we saw how to create a stack entry using the in built list functions of the kernel. Some of the operations that we did while creating the stack can further be automated using some more list functions of the kernel.

We had used a variable "tail" to keep track of the last element of the stack, which was being updated in the push_stack function after every write to the stack. This is not really needed is we use the function list_last_entry.



ptr: Pointer to the head node of the list. type: type of structure of which the head is member of member: The name of the head node in the structure.

For our example, we can use list_last_entry as below.

list_last_entry automatically returns the last node of the list and there is no need to keep track of the tail node.

Thus the push function can be modified as below.



In the pop_stack function, the check for stack being empty was being performed by comparing the tail with the head pointer. This can be easily by using the function list_empty, which returns true is the list is empty



head: Pointer to the head node of the list which is being tested.

list_last_entry can also be used in the pop_stack function to read out the last element of the stack.

Thus the modified pop_stack will look as below.



The complete modified code for stack being

stack_list.c



To compile the module use the make file.



Compile and insert the module into the kernel



To see the output



Thus we have written the 1,2,3,4 as data into 4 nodes with 4 being inserted last. Now to pop data out of the stack



Thus we can see that the proc entry is operating as a stack.
Related posts

Implementing stack using the list functions in kernel
Creating a read write proc entry in kernel versions above 3.10
Pointer to structure from its member pointer: container_of

No comments:

Post a Comment