Last active
January 24, 2018 13:41
-
-
Save jeremiahlukus/33d63a3f56ae4a96d0fd4a860e55074b to your computer and use it in GitHub Desktop.
C program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Edit the incomplete C source code to create a linked list to store the birthday information of 6 random students. For each person, the birthday information should include month, day, year, and name.When the module is loaded, traverse through the linked list and output its contentto the kernel log buffer. In addition, write code to identifythe youngeststudentby year (you can define 6 students born in 6 different years.)and remove that student from the list. After removing the youngest student, | |
output the updated linked list content to the kernel log buffer. In the module exit point, delete the elements from the updated linked list and returnthe free memory back to the kernel.Makesure to output a message to the kernel log buffer every time an element is deleted. | |
MakeFile | |
``` | |
obj-m += simple.o | |
all: | |
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules | |
clean: | |
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean | |
``` | |
simple.c | |
``` | |
#include <linux/init.h> | |
#include <linux/module.h> | |
#include <linux/kernel.h> | |
#include <linux/types.h> | |
#include <linux/slab.h> | |
struct birthday { | |
int day; | |
int month; | |
int year; | |
struct list_head list; | |
}; | |
struct birthday *person, *tmp, *ptr1, *ptr2, *ptr3, *ptr4, *ptr5; | |
static LIST_HEAD(birthday_list); | |
struct list_head *pos; | |
/* This function is called when the module is loaded. */ | |
int simple_init(void) | |
{ | |
person = kmalloc(sizeof(*person),GFP_KERNEL); | |
person->day = 1; | |
person->month = 9; | |
person->year = 1999; | |
int jeremiah_day = 8; | |
int jeremiah_month = 3; | |
int jeremiah_year = 1978; | |
int jeff_day = 9; | |
int jeff_month = 12; | |
int jeff_year = 1958; | |
int ahmid_day = 9; | |
int ahmid_month = 6; | |
int ahmid_year = 2003; | |
int nicole_day = 8; | |
int nicole_month = 3; | |
int nicole_year = 1978; | |
///set to head | |
INIT_LIST_HEAD(&person->list); | |
printk(KERN_INFO "Loading Module\n"); | |
ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL); | |
ptr1->day = jeremiah_day; | |
ptr1->month = jeremiah_month; | |
ptr1->year = jeremiah_year; | |
list_add(&(ptr1->list), &(person->list)); | |
ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL); | |
ptr2->day = jeff_day; | |
ptr2->month = jeff_month; | |
ptr2->year = jeff_year; | |
list_add(&(ptr2->list), &(person->list)); | |
ptr3 = kmalloc(sizeof(*ptr3), GFP_KERNEL); | |
ptr3->day = ahmid_day; | |
ptr3->month = ahmid_month; | |
ptr3->year = ahmid_year; | |
list_add(&(ptr3->list), &(person->list)); | |
ptr4 = kmalloc(sizeof(*ptr4), GFP_KERNEL); | |
ptr4->day = nicole_day; | |
ptr4->month = nicole_month; | |
ptr4->year = nicole_year; | |
list_add(&(ptr4->list), &(person->list)); | |
list_for_each(pos, &(person->list)){ | |
tmp = list_entry(pos, struct birthday, list); | |
printk("day = %d month = %d year = %d\n", tmp->day, tmp->month, tmp->year); | |
} | |
return 0; | |
} | |
/* This function is called when the module is removed. */ | |
void simple_exit(void) { | |
printk(KERN_INFO "Removing Module\n"); | |
} | |
/* Macros for registering module entry and exit points. */ | |
module_init( simple_init ); | |
module_exit( simple_exit ); | |
MODULE_LICENSE("GPL"); | |
MODULE_DESCRIPTION("Simple Module"); | |
MODULE_AUTHOR("SGG"); | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment