New Part II Assignment: 1) In the module entry point, create a linked list conta
ID: 3673868 • Letter: N
Question
New Part II Assignment: 1) In the module entry point, create a linked list containing five struct birthday elements (respectively for the following people, from head to tail: Alice, Bob, Mallory, Nancy and Kate). Traverse the linked list and output its contents to the kernel log buffer. Invoke the dmesg command to ensure the list is properly constructed once the kernel module has been loaded. Please take the screen shots. Alice born on Jan 9, 1999; Bob born on March 8, 1978; Mallory born on Dec, 1958; Nancy born on Jun 9, 2003; Kate born on March 8, 1978;
2) Please sort the five struct birthday elements in the list by this sequence: from head to tail, the people are from old to young. You may need to come up with new functions if you need. Traverse the linked list again and output its contents to the kernel log buffer. Invoke the dmesg command to ensure the list is properly rearranged. Please take the screen shots.
3) In the module exit point, delete the elements from the linked list and return the free memory back to the kernel. Again, invoke the dmesg command to check that the list has been removed once the kernel module has been unloaded. Please take the screen shots.
Explanation / Answer
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/slab.h>
struct birthday {
char *name;
int day;
int month;
int year;
struct list_head list;
}birthday;
LIST_HEAD(birthday_list);
int birthdayList_init(void)
{
printk(KERN_INFO "Loading Module... ");
struct birthday *person;
person = kmalloc(sizeof(*person), GFP_KERNEL);
person->name = "Alice";
person->day = 9;
person->month = 1;
person->year = 1999;
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);
person = kmalloc(sizeof(*person), GFP_KERNEL);
person->name = "Bob";
person->day = 8;
person->month = 3;
person->year = 1978;
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);
person = kmalloc(sizeof(*person), GFP_KERNEL);
person->name = "Mallory";
person->day = 0;
person->month = 12;
person->year = 1958;
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);
person = kmalloc(sizeof(*person), GFP_KERNEL);
person->name = "Nancy";
person->day = 9;
person->month = 6;
person->year = 2003;
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);
person = kmalloc(sizeof(*person), GFP_KERNEL);
person->name = "Katie";
person->day = 8;
person->month = 3;
person->year = 1978;
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);
printk(KERN_INFO "List Out of Order... ");
struct birthday *ptr;
list_for_each_entry(ptr, &birthday_list, list)
{
printk(KERN_INFO "Birthday : Name: %s, Day: %d, Month: %d, Year: %d ",
ptr->name, ptr->day, ptr->month, ptr->year);
}
printk(KERN_INFO "Done... ");
return 0;
}
void birthdayList_exit(void)
{
printk(KERN_INFO "Removing ");
struct birthday *ptr, *nxt;
list_for_each_entry_safe(ptr, nxt, &birthday_list, list)
{
printk(KERN_INFO "Deleting : Name: %s, Day: %d, Month: %d, Year: %d ",
ptr->name, ptr->day, ptr->month, ptr->year);
list_del(&ptr->list);
kfree(ptr);
}
printk(KERN_INFO "free ");
}
module_init(birthdayList_init);
module_exit(birthdayList_exit);
MODULE_LICENSE("VP");
MODULE_DESCRIPTION("Birthday List ");
MODULE_AUTHOR("Abhishek);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.