Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to complete the PART 2 ASSIGNMENT portion. The rest is for your reference. This is the code I have and it is not

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

I need to complete the PART 2 ASSIGNMENT portion. The rest is for your reference. This is the code I have and it is not compiling when I run 'make' command in terminal. I am getting multiple errors. I also pasted the make file code for your reference. Any help would be greatly appreciated!!

___________________________________________________________________________________________________________________________

#include #include #include #include #include

struct birthday { int month; int day; int year; char *name; struct list_head list; };

static LIST_HEAD(birthday_list);

int simple_init(void) { int i=0; int day = 2, month = 8 ,year = 1995; char* name[] = {"linux","unix","Mac","Windows","Solaris"}; struct birthday *temp = NULL;

while(iday = day++; temp->month = month++; temp->year = year++; temp->name = name[i]; INIT_LIST_HEAD(&(temp->list)); list_add_tail(&(temp->list) , &(birthday_list));

i++; } temp = NULL;

list_for_each_entry(temp, &birthday_list, list){

printk(KERN_INFO "Name : ",temp->name); printk(KERN_INFO "Birthday day : ",temp->month,temp->day,temp->year); }

return 0; }

int simple_exit(void) { struct birthday* ptr=NULL , *next = NULL; int i =1;

list_for_each_entry_safe(ptr,next,&birthday_list,list){

printk(KERN_INFO "Removind Element from list ",i++); list_del(&(ptr->list)); kfree(ptr); } } module_init(simple_init); module_exit(simple_exit);

MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Simple Module"); MODULE_AUTHOR("SGG");

_______________________________________________________________________________________________________________________________________

**MAKE FILE CODE**

obj-m += simple-solution.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

Programming Projects Linux Kernel Modules In this project, you will learn how to create a kernel module and load it into the Linux kernel. The project can be completed using the Linux virtual machine that is available with this text. Although you may use an editor to write these C programs, you programs, and you manage the modules in the kernel. will have to use the terminal application to compile the will have to enter commands on the command line to As you'll discover, the advantage of developing kernel modules is that it is a relatively easy method of interacting with the kernel, thus allowing you to write programs that directly invoke kernel functions. It is important for you to keep in mind that you are indeed writing kernel code that directly interacts with the kernel, That normally means that any errors in the code could crash the system! However, since you will at wor will be using a virtual machine, any failures st only require rebooting the system. Part I-Creating Kernel Modules The first part of this project involves following a series of steps for creating and inserting a module into the Linux kernel. Programming Projects 97 You can list all kernel modules that are currently loaded by entering the lsmod This command will list the current kernel modules in three columns: name, size, and where the module is being used. The following program (named simple.c and available with the source code for this text) illustrates a very basic kernel module that prints appropriate messages when the kernel module is loaded and unloaded. #include #include #include This function is called when the module is loaded. int simple.init(void) printk(KERN INFO "Loading Module "); return 0; This function is called when the module is removed. void simple_exit(void) printk(KERN INFO "Removing Module "); /*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") The function simple_initO is the module entry point, which represents the functionthat is invoked when the module is loaded into the kernel. Similarly, the simple.exitO function is the module exit point-the function that is called when the module is removed from the kernel. The module entry point f unction must return an integer value, with 0 representing success and any other value representing failure. The module exit point function returns void. Neither the module entry point nor the module exit point is passed any parameters. The two following macros are used for registering the module entry and exit points with the kernel: module_init) module exitO Notice how both the module entry and exit point functions make calls to the printk) function. printk) is the kernel equivalent of printfO, yet its output is sent to a kernel log buffer whose contents can be read by the dmesg command. One difference between printf) and printk ) is that printkO allows us to specify a priority flag whose values are given in the include file. In this instance, the priority is KERN INFO, which is defined as an informational message. The final lines-MODULE LICENSEO), MODULE DESCRIPTIONO), and MOD- ULE AUTHOR )-represent details regarding the software license, description of the module, and author. For our purposes, we do not depend on this information, but we include it because it is standard practice in developing kernel modules. This kernel module simple.c is compiled using the Makefile accom- panying the source code with this project. To compile the module, enter the following on the command line: make The compilation produces several files. The file simple.ko represents the compiled kernel module. The following step illustrates inserting this module into the Linux kernel. Loading and Removing Kernel Modules Kernel modules are loaded using the insmod command, which is run as follows: sudo insmod simple.ko To check whether the module has loaded,enter thelsmod command and search for the module simple. Recall that the module entry point is invoked when he module is inserted into the kernel. To check the contents of this message in the kernel log buffer, enter the command dmesg You should see the message "Loading Module." Removing the kernel module involves invoking the rmmod command (notice that the .ko suffix is unnecessary): sudo rmmod simple Because the kernel log buffer can fill up quickly, it often makes sense to clear the buffer periodically. Thi is can be accomplished as follows: sudo dmesg -c Programming Projects Part I Assignment Proceed through the steps described above to create the kernel module and to load and unload the module. Be sure to check the contents of the kernel log butter using dmesg to ensure you have properly followed the steps. Part II-Kernel Data Structures The second part of this project involves modifying the kernel module so that it uses the kernel linked-list data structure. In Section 1.10, we covered various data structures that are common in operating systems. The Linux kernel provides several of these structures. Here we explore using the circular, doubly linked list that is available to kernel developers. Much of what we discuss is available in the Linux source code- in this instance, the include file and we recommend that you examine this fi le as you proceed through the following steps. Initia lly, you must define a struct containing the elements that are to be inserted in the linked list. The follo lowing C struct defines birthday s: struct birthdayf int day int month; int year; struct list_head list; We create and initialize instances of struct birthday as follows: struct birthday *person; person = kmalloc (sizeof (*person), person->day2; person->month- 8; person->year 1995; INIT LIST_HEAD (&person->list); GFP-KERNEL); The kmalloc O function is the kernel equivalent of the user-level mallocO function for allocating memory, except (The GFP KERNEL flag indicates routine kernel memory allocation.) The macro INIT LIST_HEAD) initializes the list member in struct birthday. We can then add this instance to the end of the linked list using the list addtail) that kernel memory is being allocated. macrO: list add tail (&person->list, &birthdayst); Traversing the I Traversing the list involves using the list.for each entryO Macro, which inked List accepts three parameters: A pointer to the structure being iterated over pointer to the head of the list being iterated over The name of the variable containing the list head structure The following code illustrates this macro: struct birthday *ptr; list for_each entry(ptr, &birthday_list, list) { /*on each iteration ptr points /*to the next birthday struct / Removing Elements from the Linked List Removing elements from the list involves using the list_del O macro, which is passed a pointer to struct list head list.del (struct list head element) This removes element from thelist while maintaining the structure of the remainder of the list. Perhaps the simplest approach fo r removing all elements from a traverse the list. The macro linked list is to remove each element as you list for_each_entry_safe () behaves much like list_for.each entry () Bibliographical Notes 101 except that it is passed an additional argument that maintains the value of the next pointer of the item being deleted. (This is necessary for preserving the structure of the list.) The following code example illustrates this macro: struct birthday *ptr, *next list_for_each entry_safe(ptr,next,&birthday_list,list) { /*on each iteration ptr points*/ /*to the next birthday struct */ list del (&ptr->list); kfree(ptr); Notice that after deleting each element, we return memory that was previously allocated with kmalloc) back to the kernel with the call to kfree. Careful memory management-wh which includes releasing memory to prevent memory leaks-is crucia I when developing kernel-level code. Part II Assignment In the module entry point, create a linked list containing five struct birthday elements. 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. In the module exit point, delete the elements from the linked list and return heck the free memory back to the kernel. Again, invoke the dmesg command to c that the list has been removed once the kernel module has been unloaded

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Shamkant B. Navathe

7th Edition Global Edition

1292097612, 978-1292097619

More Books

Students also viewed these Databases questions

Question

Define promotion.

Answered: 1 week ago

Question

Write a note on transfer policy.

Answered: 1 week ago

Question

Discuss about training and development in India?

Answered: 1 week ago

Question

Explain the various techniques of training and development.

Answered: 1 week ago