Operating Systems
Project Reports
Class : SE
Major : software engineering
Name : nigussie nafiyad reta Student ID :
201932130104
P1 P2 P3 P4 Total
College of Mathematics and Computer Science
Content
Project 1.................................................................................................................................................. 1
Project 2.................................................................................................................................................. 2
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. You can list all
kernel modules that are currently loaded by entering the
command 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 <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
/* This function is called when the module is loaded. */ int
simple_init(void)
{
}
/* 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);
printk(KERN INFO "Loading Module\n");
return 0;
source code
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/slab.h>
struct person{
int month;
int day;
int year;
struct person *next;
};
struct person *mainList = NULL;
struct person* newNode(struct person* val, int month, int day, int
year){
if(val == NULL){
val = (struct person*)kmalloc(sizeof(struct person),
GFP_KERNEL);
val -> month = month;
val -> day = day;
val -> year = year;
}else{
val -> next = newNode(val->next, month, day, year);
}
return val;
}
void printList(struct person *list){
printk(KERN_INFO "Printing List");
while(list != NULL){
printk(KERN_INFO "%d, %d, %d\n", list -> month, list -> day, list
-> year);
if(list -> next != NULL){
list = list -> next;
}
}
}
struct person *deleteList(struct person *list){
if(list != NULL){
list = deleteList(list->next);
return NULL;
}
kfree(mainList);
return NULL;
}
void populateList(struct person *mainList){
mainList = newNode(mainList, 10, 14, 2020);
mainList = newNode(mainList, 10, 15, 2019);
mainList = newNode(mainList, 1, 20, 1432);
mainList = newNode(mainList, 4, 3, 12);
mainList = newNode(mainList, 8, 3, 1999);
mainList = newNode(mainList, 5, 27, 2005);
printList(mainList);
}
/* This function is called when the module is loaded. */
int simple_init(void)
{
printk(KERN_INFO "Loading Module\n");
populateList(mainList);
return 0;
}
/* This function is called when the module is removed. */
void simple_exit(void) {
printk(KERN_INFO "Removing Module\n");
printk(KERN_INFO "Deleting List\n");
deleteList(mainList);
printk(KERN_INFO "List Deleted\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");
Part II—Kernel Data Structures
Source code
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/slab.h>
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(i<5){
temp = kmalloc(sizeof(*temp),GFP_KERNEL);
temp->day = 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 : <%s> ",temp->name);
printk(KERN_INFO "Birthday day : <%d/%d/%d>",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 <%d> 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");