目录
一、上下文和并发场合
二、中断屏蔽(了解)
三、原子变量(掌握)
四、自旋锁:基于忙等待的并发控制机制
五、信号量:基于阻塞的并发控制机制
六、互斥锁:基于阻塞的互斥机制
七、选择并发控制机制的原则
(在内核中实现并发控制机制)
一、上下文和并发场合
(多任务操作系统都会涉及到执行流。我之前学过ucos3感觉跟linux有点像,但是没linux全面复杂。)
执行流:有开始有结束总体顺序执行的一段代码 又称上下文
应用编程:任务上下文
内核编程:
1. 任务上下文:五状态 可阻塞
a. 应用进程或线程运行在用户空间
b. 应用进程或线程运行在内核空间(通过调用syscall来间接使用内核空间)
c. 内核线程始终在内核空间
2. 异常上下文:不可阻塞
中断上下文
竞态:多任务并行执行时,如果在一个时刻同时操作同一个资源,会引起资源的错乱,这种错乱情形被称为竞态
共享资源:可能会被多个任务同时使用的资源
临界区:操作共享资源的代码段
为了解决竞态,需要提供一种控制机制,来避免在同一时刻使用共享资源,这种机制被称为并发控制机制
并发控制机制分类:
1. 原子操作类(这个操作在执行时不可被打断,哪怕时间片到了也不行 )
2. 忙等待类(临界区前进行P操作,临界区后进行V操作,P操作为了检查你可不可以使用共享资源。如果不可以忙等待类就一直在循环。等待到资源可以被使用。而阻塞类是让P操作进入睡眠。可以使用了在醒来。)
3. 阻塞类
通用并发控制机制的一般使用套路:
```c
/*互斥问题:*/
(多个任务都想用这个资源。一个再用另一个等着)
并发控制机制初始化为可用
P操作
临界区
V操作
/*同步问题:*/
(A跑完了B跑)
//并发控制机制初始化为不可用
//先行方:
。。。。。
V操作
//后行方:
P操作
。。。。。
```
这个图来形容同步非常之贴切,来自百度
百度安全验证
二、中断屏蔽(了解)
一种同步机制的辅助手段
禁止本cpu中断 使能本cpu中断
local_irq_disable(); local_irq_enable();
local_irq_save(flags); local_irq_restore(flags); 与cpu的中断位相关
(比上面那组接口高级,可以保存中断屏蔽字,中断屏蔽字是来保存哪组中断响应那组被屏蔽)
local_bh_disable(); local_bh_enable(); 与中断低半部有关,关闭、打开软中断
禁止中断
临界区 //临界区代码不能占用太长时间,需要很快完成
//比如键盘进入临界区鼠标怎么点都好使 这种现象被称为系统假死
打开中断
适用场合:中断上下文与某任务共享资源时,或多个不同优先级的中断上下文间共享资源时
三、原子变量(掌握)
原子变量:存取不可被打断的特殊整型变量
a.设置原子量的值 void atomic_set(atomic_t *v,int i); //设置原子量的值为i atomic_t v = ATOMIC_INIT(0); //定义原子变量v并初始化为0 v = 10;//错误 b.获取原子量的值 atomic_read(atomic_t *v); //返回原子量的值 c.原子变量加减 void atomic_add(int i,atomic_t *v);//原子变量增加i void atomic_sub(int i,atomic_t *v);//原子变量减少i d.原子变量自增自减 void atomic_inc(atomic_t *v);//原子变量增加1 void atomic_dec(atomic_t *v);//原子变量减少1 e.操作并测试:运算后结果为0则返回真,否则返回假 int atomic_inc_and_test(atomic_t *v); int atomic_dec_and_test(atomic_t *v); int atomic_sub_and_test(int i,atomic_t *v); 原子位操作方法: a.设置位 void set_bit(nr, void *addr); //设置addr的第nr位为1 b.清除位 void clear_bit(nr , void *addr); //清除addr的第nr位为0 c.改变位 void change_bit(nr , void *addr); //改变addr的第nr位为1 d.测试位 void test_bit(nr , void *addr); //测试addr的第nr位是否为1
适用场合:共享资源为单个整型变量的互斥场合
第一个进程可以打开第二个失败
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include "mychar.h"
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
#include <signal.h>
int fd = -1;
int main(int argc, char *argv[])
{
int max = 0;
int cur = 0;
if(argc < 2)
{
printf("The argument is too few\n");
return 1;
}
fd = open(argv[1], O_RDONLY);
if(fd < 0)
{
printf("open %s failed\n", argv[1]);
return 2;
}
while(1)
{
}
close(fd);
fd = -1;
return 0;
}
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/atomic.h>
#include <linux/poll.h>
int major = 11;
int minor = 0;
int openonce_num = 1;
struct openonce_dev
{
struct cdev mydev;
/*Define atomic variables || 1 can open, 0 can not open*/
atomic_t openflag;
};
struct openonce_dev gmydev;
int openonce_open(struct inode *pnode, struct file *pfile)
{
struct openonce_dev *pmydev = NULL;
pfile->private_data = container_of(pnode->i_cdev, struct openonce_dev, mydev);
pmydev = (struct openonce_dev *)pfile->private_data;
if(atomic_dec_and_test(&pmydev->openflag))
{
printk("openonce_open\n");
return 0;
}
else
{
atomic_inc(&pmydev->openflag);
printk("The device is opened already\n");
return -1;
}
return 0;
}
int openonce_close(struct inode *pnode, struct file *pfile)
{
//printk("openonce_close\n");
/*C90 requires printk after the variable declaration*/
struct openonce_dev *pmydev = (struct openonce_dev *)pfile->private_data;
atomic_set(&pmydev->openflag,1);
return 0;
}
struct file_operations myops = {
.owner = THIS_MODULE,
.open = openonce_open,
.release = openonce_close,
};
int __init openonce_init(void)
{
int ret = 0;
dev_t devno = MKDEV(major,minor);
/*Apply for device number*/
ret = register_chrdev_region(devno, openonce_num, "openonce");
if(ret)
{
ret = alloc_chrdev_region(&devno, minor, openonce_num, "openonce");
if(ret)
{
printk("get devno failed\n");
return -1;
}
major = MAJOR(devno);//Easy to miss *****
}
/*Assign the 'struct cdev' a set of operation functions*/
cdev_init(&gmydev.mydev, &myops);
/*Add 'struct cdev' to the kernel's data structure*/
gmydev.mydev.owner = THIS_MODULE;
cdev_add(&gmydev.mydev, devno, openonce_num);//add to Hash.
/*initialize the atomic variable to 1*/
atomic_set(&gmydev.openflag,1);
return 0;
}
void __exit openonce_exit(void)
{
dev_t devno = MKDEV(major,minor);
cdev_del(&gmydev.mydev);
//printk("openonce will exit\n");
unregister_chrdev_region(devno, openonce_num);
}
MODULE_LICENSE("GPL");
module_init(openonce_init);
module_exit(openonce_exit);
四、自旋锁:基于忙等待的并发控制机制
获得就是P操作释放就是V操作
a.定义自旋锁
spinlock_t lock;
b.初始化自旋锁
spin_lock_init(spinlock_t *);
c.获得自旋锁
spin_lock(spinlock_t *); //成功获得自旋锁立即返回,否则自旋在那里直到该自旋锁的保持者释放
spin_trylock(spinlock_t *); //成功获得自旋锁立即返回真,否则返回假,而不是像上一个那样"在原地打转”
d.释放自旋锁
spin_unlock(spinlock_t *);
``` #include <linux/spinlock.h> 定义spinlock_t类型的变量lock spin_lock_init(&lock)后才能正常使用spinlock spin_lock(&lock); 临界区 spin_unlock(&lock); ```
异常上下文是不可阻塞的。任务上下文是可以阻塞的。只能解决互斥问题
适用场合:
1. 异常上下文之间或异常上下文与任务上下文之间共享资源时
2. 任务上下文之间且临界区执行时间很短时
3. 互斥问题
测试程序还是只有打开就是驱动换了一下
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/atomic.h>
#include <linux/poll.h>
int major = 11;
int minor = 0;
int openonce_num = 1;
struct openonce_dev
{
struct cdev mydev;
/*Define spinlock variables || 1 can open, 0 can not open*/
int openflag;
spinlock_t lock;
};
struct openonce_dev gmydev;
int openonce_open(struct inode *pnode, struct file *pfile)
{
struct openonce_dev *pmydev = NULL;
pfile->private_data = (void *)(container_of(pnode->i_cdev, struct openonce_dev, mydev));
pmydev = (struct openonce_dev *)pfile->private_data;
/**/
spin_lock(&pmydev->lock);
if(pmydev->openflag)
{
pmydev->openflag = 0;
spin_unlock(&pmydev->lock);
return 0;
}
else
{
spin_unlock(&pmydev->lock);
printk("The device is opened already\n");
return -1;
}
return 0;
}
int openonce_close(struct inode *pnode, struct file *pfile)
{
//printk("openonce_close\n");
/*C90 requires printk after the variable declaration*/
struct openonce_dev *pmydev = (struct openonce_dev *)pfile->private_data;
spin_lock(&pmydev->lock);
pmydev->openflag = 1;
spin_unlock(&pmydev->lock);
return 0;
}
struct file_operations myops = {
.owner = THIS_MODULE,
.open = openonce_open,
.release = openonce_close,
};
int __init openonce_init(void)
{
int ret = 0;
dev_t devno = MKDEV(major,minor);
/*Apply for device number*/
ret = register_chrdev_region(devno, openonce_num, "openonce");
if(ret)
{
ret = alloc_chrdev_region(&devno, minor, openonce_num, "openonce");
if(ret)
{
printk("get devno failed\n");
return -1;
}
major = MAJOR(devno);//Easy to miss *****
}
/*Assign the 'struct cdev' a set of operation functions*/
cdev_init(&gmydev.mydev, &myops);
/*Add 'struct cdev' to the kernel's data structure*/
gmydev.mydev.owner = THIS_MODULE;
cdev_add(&gmydev.mydev, devno, openonce_num);//add to Hash.
/*initialize the spinlock openflag to 1*/
gmydev.openflag = 1;
spin_lock_init(&gmydev.lock);
return 0;
}
void __exit openonce_exit(void)
{
dev_t devno = MKDEV(major,minor);
cdev_del(&gmydev.mydev);
//printk("openonce will exit\n");
unregister_chrdev_region(devno, openonce_num);
}
MODULE_LICENSE("GPL");
module_init(openonce_init);
module_exit(openonce_exit);
五、信号量:基于阻塞的并发控制机制
val:有多少资源可用
a.定义信号量 struct semaphore sem; b.初始化信号量 void sema_init(struct semaphore *sem, int val); c.获得信号量P int down(struct semaphore *sem);//深度睡眠 int down_interruptible(struct semaphore *sem);//浅度睡眠 d.释放信号量V void up(struct semaphore *sem);
这时候就可以多个进程访问一个设备了。
```
#include <linux/semaphore.h>
```
适用场合:任务上下文之间且临界区执行时间较长时的互斥或同步问题
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include "mychar.h"
#define BUF_LEN 100
int major = 11;
int minor = 0;
int mychar_num = 1;
struct mychar_dev
{
struct cdev mydev;
char mydev_buf[BUF_LEN];
int curlen;
struct semaphore sem;
/*Read wait queue and write wait queue*/
wait_queue_head_t rq;
wait_queue_head_t wq;
struct fasync_struct *pasync_obj;
};
struct mychar_dev gmydev;
int mychar_open(struct inode *pnode, struct file *pfile)
{
pfile->private_data = container_of(pnode->i_cdev, struct mychar_dev, mydev);
printk("mychar_open\n");
return 0;
}
int mychar_close(struct inode *pnode, struct file *pfile)
{
//printk("mychar_close\n");
/*C90 requires printk after the variable declaration*/
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
if(pmydev->pasync_obj != NULL)
fasync_helper(-1,pfile,0, &pmydev->pasync_obj);
return 0;
}
ssize_t mychar_read(struct file *pfile, char __user *puser, size_t count, loff_t *p_pos)
{
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
int size = 0;
int ret = 0;
down(&pmydev->sem);
if(pmydev->curlen <= 0)
{
if(pfile->f_flags & O_NONBLOCK)
{//non-blocking
up(&pmydev->sem);
printk("O_NONBLOCK No Data Read\n");
return -1;
}
else
{//blocking
up(&pmydev->sem);
ret = wait_event_interruptible(pmydev->rq,pmydev->curlen > 0);
if(ret)
{
printk("Wake up by signal\n");
return -ERESTARTSYS;
}
down(&pmydev->sem);
}
}
if(count > pmydev->curlen)
{
size = pmydev->curlen;
}
else
{
size = count;
}
ret = copy_to_user(puser, pmydev->mydev_buf, size);
if(ret)
{
up(&pmydev->sem);
printk("copy_to_user failed\n");
return -1;
}
memcpy(pmydev->mydev_buf, pmydev->mydev_buf + size, pmydev->curlen - size);
pmydev->curlen = pmydev->curlen - size;
up(&pmydev->sem);
/*Wake up interrupt*/
wake_up_interruptible(&pmydev->wq);
return size;
}
ssize_t mychar_write(struct file *pfile, const char __user *puser, size_t count, loff_t *p_pos)
{
int size = 0;
int ret = 0;
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
down(&pmydev->sem);
if(pmydev->curlen >= BUF_LEN)
{
if(pfile->f_flags & O_NONBLOCK)
{
up(&pmydev->sem);
printk("O_NONBLOCK can not write\n");
return -1;
}
else
{
up(&pmydev->sem);
ret = wait_event_interruptible(pmydev->wq,
pmydev->curlen < BUF_LEN);
if(ret)
{
printk("wake up by signal\n");
return -ERESTARTSYS;
}
down(&pmydev->sem);
}
}
if(count > BUF_LEN - pmydev->curlen)
{
size = BUF_LEN - pmydev->curlen;
}
else
{
size = count;
}
ret = copy_from_user(pmydev->mydev_buf + pmydev->curlen, puser, size);
if(ret)
{
up(&pmydev->sem);
printk("copy_from_user failed\n");
return -1;
}
pmydev->curlen += size;
up(&pmydev->sem);
/*Wake up interrupt*/
wake_up_interruptible(&pmydev->rq);
if(pmydev->pasync_obj != NULL)
{
kill_fasync(&pmydev->pasync_obj, SIGIO, POLL_IN);
}
return size;
}
long mychar_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
{
int __user *pret = (int *)arg;
int maxlen = BUF_LEN;
int ret = 0;
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
switch(cmd)
{
case MYCHAR_IOCTL_GET_MAXLEN:
ret = copy_to_user(pret, &maxlen, sizeof(int));
if(ret)
{
printk("copy_to_user MAXLEN failed\n");
return -1;
}
break;
case MYCHAR_IOCTL_GET_CURLEN:
down(&pmydev->sem);
ret = copy_to_user(pret, &pmydev->curlen, sizeof(int));
up(&pmydev->sem);
if(ret)
{
printk("copy_to_user MAXLEN failed\n");
return -1;
}
break;
default:
printk("The cmd is unknow\n");
return -1;
}
return 0;
}
unsigned int mychar_poll(struct file *pfile, poll_table *ptb)
{
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
unsigned int mask = 0;
/*It not block. Adds the wait queue to the table*/
poll_wait(pfile,&pmydev->rq,ptb);
poll_wait(pfile,&pmydev->wq,ptb);
down(&pmydev->sem);
if(pmydev->curlen > 0)
{
mask |= POLLIN | POLLRDNORM;
}
if(pmydev->curlen < BUF_LEN)
{
mask |= POLLOUT | POLLWRNORM;
}
up(&pmydev->sem);
return mask;
}
int mychar_fasync(int fd,struct file *pfile, int mode)
{
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
return fasync_helper(fd, pfile, mode, &pmydev->pasync_obj);
}
struct file_operations myops = {
.owner = THIS_MODULE,
.open = mychar_open,
.release = mychar_close,
.read = mychar_read,
.write = mychar_write,
.unlocked_ioctl = mychar_ioctl,
.poll = mychar_poll,
.fasync = mychar_fasync,
};
int __init mychar_init(void)
{
int ret = 0;
dev_t devno = MKDEV(major,minor);
/*Apply for device number*/
ret = register_chrdev_region(devno, mychar_num, "mychar");
if(ret)
{
ret = alloc_chrdev_region(&devno, minor, mychar_num, "mychar");
if(ret)
{
printk("get devno failed\n");
return -1;
}
printk("copy_to_user failed\n");
major = MAJOR(devno);//Easy to miss *****
}
/*Assign the 'struct cdev' a set of operation functions*/
cdev_init(&gmydev.mydev, &myops);
/*Add 'struct cdev' to the kernel's data structure*/
gmydev.mydev.owner = THIS_MODULE;
cdev_add(&gmydev.mydev, devno, mychar_num);//add to Hash.
/*initialize the wait queue header*/
init_waitqueue_head(&gmydev.rq);
init_waitqueue_head(&gmydev.wq);
sema_init(&gmydev.sem, 1);
return 0;
}
void __exit mychar_exit(void)
{
dev_t devno = MKDEV(major,minor);
cdev_del(&gmydev.mydev);
//printk("mychar will exit\n");
unregister_chrdev_region(devno, mychar_num);
}
MODULE_LICENSE("GPL");
module_init(mychar_init);
module_exit(mychar_exit);
六、互斥锁:基于阻塞的互斥机制
a.初始化
struct mutex my_mutex;
mutex_init(&my_mutex);
b.获取互斥体
void mutex_lock(struct mutex *lock);
c.释放互斥体
void mutex_unlock(struct mutex *lock);
1. 定义对应类型的变量
2. 初始化对应变量
P/加锁
临界区
V/解锁
```
#include <linux/mutex.h>
```
适用场合:任务上下文之间且临界区执行时间较长时的互斥问题
使用互斥锁时就只能一个进程访问
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include "mychar.h"
#define BUF_LEN 100
int major = 11;
int minor = 0;
int mychar_num = 1;
struct mychar_dev
{
struct cdev mydev;
char mydev_buf[BUF_LEN];
int curlen;
struct mutex lock;
/*Read wait queue and write wait queue*/
wait_queue_head_t rq;
wait_queue_head_t wq;
struct fasync_struct *pasync_obj;
};
struct mychar_dev gmydev;
int mychar_open(struct inode *pnode, struct file *pfile)
{
pfile->private_data = container_of(pnode->i_cdev, struct mychar_dev, mydev);
printk("mychar_open\n");
return 0;
}
int mychar_close(struct inode *pnode, struct file *pfile)
{
//printk("mychar_close\n");
/*C90 requires printk after the variable declaration*/
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
if(pmydev->pasync_obj != NULL)
fasync_helper(-1,pfile,0, &pmydev->pasync_obj);
return 0;
}
ssize_t mychar_read(struct file *pfile, char __user *puser, size_t count, loff_t *p_pos)
{
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
int size = 0;
int ret = 0;
mutex_lock(&pmydev->lock);
if(pmydev->curlen <= 0)
{
if(pfile->f_flags & O_NONBLOCK)
{//non-blocking
mutex_unlock(&pmydev->lock);
printk("O_NONBLOCK No Data Read\n");
return -1;
}
else
{//blocking
mutex_unlock(&pmydev->lock);
ret = wait_event_interruptible(pmydev->rq,pmydev->curlen > 0);
if(ret)
{
printk("Wake up by signal\n");
return -ERESTARTSYS;
}
mutex_lock(&pmydev->lock);
}
}
if(count > pmydev->curlen)
{
size = pmydev->curlen;
}
else
{
size = count;
}
ret = copy_to_user(puser, pmydev->mydev_buf, size);
if(ret)
{
mutex_unlock(&pmydev->lock);
printk("copy_to_user failed\n");
return -1;
}
memcpy(pmydev->mydev_buf, pmydev->mydev_buf + size, pmydev->curlen - size);
pmydev->curlen = pmydev->curlen - size;
mutex_unlock(&pmydev->lock);
/*Wake up interrupt*/
wake_up_interruptible(&pmydev->wq);
return size;
}
ssize_t mychar_write(struct file *pfile, const char __user *puser, size_t count, loff_t *p_pos)
{
int size = 0;
int ret = 0;
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
mutex_lock(&pmydev->lock);
if(pmydev->curlen >= BUF_LEN)
{
if(pfile->f_flags & O_NONBLOCK)
{
mutex_unlock(&pmydev->lock);
printk("O_NONBLOCK can not write\n");
return -1;
}
else
{
mutex_unlock(&pmydev->lock);
ret = wait_event_interruptible(pmydev->wq,
pmydev->curlen < BUF_LEN);
if(ret)
{
printk("wake up by signal\n");
return -ERESTARTSYS;
}
mutex_lock(&pmydev->lock);
}
}
if(count > BUF_LEN - pmydev->curlen)
{
size = BUF_LEN - pmydev->curlen;
}
else
{
size = count;
}
ret = copy_from_user(pmydev->mydev_buf + pmydev->curlen, puser, size);
if(ret)
{
mutex_unlock(&pmydev->lock);
printk("copy_from_user failed\n");
return -1;
}
pmydev->curlen += size;
mutex_unlock(&pmydev->lock);
/*Wake up interrupt*/
wake_up_interruptible(&pmydev->rq);
if(pmydev->pasync_obj != NULL)
{
kill_fasync(&pmydev->pasync_obj, SIGIO, POLL_IN);
}
return size;
}
long mychar_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
{
int __user *pret = (int *)arg;
int maxlen = BUF_LEN;
int ret = 0;
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
switch(cmd)
{
case MYCHAR_IOCTL_GET_MAXLEN:
ret = copy_to_user(pret, &maxlen, sizeof(int));
if(ret)
{
printk("copy_to_user MAXLEN failed\n");
return -1;
}
break;
case MYCHAR_IOCTL_GET_CURLEN:
mutex_lock(&pmydev->lock);
ret = copy_to_user(pret, &pmydev->curlen, sizeof(int));
mutex_unlock(&pmydev->lock);
if(ret)
{
printk("copy_to_user MAXLEN failed\n");
return -1;
}
break;
default:
printk("The cmd is unknow\n");
return -1;
}
return 0;
}
unsigned int mychar_poll(struct file *pfile, poll_table *ptb)
{
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
unsigned int mask = 0;
/*It not block. Adds the wait queue to the table*/
poll_wait(pfile,&pmydev->rq,ptb);
poll_wait(pfile,&pmydev->wq,ptb);
mutex_lock(&pmydev->lock);
if(pmydev->curlen > 0)
{
mask |= POLLIN | POLLRDNORM;
}
if(pmydev->curlen < BUF_LEN)
{
mask |= POLLOUT | POLLWRNORM;
}
mutex_unlock(&pmydev->lock);
return mask;
}
int mychar_fasync(int fd,struct file *pfile, int mode)
{
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
return fasync_helper(fd, pfile, mode, &pmydev->pasync_obj);
}
struct file_operations myops = {
.owner = THIS_MODULE,
.open = mychar_open,
.release = mychar_close,
.read = mychar_read,
.write = mychar_write,
.unlocked_ioctl = mychar_ioctl,
.poll = mychar_poll,
.fasync = mychar_fasync,
};
int __init mychar_init(void)
{
int ret = 0;
dev_t devno = MKDEV(major,minor);
/*Apply for device number*/
ret = register_chrdev_region(devno, mychar_num, "mychar");
if(ret)
{
ret = alloc_chrdev_region(&devno, minor, mychar_num, "mychar");
if(ret)
{
printk("get devno failed\n");
return -1;
}
printk("copy_to_user failed\n");
major = MAJOR(devno);//Easy to miss *****
}
/*Assign the 'struct cdev' a set of operation functions*/
cdev_init(&gmydev.mydev, &myops);
/*Add 'struct cdev' to the kernel's data structure*/
gmydev.mydev.owner = THIS_MODULE;
cdev_add(&gmydev.mydev, devno, mychar_num);//add to Hash.
/*initialize the wait queue header*/
init_waitqueue_head(&gmydev.rq);
init_waitqueue_head(&gmydev.wq);
mutex_init(&gmydev.lock);
return 0;
}
void __exit mychar_exit(void)
{
dev_t devno = MKDEV(major,minor);
cdev_del(&gmydev.mydev);
//printk("mychar will exit\n");
unregister_chrdev_region(devno, mychar_num);
}
MODULE_LICENSE("GPL");
module_init(mychar_init);
module_exit(mychar_exit);
七、选择并发控制机制的原则
1. 不允许睡眠的上下文需要采用忙等待类,可以睡眠的上下文可以采用阻塞类。在异常上下文中访问的竞争资源一定采用忙等待类。
2. 临界区操作较长的应用建议采用阻塞类,临界区很短的操作建议采用忙等待类。
3. 中断屏蔽仅在有与中断上下文共享资源时使用。
4. 共享资源仅是一个简单整型量时用原子变量