6.下面的代码是 linux kernel的代码,请指出其中的错误来: struct student{ int id; char name[20]; struct list_head 1ist; }; static LIST_HEAD(student_list); static DEFINE MUTEX(student_mutex); static int add_student(struct student *stu) { struct student *new_stu = NULL; if(!stu) { printk(KERN_ ERR " Invalid argument\n"); return -EINVAL; } if (stu->id <= 0){ i printk(KERN_ ERR "Invalid id\n"); return -EINVAL; } if(!mutex_trylock(&student_mutex)){ printk(KERN_ INFO "Failed to lock mutex\n"); return-EBUSY; } new_stu = kmalloc(sizeof(struct student), GFP KERNEL); if(!new_ stu) { printk(KERN ERR "Failed to allocate memory\n"); return -ENOMEM; *new_stu= *stu; list_add_ tail(&new_stu->list, &student_list); printk(KERN_ INFO "Added student: id=%d, name-%s\n", stu->id, stu->name); mutex_unlock(&student_mutex); return 0; }
时间: 2023-11-27 11:54:42 浏览: 72
以下是一个简单的C语言学生成绩管理系统的示例代码:.docx
这段代码存在以下几个错误:
1. 在定义结构体 student 中,list_head 的名称中包含数字 1,请修改为合法的名称。
2. DEFINE MUTEX 应该改为 DEFINE_MUTEX。
3. if (stu->id <= 0){ i 应该改为 if (stu->id <= 0){
4. kmalloc 中 GFP KERNEL 应该改为 GFP_KERNEL。
修改后的代码如下:
```
struct student{
int id;
char name[20];
struct list_head list;
};
static LIST_HEAD(student_list);
static DEFINE_MUTEX(student_mutex);
static int add_student(struct student *stu) {
struct student *new_stu = NULL;
if (!stu) {
printk(KERN_ERR "Invalid argument\n");
return -EINVAL;
}
if (stu->id <= 0) {
printk(KERN_ERR "Invalid id\n");
return -EINVAL;
}
if (!mutex_trylock(&student_mutex)) {
printk(KERN_INFO "Failed to lock mutex\n");
return -EBUSY;
}
new_stu = kmalloc(sizeof(struct student), GFP_KERNEL);
if (!new_stu) {
printk(KERN_ERR "Failed to allocate memory\n");
return -ENOMEM;
}
*new_stu = *stu;
list_add_tail(&new_stu->list, &student_list);
printk(KERN_INFO "Added student: id=%d, name=%s\n", stu->id, stu->name);
mutex_unlock(&student_mutex);
return 0;
}
```
阅读全文