Given the following code Please sort the five struct birthday elements in the li
ID: 3673554 • Letter: G
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 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
needs to be implemented but im not entirely sure how.
===============================================================================================
#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 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");
Explanation / Answer
Consider the following program: /* * Simple demonstratino of the seq_file interface * Felix SHI */ #include #include #include #include /* everything... */ #include #include /* printk() */ #include /* kmalloc() */ #include /* size_t */ #include MODULE_AUTHOR("Felix Shi"); MODULE_LICENSE("Dual BSD/GPL"); struct info_struct { struct list_head list; char info[10]; }; static LIST_HEAD(data_list); void empty_list(void) { struct info_struct *p, *ptmp; list_for_each_entry_safe(p, ptmp, &data_list, list){ list_del(p); kfree(p); } } /* The procfs read entry point */ /* static int proctest_read(char *page, char **start, off_t offset, int count, int *eof, void *data) { int i = 0; off_t len = 0; struct info_struct *p; list_for_each_entry(p, &data_list, list) { len += sprintf(page + len, p->info); } *eof = 1; return len; }*/ static int proctest_large_read(char *page, char **start, off_t offset, int count, int *eof, void *data) { off_t start_point = 0; off_t len = 0; struct info_struct *p; list_for_each_entry(p, &data_list, list){ len += sprintf(page + len, p->info); if(start_point + len = offset + count){ break; } } /* Actual start */ *start = page + (offset - start_point); len -= (offset - start_point); if(len > count) len = count; else *eof = 1; return len; } static int seq_test_init(void) { int i; static struct proc_dir_entry *entry = NULL; struct info_struct *pinfo; entry = create_proc_entry("seqtest", 0, NULL); if(entry) //entry->read_proc = proctest__read; entry->read_proc = proctest_large_read; for(i = 0; i < 500; ++i){ pinfo = kmalloc(sizeof(struct info_struct), GFP_ATOMIC); if(!pinfo){ empty_list(); return ENOMEM; } sprintf(pinfo->info, "Node No: %d ", i); list_add_tail(&pinfo->list, &data_list); } return 0; } static void seq_test_exit(void) { remove_proc_entry("seqtest",NULL); empty_list(); } module_init(seq_test_init); module_exit(seq_test_exit);Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.