You are on page 1of 10

Name: Ahmed shimels muhe

Reg :no:18BCE2427
a) Create a kernel data structure with three fields to
store employee data.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/slab.h>
struct Employee //Structure definition
{
string fname, lname;
double salary;
Employee *next;
};
void create_list(Employee *head)
{
//To create linked list
string line;
char *temp, *word;
ifstream input;
input.open("HW3Data.dat"); //Opening file in read mode
Employee *last, *item;
head->next = NULL; //head has no data. head->next points to first node.
while(getline(input, line)) //Reading file line by line
{
item = new Employee;
temp = &line[0u]; //Converting from string type to C-String type
word = strtok(temp, " "); //Splitting string for first word
item->fname.assign(word, strlen(word)); //Copying first name (c-string type) to
item->fname(string type)
word = strtok(NULL, " "); //Splitting for next word
item->lname.assign(word, strlen(word)); //Copying last name (c-string type) to
item->lname(string type)
word = strtok(NULL, " "); //Splitting for next word
item->salary = atof(word); //Converting string to double
item->next = NULL;
if(head->next == NULL){ //If item is the first element
head->next = item; //Add item after head.
last = item; //last points to last item of list
}
else{ //If item is not the first node
last->next = item; //Add item after last node
last = item; //Now item is the last node
}
}
input.close(); //Close input file
}
void insert_item(Employee *head, string firstName, Employee *item)
{
Employee *ptr;
ptr = head->next; //ptr points to first node
while(ptr != NULL) //If ptr still points to some node i.e., list has not finished yet
{
if(ptr->fname == firstName) //If firstname = ptr->fname
{
//Inserting item after ptr
item->next = ptr->next; //item->next points to ptr->next
ptr->next = item; //ptr->next points to item. If we write this line at first,
// then we would have lost the rest of the existing nodes in
//the list after ptr.
return;
}
ptr = ptr->next; //Point ptr to next node
}
}
void delete_item(Employee *head, string firstName)
{
Employee *ptr, *last;
last = head; //last holds the last node just traversed
ptr = head->next; //ptr points to first node
while(ptr != NULL)
{
if(ptr->fname == firstName){ //If first name matches
last->next = ptr->next; //removing ptr from list
ptr->next = NULL; //removing link from ptr to list
delete ptr; //Releasing memory
return;
}
last = ptr; //last points to just traversed node
ptr = ptr->next; //ptr moves to next
}
}

void show_list(Employee *head)


{
Employee *ptr = head->next;
cout<<setw(15)<<left<<"Last name"; //setw(15) pads with whitespaces to hold
constant length
cout<<setw(15)<<left<<"First name"; //left means text will be on left side
cout<<"Salary\n";
while(ptr != NULL){
cout<<setw(15)<<left<<ptr->lname;
cout<<setw(15)<<left<<ptr->fname;
cout<<ptr->salary<<endl;
ptr = ptr->next;
}
}

int main()
{
Employee *head=new Employee;
create_list(head);
Employee *item=new Employee;
item->fname="Robert";
item->lname="Johnson";
item->salary=75000.50;
insert_item(head,"Sheila",item);
delete_item(head, "Sheila");
show_list(head);
return 0;
}
b) In the init kernel module, create the list with 5
employee elements. Traverse the list and output to
kernel log buffer. Invoke the dmesg command to
verify whether the list is properly created.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/slab.h>

struct employee{
char *emp_name;
int emp_id;
int emp_salary;
struct list_head list;
}employee[5];

LIST_HEAD(employee_list);

int EmployeeList_init(void)
{
printk(KERN_INFO "Loading Module...\n");

struct employee *employ;

employ= kmalloc(sizeof(*person), GFP_KERNEL);


employ->emp_name = "Ahmed";
employ->emp_id = 1524;
employ->emp_salary = 12000;

INIT_LIST_HEAD(&employ->list);
list_add_tail(&employ->list, &employee_list);

employ= kmalloc(sizeof(*employ), GFP_KERNEL);


employ->emp_name = "Bobu";
employ->emp_id = 4321;
employ->emp_salary= 30000;

INIT_LIST_HEAD(&employ->list);
list_add_tail(&employ->list, &employee_list);

employ = kmalloc(sizeof(*employ), GFP_KERNEL);


employ->emp_name = "Mallory";
employ->id = 5132;
employ->emp_salary = 12000;

INIT_LIST_HEAD(&employ->list);
list_add_tail(&employ->list, &employee_list);

employ = kmalloc(sizeof(*employ), GFP_KERNEL);


employ->emp_name = "getch";
employ->id = 6132;
employ->emp_salary = 22000;

INIT_LIST_HEAD(&employ->list);
list_add_tail(&employ->list, &employee_list);

employ = kmalloc(sizeof(*employ), GFP_KERNEL);


employ->emp_name = "kumar";
employ->id = 1132;
employ->emp_salary = 32000;

INIT_LIST_HEAD(&employ->list);
list_add_tail(&employ->list, &employee_list);

printk(KERN_INFO "List Out of Order...\n");

struct employee *ptr;


list_for_each_entry(ptr, &employee_list, list)
{
printk(KERN_INFO "Employee : Name: %s, ID: %d, Salary: %d\n",
ptr->emp_name, ptr->id, ptr->emp_salary);
}

printk(KERN_INFO "Done...\n");

return 0;
}
void employeeList_exit(void)
{
printk(KERN_INFO "Removing\n");

struct employee *ptr, *nxt;


list_for_each_entry_safe(ptr, nxt, &employee_list, list)
{
printk(KERN_INFO "Deleting : Name: %s, ID: %d, Salary: %d\n",
ptr->emp_name, ptr->emp_id, ptr->emp_salary);
list_del(&ptr->list);
kfree(ptr);
}

printk(KERN_INFO "free\n");
}

module_init(employeeList_init);
module_exit(employeeList_exit);

MODULE_LICENSE("VP");
MODULE_DESCRIPTION("employee List ");

Output:
c) In the exit kernel module, delete the list. Send
message to kernel log buffer regarding deletion.
Invoke dmesg command to verify the list is properly
deleted.

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <asm/uaccess.h>MODULE_LICENSE(“GPL”);
MODULE_AUTHOR(“Robert W. Oliver II”);
MODULE_DESCRIPTION(“A simple example Linux module.”);
MODULE_VERSION(“0.01”);#define DEVICE_NAME “lkm_example”
#define EXAMPLE_MSG “Hello, World!\n”
#define MSG_BUFFER_LEN 15/* Prototypes for device functions */
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);static int
major_num;
static int device_open_count = 0;
static char msg_buffer[MSG_BUFFER_LEN];
static char *msg_ptr;/* This structure points to all of the device functions */
static struct file_operations file_ops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};/* When a process reads from our device, this gets called. */
static ssize_t device_read(struct file *flip, char *buffer, size_t len, loff_t *offset) {
int bytes_read = 0;
/* If we’re at the end, loop back to the beginning */
if (*msg_ptr == 0) {
msg_ptr = msg_buffer;
}
/* Put data in the buffer */
while (len && *msg_ptr) {
/* Buffer is in user data, not kernel, so you can’t just reference
* with a pointer. The function put_user handles this for us */
put_user(*(msg_ptr++), buffer++);
len--;
bytes_read++;
}
return bytes_read;
}/* Called when a process tries to write to our device */
static ssize_t device_write(struct file *flip, const char *buffer, size_t len, loff_t
*offset) {
/* This is a read-only device */
printk(KERN_ALERT “This operation is not supported.\n”);
return -EINVAL;
}/* Called when a process opens our device */
static int device_open(struct inode *inode, struct file *file) {
/* If device is open, return busy */
if (device_open_count) {
return -EBUSY;
}
device_open_count++;
try_module_get(THIS_MODULE);
return 0;
}/* Called when a process closes our device */
static int device_release(struct inode *inode, struct file *file) {
/* Decrement the open counter and usage count. Without this, the module would
not unload. */
device_open_count--;
module_put(THIS_MODULE);
return 0;
}static int __init lkm_example_init(void) {
/* Fill buffer with our message */
strncpy(msg_buffer, EXAMPLE_MSG, MSG_BUFFER_LEN);
/* Set the msg_ptr to the buffer */
msg_ptr = msg_buffer;
/* Try to register character device */
major_num = register_chrdev(0, “lkm_example”, &file_ops);
if (major_num < 0) {
printk(KERN_ALERT “Could not register device: %d\n”, major_num);
return major_num;
} else {
printk(KERN_INFO “lkm_example module loaded with device major number
%d\n”, major_num);
return 0;
}
}static void __exit lkm_example_exit(void) {
/* Remember — we have to clean up after ourselves. Unregister the character
device. */
unregister_chrdev(major_num, DEVICE_NAME);
printk(KERN_INFO “Goodbye, World!\n”);
}/* Register module functions */
module_init(lkm_example_init);
module_exit(lkm_example_exit);

d) Compile, load, list and remove the kernel module.


#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
static int fish_init(void){
printk(KERN_ALERT "loading kernel module."\n);
return 0;
}

static void fish_exit(void){


printk(KERN_ALERT "kernel module removed.");
}
module_init(fish_init);
module_exit(fish_exit);

screen shoot:

You might also like