信号量(semaphore)是用于保护临界区的一种常用方法,它的使用和自旋锁类似。与自旋锁相同,只有得到信号量的进程才能执行临界区代码。但是与自旋锁不同的是当获取不到信号量时,进程不会原地打转而是进入休眠等待状态。
关于信号量的操作函数:
1、定义信号量
struct semaphore sem;
2、初始化信号量
sema_init(struct semaphore *sem, int val);//用于初始化信号量并设置sem为val
init_MUTEX(struct semaphore *sem);//初始化为1
init_MUTEX_LOCKED();//用于初始化信号量,并将信号量sem的值设置为0.也就是在创建时就处于已锁定状态。
DECLARE_MUTEX(name);//定义一个名称为name的互斥信号量,并将信号量初始化为1
3、获取信号量
void down(struct semaphore *sem)//获取信号量,可能导致睡眠,不能再中断上下文中使用该函数
int down_interruptible(struct semaphore *sem)//获取信号量,如果信号量不可用进程将设置为TASK_INTERRUPTIBLE类型的睡眠状态。该函数返回值来区分正常返回还是被信号中断返回。如果返回值为0代表获取信号量正常返回,返回非0代表被信号打断。
int down_killable(struct semaphore *sem)//获取信号量,如果信号不可用,进程将被设置为TASK_KELLABLE类型的睡眠状态。
int down_trylock(struct semaphore *sem)//该后双女户尝试获取信号量sem。如果能够获得立即获得,它就获得信号量并返回。否则,返回非0值。它不会导致调用者睡眠,可以在中断上下文中使用。
4、释放信号量
void up(struct semaphore *sem)//该函数释放信号量sem。实质上是把sem的值加1,如果sem的值为非正数,表明有任务等待该信号量,因此需要唤醒等待者。
信号量用于同步
如果信号量被初始化为0,则它可以用于同步,同步意味着一个执行单元的继续执行需等待另一个执行单元完成某事,保证执行的先后顺序。
/** *Copyright (c) 2013.TianYuan *All rights reserved. * *文件名称: char_device_driver12.c *文件标识: 信号量 * *当前版本:1.0 *作者:wuyq * *取代版本:xxx *原作者:xxx *完成日期:2013-11-29 */ #include <linux/init.h> #include <linux/module.h> #include <linux/fs.h> #include <linux/cdev.h> #include <linux/device.h> #include <linux/slab.h> #include <asm/uaccess.h> #include <asm/gpio.h> #include <plat/gpio-cfg.h> #include <linux/spinlock_types.h> MODULE_LICENSE("GPL"); #define CDD_MAJOR 200//cat /proc/devices找一个尚未使用的 #define CDD_MINOR 0 #define CDD_COUNT 1//一个cdev控制两个led dev_t dev = 0; u32 cdd_major = 0; u32 cdd_minor = 0; struct class *dev_class = NULL; #define BUF_SIZE 100 struct cdd_cdev{ struct cdev cdev; struct device *dev_device; u8 led; char kbuf[BUF_SIZE]; u32 data_len;//记录缓冲区中已经写入数据的长度 //定义一个信号量 struct semaphore sem_open; }; struct cdd_cdev *cdd_cdevp = NULL; unsigned long led_gpio_table[2] = { S5PV210_GPC1(3),//数字 S5PV210_GPC1(4), }; int cdd_open(struct inode* inode, struct file *filp) { struct cdd_cdev *pcdevp = NULL; printk("enter cdd_open!\n"); pcdevp = container_of(inode->i_cdev, struct cdd_cdev, cdev); printk("led = %d\n", pcdevp->led); /*获取信号量*/ //down获取信号量不成功,会导致进程睡眠(第3个进程的时候) //down(&pcdevp->sem_open); if(down_interruptible(&pcdevp->sem_open)<0){ return -1; } filp->private_data = pcdevp; //申请gpio管脚 gpio_request(led_gpio_table[0], "GPC1_3"); gpio_request(led_gpio_table[1], "GPC1_4"); return 0; } int cdd_read(struct file *filp, char __user *buf, size_t count, loff_t *offset) { int ret = 0; u32 pos = *offset; u32 cnt = count; struct cdd_cdev *cdevp = filp->private_data; //printk("enter cdd_read!\n"); if(cnt > (cdevp->data_len-pos) ){ cnt = cdevp->data_len - pos; } ret = copy_to_user(buf, cdevp->kbuf+pos, cnt); //printk("kernel kbuf content:%s\n", cdevp->kbuf); *offset += cnt; return ret; } int cdd_write(struct file *filp, const char __user *buf, size_t count, loff_t *offset) { int ret = 0; struct cdd_cdev *cdevp = filp->private_data; u32 pos = *offset; u32 cnt = count; //printk("enter cdd_write!\n"); if(cnt > (BUF_SIZE - pos) ){ cnt = BUF_SIZE - pos; } ret = copy_from_user(cdevp->kbuf+pos, buf, cnt); *offset += cnt; if(*offset > cdevp->data_len){ cdevp->data_len = *offset; } return ret; } int cdd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long data) { //printk("enter cdd_ioctl!\n"); switch(cmd){ case 1://点亮灯 //设置管脚为输出功能 //参数:1.要设置的管脚编号2.默认的输出值 0低电平1高电平 gpio_direction_output(led_gpio_table[data], 0); //禁止内部上拉 s3c_gpio_setpull(led_gpio_table[data], SEC_GPIO_PULL_NONE); //设置输出值 gpio_set_value(led_gpio_table[data], 1); break; case 0://熄灭灯 //设置管脚为输出功能 //参数:1.要设置的管脚编号2.默认的输出值 0低电平1高电平 gpio_direction_output(led_gpio_table[data], 0); //禁止内部上拉 s3c_gpio_setpull(led_gpio_table[data], SEC_GPIO_PULL_NONE); //设置输出值 gpio_set_value(led_gpio_table[data], 0); break; default: return -EINVAL; } return 0; } int cdd_release(struct inode *inode, struct file *filp) { struct cdd_cdev *pcdevp = filp->private_data; printk("enter cdd_release!\n"); gpio_free(led_gpio_table[0]); gpio_free(led_gpio_table[1]); //释放信号量 up(&pcdevp->sem_open); return 0; } loff_t cdd_llseek(struct file *filp, loff_t offset, int whence) { struct cdd_cdev *pcdevp = filp->private_data; loff_t newpos = 0; switch(whence){ case SEEK_SET: newpos = offset; break; case SEEK_CUR: newpos = filp->f_pos + offset; break; case SEEK_END: newpos = pcdevp->data_len + offset; break; default: return -EINVAL;//无效的参数 } if( newpos<0 || newpos>= BUF_SIZE ){ return -EINVAL; } filp->f_pos = newpos; return newpos; } struct file_operations cdd_fops = { .owner = THIS_MODULE, .open = cdd_open, .read = cdd_read, .write = cdd_write, .ioctl = cdd_ioctl, .release = cdd_release, .llseek = cdd_llseek, }; int __init cdd_init(void) { int ret = 0; int i = 0; if(cdd_major){ dev = MKDEV(CDD_MAJOR, CDD_MINOR);//生成设备号 //注册设备号;1、要注册的起始设备号2、连续注册的设备号个数3、名字 ret = register_chrdev_region(dev, CDD_COUNT, "cdd_demo"); }else{ // 动态分配设备号 ret = alloc_chrdev_region(&dev, cdd_minor, CDD_COUNT, "cdd_demo02"); } if(ret < 0){ printk("register_chrdev_region failed!\n"); goto failure_register_chrdev; } //获取主设备号 cdd_major = MAJOR(dev); printk("cdd_major = %d\n", cdd_major); cdd_cdevp = kzalloc(sizeof(struct cdd_cdev)*CDD_COUNT, GFP_KERNEL); if(IS_ERR(cdd_cdevp)){ printk("kzalloc failed!\n"); goto failure_kzalloc; } /*创建设备类*/ dev_class = class_create(THIS_MODULE, "cdd_class"); if(IS_ERR(dev_class)){ printk("class_create failed!\n"); goto failure_dev_class; } for(i=0; i<CDD_COUNT; i++){ /*初始化cdev*/ cdev_init(&(cdd_cdevp[i].cdev), &cdd_fops); /*添加cdev到内核*/ cdev_add(&(cdd_cdevp[i].cdev), dev+i, 1); /* “/dev/xxx” */ device_create(dev_class, NULL, dev+i, NULL, "cdd%d", i); cdd_cdevp[i].led = i; //初始化信号量 sem_init(&cdd_cdevp[i].sem_open, 2);//用户空间运行打开2次 } return 0; failure_dev_class: kfree(cdd_cdevp); failure_kzalloc: unregister_chrdev_region(dev, CDD_COUNT); failure_register_chrdev: return ret; } void __exit cdd_exit(void) { /*逆序消除*/ int i = 0; for(; i < CDD_COUNT; i++){ device_destroy(dev_class, dev+i); cdev_del(&(cdd_cdevp[i].cdev)); //cdev_del(&((cdd_cdevp+i)->cdev)); } class_destroy(dev_class); kfree(cdd_cdevp); unregister_chrdev_region(dev, CDD_COUNT); } module_init(cdd_init); module_exit(cdd_exit);
/** *Copyright (c) 2013.TianYuan *All rights reserved. * *文件名称: char_device_driver12_test.c *文件标识: 只允许用户空间有2个进程可以开发设备文件,第3个进程睡眠 * *当前版本:1.0 *作者:wuyq * *取代版本:xxx *原作者:xxx *完成日期:2013-11-29 */ #include <stdio.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> /*手工创建设备节点文件 mknod /dev/cdd c 248 0 */ int fd = 0; char rbuf[100]; char wbuf[100] = "nihao!\n"; int main() { char ch; fd = open("/dev/led0", O_RDWR); if(fd < 0){ printf("open failed!\n"); return -1; } printf("open successed fd = %d\n", fd); while(1) { printf("starting to test /dev/cdd...\n"); ch = getchar(); getchar();//取走回车 if(ch == ‘q‘){ break; } switch(ch){ case ‘r‘: memset(rbuf, 0, 100);//清空 read(fd, rbuf, 3); printf("user space from kernel: %s\n", rbuf); break; case ‘w‘: write(fd, wbuf, strlen(wbuf) ); break; case ‘o‘: ioctl(fd, 0, 0); break; case ‘O‘: ioctl(fd, 1, 0); break; case ‘p‘: ioctl(fd, 0, 1); break; case ‘P‘: ioctl(fd, 1, 1); break; case ‘l‘: lseek(fd, 0, SEEK_SET);//移动的文件的开头 break; default: break; } sleep(1); } close(fd); return 0; }