1 信号量:基于阻塞的并发控制机制
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>
注意:pv操作成对出现
1.1 示例
新建信号量,在init中初始化,read、write、poll、ioctl函数中添加pv操作
mychar_sema.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include "mychar.h"
#define BUF_LEN 100
#define MYCHAR_DEV_CNT 3
int major = 11;
int minor = 0;
int mychar_num = MYCHAR_DEV_CNT;
//新建结构体类型
struct mychar_dev
{
struct cdev mydev;
char mydef_buf[BUF_LEN]; //相当于结构体的私有变量
int curlen; //相当于结构体的私有变量
struct semaphore sem; //定义信号量
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)
{
//利用private_data私有变量来指向全局变量结构体地址
pfile->private_data = (void*)(container_of(pnode->i_cdev,struct mychar_dev,mydev));
printk("mychar_open is called\n");
return 0;
}
int mychar_close(struct inode *pnode, struct file *pfile)
{
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
printk("mychar_close is called\n");
if(pmydev->pasync_obj != NULL)
{
fasync_helper(-1,pfile,0,&pmydev->pasync_obj); //则调用fasync_helper函数来取消异步通知
}
return 0;
}
ssize_t mychar_read(struct file *filp, char __user *pbuf, size_t count, loff_t *ppos)
{
int ret = 0;
int size = 0;
//获取全家变量结构体地址
struct mychar_dev *pmydev = (struct mychar_dev *)filp->private_data;
down(&pmydev->sem);
if(pmydev->curlen <= 0)
{
if(filp->f_flags & O_NONBLOCK)
{//非阻塞
up(&pmydev->sem);
printk("O_NONBLOCK No Data Read\n");
return -1;
}
else
{//阻塞
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(pbuf,pmydev->mydef_buf,size);
if(ret)
{
up(&pmydev->sem);
printk("copy_to_user failed\n");
return -1;
}
//读完之后把后面的内容再拷贝过来,同时更新curlen
memcpy(pmydev->mydef_buf,pmydev->mydef_buf+size,pmydev->curlen - size);
pmydev->curlen = pmydev->curlen - size;
up(&pmydev->sem);
wake_up_interruptible(&pmydev->wq);
return size;
}
ssize_t mychar_write (struct file *filp, const char __user *pbuf, size_t count, loff_t *ppos)
{
int size = 0;
int ret = 0;
//获取全家变量结构体地址
struct mychar_dev *pmydev = (struct mychar_dev *)filp->private_data;
down(&pmydev->sem);
if(pmydev->curlen >= BUF_LEN)
{
if(filp->f_flags & O_NONBLOCK)
{
up(&pmydev->sem);
printk("O_NONBLOCK Can not write data\n");
return -1;
}
else
{
up(&pmydev->sem); //不能让write函数拿着资源睡眠,否则其他无法拿到资源了
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->mydef_buf + pmydev->curlen, pbuf, size);
if(ret)
{
up(&pmydev->sem);
printk("copy_from_user failed\n");
return -1;
}
//更新curlen
pmydev->curlen = pmydev->curlen + size;
up(&pmydev->sem);
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 *filp, 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 *)filp->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 CURLEN failed\n");
return -1;
}
break;
default:
printk("The cmd is unknow\n");
return -1;
}
return 0;
}
/*该函数与select、poll、epoll_wait函数相对应,协助这些多路监控函数判断本设备是否有数据可读写*/
unsigned int mychar_poll(struct file *filp, poll_table *ptb)
{
struct mychar_dev *pmydev = (struct mychar_dev *)filp->private_data;
unsigned int mask = 0;
//将等待队列头添加至poll_table表中
poll_wait(filp, &pmydev->rq,ptb);
poll_wait(filp, &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;
}
//使用pmydev->pasync_obj作为异步通知对象进行注册
int mychar_fasync(int fd,struct file *filp,int mode)
{
struct mychar_dev *pmydev = (struct mychar_dev *)filp->private_data;
return fasync_helper(fd,filp,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 mychar_init(void)
{
int ret = 0;
dev_t devno = MKDEV(major, minor);
/* 申请设备号 */
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;
}
major = MAJOR(devno); // 容易遗漏,注意
}
/* 给struct cdev对象指定操作函数集 */
cdev_init(&gmydev.mydev, &myops);
/* 将 struct cdev对象添加到内核对应的数据结构里 */
gmydev.mydev.owner = THIS_MODULE;
cdev_add(&gmydev.mydev, devno, 1);
//初始化队列
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);
unregister_chrdev_region(devno, mychar_num);
}
//表示支持GPL的开源协议
MODULE_LICENSE("GPL");
module_init(mychar_init);
module_exit(mychar_exit);
Makefile
ifeq ($(KERNELRELEASE),)
ifeq ($(ARCH),arm)
KERNELDIR ?= /home/linux/Linux_4412/kernel/linux-3.14
ROOTFS ?= /opt/4412/rootfs
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
endif
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules INSTALL_MOD_PATH=$(ROOTFS) modules_install
clean:
rm -rf *.o *.ko .*.cmd *.mod.* modules.order Module.symvers .tmp_versions
else
CONFIG_MODULE_SIG=n
obj-m += mychar.o
obj-m += mychar_poll.o
obj-m += openonce_atomic.o
obj-m += openonce_spinlock.o
obj-m += mychar_sema.o
endif
testmychar_signal.c
测试函数不需要进行大的修改,直接拿以前的来测试,主要目的可以让函数打开字符设备的时候可以实现并发,不会出现竞态
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/select.h>
#include <errno.h>
#include <signal.h>
#include "mychar.h"
#include <stdio.h>
int fd = -1;
void sigio_handler(int signo);
int main(int argc,char *argv[])
{
int flg = 0;
if(argc < 2)
{
printf("The argument is too few\n");
return 1;
}
signal(SIGIO,sigio_handler);
fd = open(argv[1],O_RDWR);
if(fd < 0)
{
printf("open %s failed\n",argv[1]);
return 2;
}
//将文件描述符fd设置为异步通知模式,并将当前进程的PID设置为接收异步通知的进程
fcntl(fd,F_SETOWN,getpid()); //数将当前进程的PID设置为fd的拥有者
flg = fcntl(fd,F_GETFL); //取fd的标志位
flg |= FASYNC; //然后使用按位或运算符将FASYNC标志位添加到标志中
fcntl(fd,F_SETFL,flg); //将flg(即带有FASYNC标志位的标志)设置为fd的标志位,从而使fd进入异步通知模式
while(1)
{
}
close(fd);
fd = -1;
return 0;
}
void sigio_handler(int signo)
{
char buf[8] = "";
read(fd,buf,8);
printf("buf=%s\n",buf);
}
编译
插入内核模块
测试并发效果
2 互斥锁:基于阻塞的互斥机制
可以理解为特殊的信号量,只能解决单个资源控制。
a.初始化
struct mutex my_mutex;
mutex_init(&my_mutex);
b.获取互斥体
void mutex_lock(struct mutex *lock);
c.释放互斥体
void mutex_unlock(struct mutex *lock);
操作步骤:
定义对应类型的变量
初始化对应变量
P/加锁
临界区
V/解锁
#include <linux/mutex.h>
适用场合:任务上下文之间且临界区执行时间较长时的互斥问题。
1.2 示例
mychar_mutex.c
新建信号量,在init中初始化,read、write、poll、ioctl函数中添加pv操作
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include "mychar.h"
#define BUF_LEN 100
#define MYCHAR_DEV_CNT 3
int major = 11;
int minor = 0;
int mychar_num = MYCHAR_DEV_CNT;
//新建结构体类型
struct mychar_dev
{
struct cdev mydev;
char mydef_buf[BUF_LEN]; //相当于结构体的私有变量
int curlen; //相当于结构体的私有变量
struct mutex lock; //定义互斥锁
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)
{
//利用private_data私有变量来指向全局变量结构体地址
pfile->private_data = (void*)(container_of(pnode->i_cdev,struct mychar_dev,mydev));
printk("mychar_open is called\n");
return 0;
}
int mychar_close(struct inode *pnode, struct file *pfile)
{
struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
printk("mychar_close is called\n");
if(pmydev->pasync_obj != NULL)
{
fasync_helper(-1,pfile,0,&pmydev->pasync_obj); //则调用fasync_helper函数来取消异步通知
}
return 0;
}
ssize_t mychar_read(struct file *filp, char __user *pbuf, size_t count, loff_t *ppos)
{
int ret = 0;
int size = 0;
//获取全家变量结构体地址
struct mychar_dev *pmydev = (struct mychar_dev *)filp->private_data;
mutex_lock(&pmydev->lock);
if(pmydev->curlen <= 0)
{
if(filp->f_flags & O_NONBLOCK)
{//非阻塞
mutex_unlock(&pmydev->lock);
printk("O_NONBLOCK No Data Read\n");
return -1;
}
else
{//阻塞
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(pbuf,pmydev->mydef_buf,size);
if(ret)
{
mutex_unlock(&pmydev->lock);
printk("copy_to_user failed\n");
return -1;
}
//读完之后把后面的内容再拷贝过来,同时更新curlen
memcpy(pmydev->mydef_buf,pmydev->mydef_buf+size,pmydev->curlen - size);
pmydev->curlen = pmydev->curlen - size;
mutex_unlock(&pmydev->lock);
wake_up_interruptible(&pmydev->wq);
return size;
}
ssize_t mychar_write (struct file *filp, const char __user *pbuf, size_t count, loff_t *ppos)
{
int size = 0;
int ret = 0;
//获取全家变量结构体地址
struct mychar_dev *pmydev = (struct mychar_dev *)filp->private_data;
mutex_lock(&pmydev->lock);
if(pmydev->curlen >= BUF_LEN)
{
if(filp->f_flags & O_NONBLOCK)
{
mutex_unlock(&pmydev->lock);
printk("O_NONBLOCK Can not write data\n");
return -1;
}
else
{
mutex_unlock(&pmydev->lock); //不能让write函数拿着资源睡眠,否则其他无法拿到资源了
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->mydef_buf + pmydev->curlen, pbuf, size);
if(ret)
{
mutex_unlock(&pmydev->lock);
printk("copy_from_user failed\n");
return -1;
}
//更新curlen
pmydev->curlen = pmydev->curlen + size;
mutex_unlock(&pmydev->lock);
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 *filp, 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 *)filp->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 CURLEN failed\n");
return -1;
}
break;
default:
printk("The cmd is unknow\n");
return -1;
}
return 0;
}
/*该函数与select、poll、epoll_wait函数相对应,协助这些多路监控函数判断本设备是否有数据可读写*/
unsigned int mychar_poll(struct file *filp, poll_table *ptb)
{
struct mychar_dev *pmydev = (struct mychar_dev *)filp->private_data;
unsigned int mask = 0;
//将等待队列头添加至poll_table表中
poll_wait(filp, &pmydev->rq,ptb);
poll_wait(filp, &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;
}
//使用pmydev->pasync_obj作为异步通知对象进行注册
int mychar_fasync(int fd,struct file *filp,int mode)
{
struct mychar_dev *pmydev = (struct mychar_dev *)filp->private_data;
return fasync_helper(fd,filp,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 mychar_init(void)
{
int ret = 0;
dev_t devno = MKDEV(major, minor);
/* 申请设备号 */
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;
}
major = MAJOR(devno); // 容易遗漏,注意
}
/* 给struct cdev对象指定操作函数集 */
cdev_init(&gmydev.mydev, &myops);
/* 将 struct cdev对象添加到内核对应的数据结构里 */
gmydev.mydev.owner = THIS_MODULE;
cdev_add(&gmydev.mydev, devno, 1);
//初始化队列
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);
unregister_chrdev_region(devno, mychar_num);
}
//表示支持GPL的开源协议
MODULE_LICENSE("GPL");
module_init(mychar_init);
module_exit(mychar_exit);
Makefile
ifeq ($(KERNELRELEASE),)
ifeq ($(ARCH),arm)
KERNELDIR ?= /home/linux/Linux_4412/kernel/linux-3.14
ROOTFS ?= /opt/4412/rootfs
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
endif
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules INSTALL_MOD_PATH=$(ROOTFS) modules_install
clean:
rm -rf *.o *.ko .*.cmd *.mod.* modules.order Module.symvers .tmp_versions
else
CONFIG_MODULE_SIG=n
obj-m += mychar.o
obj-m += mychar_poll.o
obj-m += openonce_atomic.o
obj-m += openonce_spinlock.o
obj-m += mychar_sema.o
obj-m += mychar_mutex.o
endif
testmychar_signal.c(略)同1.1示例
编译,添加到内核模块,测试并发
补充:
生产者消费者问题是操作系统中的一个经典同步问题,用于描述多个线程之间的协作和资源竞争。该问题涉及到两种类型的线程:生产者和消费者。
生产者线程负责生成数据,并将其放入一个共享的缓冲区中。消费者线程从缓冲区中取出数据并进行处理。缓冲区有限,当缓冲区满时,生产者必须等待消费者取走数据;当缓冲区为空时,消费者必须等待生产者生成数据。
为了解决生产者消费者问题,可以使用以下机制:
- 互斥锁:用于确保在同一时间只有一个线程能够访问共享缓冲区。
- 条件变量:用于线程间的通信。当缓冲区满时,生产者线程将释放该条件变量,使消费者线程进入等待状态,直到缓冲区不再满;当缓冲区为空时,消费者线程将释放该条件变量,使生产者线程进入等待状态,直到缓冲区非空。
- 信号量:用于控制生产者和消费者线程的执行顺序和数量。可以使用两个计数信号量来表示缓冲区中的空槽和已占用槽的数量。
使用这些机制,可以编写一个并发程序来解决生产者消费者问题。在实现时需要注意避免线程死锁和饥饿的情况发生,确保线程能够正确地互相通信和协作。
以上是一般的解决方法,具体的实现细节可能会因操作系统和编程语言的不同而有所差异。
3 选择并发控制机制的原则
- 不允许睡眠的上下文需要采用忙等待类(spin_lock),可以睡眠的上下文可以采用阻塞类。在异常上下文中访问的竞争资源一定采用忙等待类。
- 临界区操作较长的应用建议采用阻塞类,临界区很短的操作建议采用忙等待类。
- 中断屏蔽仅在有与中断上下文共享资源时使用。
- 共享资源仅是一个简单整型量时用原子变量