Question: Given the following code Please sort the five struct birthday elements in the list by this sequence: from head to tail, the people are from
Given the following code 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.
Im pretty sure that a method similar to
void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv, struct list_head *a, struct list_head *b))
needs to be implemented but im not entirely sure how.
============================================================
#include
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 Module... ");
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 "Memory free... "); }
module_init(birthdayList_init); module_exit(birthdayList_exit);
MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Birthday List - CSC 212"); MODULE_AUTHOR("Horris McDavey");
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
