HW1
This commit is contained in:
68
sample.c
Normal file
68
sample.c
Normal file
@ -0,0 +1,68 @@
|
||||
#include <linux/init.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/list.h>
|
||||
#include <linux/types.h>
|
||||
#include <linux/slab.h>
|
||||
|
||||
struct birthday
|
||||
{
|
||||
int day;
|
||||
int month;
|
||||
int year;
|
||||
struct list_head list;
|
||||
};
|
||||
|
||||
static LIST_HEAD(birthday_list);
|
||||
|
||||
/* This function is called when the module is loaded. */
|
||||
|
||||
int sample_init(void)
|
||||
{
|
||||
printk(KERN_INFO "Loading Module\n");
|
||||
|
||||
struct birthday *person;
|
||||
int i = 0;
|
||||
|
||||
for (i = 0; i < 5; i++)
|
||||
{
|
||||
person = kmalloc(sizeof(*person), GFP_KERNEL);
|
||||
person->day = 2;
|
||||
person->month = 8 + i ;
|
||||
person->year = 1995 + i;
|
||||
|
||||
INIT_LIST_HEAD(&person->list);
|
||||
list_add_tail(&person->list, &birthday_list);
|
||||
}
|
||||
|
||||
list_for_each_entry(person, &birthday_list, list)
|
||||
{
|
||||
printk(KERN_INFO "Birthday: %1d/%1d/%1d\n", person->year, person->month, person->day);
|
||||
}
|
||||
|
||||
printk(KERN_INFO "Module Loaded\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* This function is called when the module is removed. */
|
||||
|
||||
void sample_exit(void)
|
||||
{
|
||||
printk(KERN_INFO "Removing Module\n");
|
||||
struct birthday *person, *next;
|
||||
|
||||
list_for_each_entry_safe(person, next, &birthday_list, list)
|
||||
{
|
||||
list_del(&person->list);
|
||||
kfree(person);
|
||||
}
|
||||
|
||||
printk(KERN_INFO "Module Removed\n");
|
||||
}
|
||||
|
||||
/* Macros for registering module entry and exit points. */
|
||||
module_init(sample_init);
|
||||
module_exit(sample_exit);
|
||||
MODULE_LICENSE("GPL");
|
||||
MODULE_DESCRIPTION("Sample Module");
|
||||
MODULE_AUTHOR("SGG");
|
Reference in New Issue
Block a user