Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given the following code Please sort the five struct birthday elements in the li

ID: 3673573 • 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

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/slab.h>

struct birthday
{
    int day;
    int month;
    int year;
    struct list_head list;
};

struct list_head birthday_list;

struct birthday *createBirthday(int day, int month, int year)
{
    struct birthday *person = kmalloc(sizeof(struct birthday), GFP_KERNEL);

    person->day = day;
    person->month = month;
    person->year = year;

    return person;
}

void printInfo(char *str)
{
    printk(KERN_INFO "Kernal: %s", str);
}

int simple_init(void)
{
    struct birthday *person = createBirthday(13, 4, 1987);
    struct birthday *ptr;

    printInfo("Loading Module ");

    INIT_LIST_HEAD(&birthday_list);

    list_add_tail(&person->list, &birthday_list);
    person = createBirthday(14, 4, 1987);
    list_add_tail(&person->list, &birthday_list);
    person = createBirthday(15, 4, 1987);
    list_add_tail(&person->list, &birthday_list);
    person = createBirthday(16, 4, 1987);
    list_add_tail(&person->list, &birthday_list);
    person = createBirthday(17, 4, 1987);
    list_add_tail(&person->list, &birthday_list);

    list_for_each_entry(ptr, &birthday_list, list) {
        printk(KERN_INFO "OS Module: Day %d.%d.%d ", ptr->day, ptr->month, ptr->year);
    }

       return 0;
}

void simple_exit(void)
{
    struct birthday *tmp;
    struct list_head *ptr, *next;

    printInfo("Removing Module ");

    if (list_empty(&birthday_list)) {
        printInfo("List is empty");
        return;
    }

    list_for_each_safe(ptr, next, &birthday_list)
    {
        tmp = list_entry(ptr, struct birthday, list);
        printk(KERN_INFO "OS Module: Removing %d.%d.%d ", tmp->day, tmp->month, tmp->year);
        list_del(ptr);
        kfree(tmp);
    }

    printInfo("Module removed ");
}

module_init( simple_init );
module_exit( simple_exit );

MODULE_LICENSE("HP");
MODULE_DESCRIPTION("Birthday List");
MODULE_AUTHOR("Abhishek");

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote