相关API如下:
#include<linux/wait.h>
//变量定义
wait_queue_head_t wait;
//队列初始化
init_waitqueue_head(&wait);
//等待条件满足,或者时间到了函数返回,或者被唤醒(返回剩余的时间值)
wait_event_timeout(wq_head, condition, timeout);
wait_event_interruptible_timeout(wq_head, condition, timeout);
//唤醒
wake_up(wait)
例程演示:
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/fs.h>
#include<linux/slab.h>
#include<linux/timer.h>
#include<linux/sched.h>
#include<linux/list.h>
#include<linux/interrupt.h>
#include<linux/jiffies.h>
#include<linux/wait.h>
#include<linux/cdev.h> //cdev_init() cdev_add() cdev_del()
#include<linux/types.h> //dev_t
#include<linux/kdev_t.h> //有两个宏获取主设备号和次设备号
#include<linux/uaccess.h> //container_of
MODULE_AUTHOR("Tan xujia");
MODULE_LICENSE("Dual BSD/GPL");
dev_t c_devt; //设备的主次设备号
/*定义一个结构体,包含了cdev,类似与继承,把这个设备文件的属性给继承过来*/
struct char_dev {
struct cdev c_dev;
char *c;
int n;
};
wait_queue_head_t wait;
bool con = false;
/*设备的操作方法*/
int
char_open(struct inode *inode, struct file *filp)
{
printk("char_open\n");
return 0;
}
ssize_t
char_read(struct file *filp, char __user *buf, size_t count, loff_t *fpos)
{
printk("char_read\n");
wait_event_interruptible_timeout(wait, con, msecs_to_jiffies(10000));
return 0;
}
ssize_t
char_write(struct file *filp, const char __user *buf, size_t count, loff_t *fpos)
{
printk("char_write\n");
wake_up(&wait);
con = true;
return count;
}
int
char_release(struct inode *inode, struct file *filp)
{
printk("char_release\n");
return 0;
}
struct file_operations char_fops = {
.owner = THIS_MODULE,
.read = char_read,
.write = char_write,
.open = char_open,
.release = char_release,
};
struct class *cdev_class;
struct char_dev *cdev_p;
static
int __init hello_init (void)
{
int ret = 0, index = 0;
printk("hello_init\n");
ret = alloc_chrdev_region(&c_devt, 0, 2, "char_dev");
if(ret)
printk("alloc_chrdev_region fail\n");
printk("major = %d, minor = %d\n", MAJOR(c_devt), MINOR(c_devt));
cdev_p = kzalloc(sizeof(struct char_dev) * 2, GFP_KERNEL);
if(!cdev_p)
printk("alloc fail\n");
for(index = 0; index < 2; index++) {
cdev_init(&cdev_p[index].c_dev, &char_fops);
cdev_p[index].c_dev.owner = THIS_MODULE;
ret = cdev_add(&cdev_p[index].c_dev,
MKDEV(MAJOR(c_devt), MINOR(c_devt) + index), 1);
if(ret)
printk("cdev_add fail\n");
}
/*根据以下api替代mknod生成设备节点*/
cdev_class = class_create(THIS_MODULE, "c_dev");
if(!cdev_class)
printk("class_create fail\n");
for(index = 0; index < 2; index++) {
device_create(cdev_class, NULL,
MKDEV(MAJOR(c_devt), MINOR(c_devt) + index),
NULL, "c_dev%d", index);
}
init_waitqueue_head(&wait);
return ret;
}
static
void __exit hello_exit (void)
{
int index = 0;
printk("hello_exit\n");
for(index = 0; index < 2; index++) {
device_destroy(cdev_class,
MKDEV(MAJOR(c_devt), MINOR(c_devt) + index));
}
class_destroy(cdev_class);
for(index = 0; index < 2; index++) {
cdev_del(&cdev_p[index].c_dev);
}
kfree(cdev_p);
unregister_chrdev_region(c_devt, 2);
}
module_init(hello_init);
module_exit(hello_exit);
Makefile:
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
obj-m :=mywait.o
all:
make -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.* .tmp_versions *.mod *.order *.symvers *.dwo
类似这方面的接口还有很多,这几个已经够用了,代码里开两个终端,一个先读,然后另一个写,此时con为ture,但是另一边读的终端不会立即返回。。要到时间时才返回。。。
上面涉及到了一些与时间相关的API
<include/linux/jiffies.h>
//jiffies与ms,us互转
unsigned int jiffies_to_msecs(const unsigned long j);
unsigned int jiffies_to_usecs(const unsigned long j);
unsigned long msecs_to_jiffies(const unsigned int m);
unsigned long usecs_to_jiffies(const unsigned int u);
<include/linux/jiffies.h>
unsigned long timespec_to_jiffies(const struct timespec *value);
void jiffies_to_timespec(const unsigned long jiffies, struct timespec *value);
unsigned long timeval_to_jiffies(const struct timeval *value);
void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value);
<include/linux/time.h>
struct timespec {
__kernel_time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
struct timeval {
__kernel_time_t tv_sec; /* seconds */
__kernel_suseconds_t tv_usec; /* microseconds */
};