0
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/slab.h>
struct birthday
{
char *name;
int nameLength;
char *gender;
int month;
int day;
int year;
struct list_head list;

};

/**
* The following defines and initializes a list_head object named birthday_list
*/
static LIST_HEAD(birthday_list);

_Bool compare_birthday(const struct birthday *b1, const struct birthday *b2){
return (b1->nameLength > b2->nameLength) && (b1->year < b2->year) && (b1->month < b2->month)
&& (b1->day < b2->day);
}

int simple_init(void)
{
int simple_init(void)
{
struct birthday *person;
struct birthday *head;
printk(KERN_INFO "Loading Module\n");

person = kmalloc(sizeof(*person), GFP_KERNEL);
person->name = "Marques";
person->nameLength = 7;
person->gender = "Male";
person->month = 1;
person->day = 2;
person->year= 2000;
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);
head = person;

person = kmalloc(sizeof(*person), GFP_KERNEL);
person->name = "Nate";
person->nameLength = 4;
person->gender = "Male";
person->month = 2;
person->day = 24;
person->year= 1999;
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);

person = kmalloc(sizeof(*person), GFP_KERNEL);
person->name = "Charlie";
person->nameLength = 7;
person->gender = "Female";
person->month = 1;
person->day = 17;
person->year= 1999;
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);

person = kmalloc(sizeof(*person), GFP_KERNEL);
person->name = "Serena";
person->nameLength = 6;
person->gender = "Female";
person->month = 6;
person->day = 1;
person->year= 2001;
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);

person = kmalloc(sizeof(*person), GFP_KERNEL);
person->name = "Elice";
person->nameLength = 5;
person->gender = "Female";
person->month = 8;
person->day = 9;
person->year= 1999;
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);


struct birthday *ptr;
list_for_each_entry(ptr, &birthday_list, list){
printk(KERN_INFO "Name: %s nameLength: %d Gender: %s Birthday: Month: %d Day: %d Year: %d \n", ptr->name, ptr->nameLength, ptr->gender, ptr->month, ptr->day, ptr->year);
}

//finding a female with the longest name

struct birthday *oldestfemale = head;
_Bool check;
list_for_each_entry(ptr, &birthday_list, list){
    check = compare_birthday(ptr, oldestfemale;
    if (check && ptr->gender == "Female")
        oldestfemale = ptr;
}

list_del(&oldestfemale->list);
kfree(oldestfemale);

    // Printing updated list
printk(KERN_INFO "Deleted the female with the longest name. Updated list is here:");    list_for_each_entry(ptr, &birthday_list, list){
    printk(KERN_INFO "Name: %s nameLength: %d Birthday: %d/%d/%d\n", ptr->name, ptr->nameLength, ptr->month, ptr->day, ptr->year);
}

   return 0;

}



void simple_exit(void) {

printk(KERN_INFO "Removing Module\n");
struct birthday *ptr, *next;
list_for_each_entry_safe(ptr,next,&birthday_list,list){
printk(KERN_INFO "Removing %s %d %s %d/%d/%d \n",  ptr->name, ptr->nameLength, ptr->gender, ptr->month, ptr->day, ptr->year);




}
}



module_init( simple_init );
module_exit( simple_exit );

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Kernel Data Structures");
MODULE_AUTHOR("SGG");

I have to implement a function that finds a female person with the longest name and is a female gender. I am new to c programming and am having issues finding a function (.sizeOf) or writing a function from scratch since I am using Linux Kernel. I put in bold my function I need to fix but I know it is incorrect. Any help would be appreciated. Here is an image of the part I am stuck on if my explanation does not make sense. I don't expect anyone to finish the code for me, just need help implementing the length of the variable: char *name.

Clarification of the assignment

notKash
  • 1
  • 1
  • 1
    You are writing a kernel module. That is, you are not writing a normal user program but rather code that runs inside the Linux kernel. Is that really what you want to do? – kaylum Jan 22 '21 at 02:58
  • @kaylum Homework – Tarik Jan 22 '21 at 03:06
  • 1
    @Tarik Maybe. But it is good to confirm. People can go down surprisingly wrong paths. – kaylum Jan 22 '21 at 03:08
  • @kaylum Yes I have to write the code in a linux kernel. Since I have to use a Makefile to compile my code – notKash Jan 22 '21 at 03:10
  • 5
    Makefile can also be used to build regular programs. So using a makefile does not imply kernel programming. – Tarik Jan 22 '21 at 03:15
  • Isn't `strlen` available? – Support Ukraine Jan 22 '21 at 03:17
  • To build a kernel module, you have to use [_should_ use] the kernel's system for makefiles for modules. It is different than just writing a standalone makefile as for an application. You are referencing `.sizeOf` from _your_ `struct`. This means that you have to provide a member in your `struct` (e.g.) `int (*sizeOf)(void);` But, the calling syntax looks more like `c++` because, in `c`, you'd probably want to pass a pointer to the struct as an argument. Or, do you _just_ want: `int sizeOf;` and `n1->name.sizeOf` without the `()`? – Craig Estey Jan 22 '21 at 03:21
  • 2
    Upon a second look, you can _not_ have a `.whatever` attached to a `char *` pointer in `c` [or even in `c++`]. What language did you draw that construct from? And, IMO, if you're having an issue with that, I'd write a few more _applications_ to really learn the rudiments of the C language _before_ you dive into _kernel_ programming, which is comparatively _hard_, especially for someone at your current level. – Craig Estey Jan 22 '21 at 03:26
  • @CraigEstey I found I can use a strlen function but I believe I have to initialize my variable "char *name[]" instead of char *name but I am having trouble implementing it. I know I should use some practice but I am required to use kernel programming for one of my classes. – notKash Jan 22 '21 at 04:08
  • If this is an assignment, then post the complete assignment. We can't know how best to assist your learning, if we don't have the complete assignment. – jwdonahue Jan 22 '21 at 05:49
  • Please take the [tour], read [ask] and the [open letter to students with homework problems](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems). – jwdonahue Jan 22 '21 at 05:52
  • Does this answer your question? [How do I properly compare strings in C?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c) – Tsyvarev Jan 22 '21 at 08:35

0 Answers0