cdev动态和静态注册的理解错误

先写下LDD3P59页字符设备的注册一节的原文:

我们前面提到,内核内部使用struct cdev结构来表示字符设备。。。

分配和初始化上述结构的方式有两种。如果读者打算在运行时获取一个独立的cdev结构,则应该如下编写代码:

struct cdev *my_cdev = cdev_alloc();

my_cdev->ops = &my_fops;

这时,你可以将cdev结构嵌入到自己的设备待定结构中,scull就是这样做的。这种情况下,我们需要用下面的代码来初始化已分配到的结果:

void cdev_init(struct cdev *cdev, struct file_operations *fops);

。。。

上面是部分原文,主要是”这时“让我产生了误解,以为是上面申请好后,下面进行了初始化。

查看了英文的原文:(多copy了两段)

There are two ways of allocating and initializing one of these structures. If you wish to obtain a standalone cdev structure at runtime, you may do so with code such as:
struct cdev *my_cdev = cdev_alloc( );
my_cdev->ops = &my_fops;

Chances are, however, that you will want to embed the cdev structure within a device-specific structure of your own; that is what scull does. In that case, you

should initialize the structure that you have already allocated with: 

void cdev_init(struct cdev *cdev, struct file_operations *fops);

Either way, there is one other struct cdev field that you need to initialize. Like the file_operations structure, struct cdev has an owner field that should be set to 

THIS_MODULE.

Once the cdev structure is set up, the final step is to tell the kernel about it with a call to: 

int cdev_add(struct cdev *dev, dev_t num, unsigned int count);

找到了有疑问的那一段,查了下解释:

chances are that:可能

initialize: vt  初始化

它的意思是指你可以在运行时动态的分配一个cdev结构,也可以把cdev结构内嵌到一个自己的设备结构中然而后者就需要使用cdev_init来初始化你已分配的结构。

所以

struct cdev *my_cdev = cdev_alloc();

my_cdev->ops = &my_fops;

void cdev_init(struct cdev *cdev, struct file_operations *fops);

是对cdev结构的两种初始化方式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值