当前位置: 技术问答>linux和unix
linux设备驱动中的信号量机制
来源: 互联网 发布时间:2016-03-24
本文导语: 《linux设备驱动程序》3rd 简称ldd3,在第三章高级字符设备驱动中介绍了信号量机制后,我查看了ldd3附带的源代码,对其中某些部分不理解,请教各位: 在scull.h中定义结构的,如下 /* struct ...
《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里的需要检测的信号量?
在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 */
...
我觉得你看书不是很仔细,理解的关键点在于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 */
...