Using semaphore in scull - from ldd3

本文详细解析了scull虚拟字符设备的结构与初始化过程。重点介绍了scull_dev结构体成员的作用,包括量子集指针、量子大小等。此外,还阐述了互斥信号量在scull设备中的使用方式及初始化顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 */
};

 

    each virtual scull device uses a semaphore seperately.

 

semaphore has to be intialized before the other section of scull device initialization.

 

for (i = 0; i < scull_nr_devs; i++) {
scull_devices[i].quantum = scull_quantum;
scull_devices[i].qset = scull_qset;
init_MUTEX(&scull_devices[i].sem);
scull_setup_cdev(&scull_devices[i], i);
}

scull_write starts from the following:

 

if (down_interruptible(&dev->sem))
return -ERESTARTSYS;

and release semaphore as follow(whether it can successfully carry out its other task):

 

out:
up(&dev->sem);
return retval;

        if some mistake ocurrs during scull_write operating, use : "goto out" to release semaphore correctly.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值