Follow instructions of “Chapter 2-Programming Projects-Linux Kernel Modules” on
ID: 3672269 • Letter: F
Question
Follow instructions of “Chapter 2-Programming Projects-Linux Kernel Modules” on the textbook "Operating Systems Concepts-9th Edition" Page 96. However, I’ve changed “Part II Assignment” for new assignment requirements. The new “Part II Assignment” is as follows:
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
Code to copy:
//Include Header files
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/slab.h>
//Create a structure.
struct birthday
{
char *name;
int day;
int month;
int year;
struct list_head list;
}birth;
//Function declaration.
Head_list(birthday_list);
//Function definition
int birthdayList(void)
{
//Print on the screen.
printk(KERN_INFO "Loading Module... ");
//Create an object of the structure birth.
struct birth *people;
//Create the dynamic size of the structure.
people = kmalloc(sizeof(*people), GFP_KERNEL);
//Attributes of the structure.
people->name = "Alice";
people->day = 9;
people->month = 1;
people->year = 1999;
//Function call.
INIT_Head_list(&people->list);
list_add_tail(&people->list, &birthday_list);
//Create the dynamic size of the structure.
people = kmalloc(sizeof(*people), GFP_KERNEL);
//Attributes of the structure.
people->name = "Bob";
people->day = 8;
people->month = 3;
people->year = 1978;
//Function call.
INIT_Head_list(&people->list);
list_add_tail(&people->list, &birthday_list);
//Create the dynamic size of the structure.
people = kmalloc(sizeof(*people), GFP_KERNEL);
//Attributes of the structure.
people->name = "Mallory";
people->day = 0;
people->month = 12;
people->year = 1958;
//Function call.
INIT_Head_list(&people->list);
list_add_tail(&people->list, &birthday_list);
//Create the dynamic size of the structure.
people = kmalloc(sizeof(*people), GFP_KERNEL);
//Attributes of the structure.
people->name = "Nancy";
people->day = 9;
people->month = 6;
people->year = 2003;
//Function call.
INIT_Head_list(&people->list);
list_add_tail(&people->list, &birthday_list);
//Create the dynamic size of the structure.
people = kmalloc(sizeof(*people), GFP_KERNEL);
//Attributes of the structure.
people->name = "Katie";
people->day = 8;
people->month = 3;
people->year = 1978;
//Function call.
INIT_Head_list(&people->list);
list_add_tail(&people->list, &birthday_list);
printk(KERN_INFO "List Out of Order... ");
//Create an object of the structure.
struct birth *pt;
//Function
list_for_each_entry(pt, &birthday_list, list)
{
printk(KERN_INFO "Birthday : Name: %s, Day: %d, Month: %d, Year: %d ",
pt->name, pt->day, pt->month, pt->year);
}
printk(KERN_INFO "Done... ");
return 0;
}
//Function definition.
void birthday_exit(void)
{
printk(KERN_INFO "Removing ");
struct birth *pt1, *next;
//Function definition.
list_for_each_entry_safe(ptr1, next, &birthday_list, list)
{
printk(KERN_INFO "Deleting : Name: %s, Day: %d, Month: %d, Year: %d ",
ptr1->name, ptr1->day, ptr1->month, ptr1->year);
list_del(&ptr1->list);
kfree(ptr1);
}
printk(KERN_INFO "free ");
}
//Module call
module_init(birthdayList_init);
module_exit(birthdayList_exit);
MODULE_LICENSE("VP");
MODULE_DESCRIPTION("Birth List ");
MODULE_AUTHOR("Mila Tran");