linux信号量驱动程序,linux设备驱动中的信号量机制

《linux设备驱动程序》3rd 简称ldd3,在第三章高级字符设备驱动中介绍了信号量机制后,我查看了ldd3附带的源代码,对其中某些部分不理解,请教各位:

在scull.h中定义结构的,如下

/*

struct scull_dev {

struct scull_qset *data;  /* Pointer to first quantum set */

int quantum;              /* the current quantum size */

int qset;                /* the current array size */

unsigned long size;      /* amount of data stored here */

unsigned int access_key;  /* used by sculluid and scullpriv */

struct semaphore sem;    /* mutual exclusion semaphore    */  这里定义的结构中包含了信号量

struct cdev cdev;   /* Char device structure */

};

在main.c中定义了全局变量

struct scull_dev *scull_devices; /* allocated in scull_init_module */

/*

在初始化函数scull_init_module里面初始化信号量:scull_devices->sem;

但是在sucll_open函数里面,又重新定义了一个scull_dev指针:

struct scull_dev *dev; /* device information */

并有以下语句:

dev = container_of(inode->i_cdev, struct scull_dev, cdev);

filp->private_data = dev; /* for other methods */

还检测信号量:

if (down_interruptible(&dev->sem))

return -ERESTARTSYS;

问:

1.在初始化函数scull_init_module里面初始化的信号量和在打开函数:scull_open里面检测的信号量是不是同一个?如果不是,又为什么在

scull_init_module里面初始化不是属于scull_open里的需要检测的信号量?

|

是的。

我觉得你看书不是很仔细,理解的关键点在于container_of

其实在LDD3里也有说到:

3.5 open and release

...

int (*open)(struct inode *inode, struct file *filp);

The inode argument has the information we need in the form of its i_cdev field, which contains the cdev structure we set up before. The only problem is that we do not normally want the cdev structure itself, we want the scull_dev structure that contains that cdev structure. The C language lets programmers play all sorts of tricks to make that kind of conversion; programming such tricks is error prone, however, and leads to code that is difficult for others to read and understand. Fortunately, in this case, the kernel hackers have done the tricky stuff for us, in the form of the container_of macro, defined in :

container_of(pointer, container_type, container_field);

This macro takes a pointer to a field of type container_field, within a structure of type container_type, and returns a pointer to the containing structure. In scull_open, this macro is used to find the appropriate device structure:

struct scull_dev *dev; /* device information */

...

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值