到cdev这个结构体这里:
static int __init freg_init(void) { int err; printk(KERN_ALERT"Initializing freg device.\n"); cdev_init(&freg_cdev, &freg_ops); //initialize the device instance ndev = MKDEV(freg_major, freg_minor); err = register_chrdev_region(ndev, 1, "freg");//allocate the device node number dynamically if(err < 0) return err; printk("freg_init():major=%d, minor=%d\n", MAJOR(ndev), MINOR(ndev)); freg_cdev.owner = THIS_MODULE; freg_cdev.ops = &freg_ops; err = cdev_add(&freg_cdev, ndev, 1);//register the char_dev into the system if(err < 0) return err; printk(KERN_ALERT"Succedded to initialize freg device.\n"); return 0; }
而当没有
freg_cdev.owner = THIS_MODULE;
freg_cdev.ops = &freg_ops;
这两行的时候,程序也是无误的运行,这里实践上的,还有理论上的cdev.owner和cdev.ops之类的和fops中是不是一样的 还有作用吗?
cdev_init(&freg_cdev, &freg_ops); //initialize the device instance
有这一句,后面那个
freg_cdev.ops = &freg_ops;
确实是多余的
因为cdev_init会对ops赋值。
看源码确实如此,这里。
cdev->ops = fops已经赋值了,不过freg_cdev.owner = THIS_MODULE没有赋值,要自己赋值。