Linux Kernel Linked List Example
# Include <Linux/kernel. h>
# Include <Linux/module. h>
# Include <Linux/init. h>
# Include <Linux/string. h>
# Include <Linux/spinlock. h>
# Include <Linux/list. h>
Module_license ("GPL ");
Module_author ("wzt ");
Typedef struct list_test_struct {
Int count;
Struct list_head list;
} Node;
Struct list_head head_list;
Rwlock_t list_lock = rw_lock_unlocked;
Void creat_list (void)
{
Node * s = NULL;
Int I = 0;
For (I = 0; I <10; I ++ ){
S = (node *) kmalloc (sizeof (node), gfp_atomic );
If (! S)
Return;
S-> COUNT = I;
Write_lock (& list_lock );
List_add_tail (& (S-> list), & head_list );
Write_unlock (& list_lock );
}
}
Void del_data (void)
{
Node * s;
Struct list_head * P;
Write_lock (& list_lock );
List_for_each (p, & head_list ){
S = list_entry (p, node, list );
If (S-> COUNT = 5 ){
List_del (P );
Write_unlock (& list_lock );
Return;
}
}
Write_unlock (& list_lock );
}
Void print_list (void)
{
Node * s;
Struct list_head * P;
Read_lock (& list_lock );
List_for_each (p, & head_list ){
S = list_entry (p, node, list );
Printk ("% d,", S-> count );
}
Write_lock (& list_lock );
}
Int list_test_init (void)
{
Init_list_head (& head_list );
Creat_list ();
Print_list ();
Del_data ();
Print_list ();
Return 0;
}
Void list_test_exit (void)
{
}
Module_init (list_test_init );
Module_exit (list_test_exit );