目录
一、五种IO模型------读写外设数据的方式
二、阻塞与非阻塞
三、多路复用
3.1 应用层:三套接口select、poll、epoll
3.2 驱动层:实现poll函数
四、信号驱动
4.1 应用层:信号注册+fcntl
4.2 驱动层:实现fasync函数
一、五种IO模型------读写外设数据的方式
1. 阻塞: 不能操作就睡觉
(进入睡眠态,不参与时间片轮转机制)
2. 非阻塞:不能操作就返回错误
3. 多路复用:委托中介监控
4. 信号驱动:让内核如果能操作时发信号,在信号处理函数中操作
(不可靠,项目少用。序号小于32的信号连续发送内核只认第一次.多用于两个进程一对一)
(当程序发出一个信号给另一个程序时先到内核中的系统调用函数,当回到用户态时看看收没收到信号然后才进入信号处理函数。然后回到系统调用函数再回到用户态程序继续执行i。再处理时如果来的信号序号小于32都会被丢掉)
(让内核帮我们检查能不能做。如果能做再信号处理函数中操作)
5. 异步IO:向内核注册操作请求,内核完成操作后发通知信号
(在内核中注册一个信号,交给内核。内核完成操作后给程序发信号)
二、阻塞与非阻塞
应用层:
open时由O_NONBLOCK指示read、write时是否阻塞
(O_RDWD | O_NONBLOCK)非阻塞
open以后可以由fcntl函数来改变是否阻塞:
```
flags = fcntl(fd,F_GETFL,0);
flags |= O_NONBLOCK; //flags |= (~O_NONBLOCK);
fcntl(fd, F_SETFL, flags);
```
(就绪态和运行态之间的转换
就绪态有一个链式队列,队列中有很多节点
当一个cpu时间片开始时取出队列中的头节点去运行一个时间片然后插入到队尾
睡眠态怎么实现呢,再搞一个队列叫等待队列。把一个任务放到等待队列就处于睡眠态了。把任务从等待队列放到就绪队列就会继续参与时间片轮转。
睡眠分两种,资源就位了,满足的条件成立了才会醒来的叫深度睡眠,除了这俩情况接收到信号也会醒来的叫浅睡眠)
驱动层:通过等待队列
严格来说下面这俩不是函数而是带参的宏
```c wait_queue_head_t //等待队列头数据类型 init_waitqueue_head(wait_queue_head_t *pwq) //初始化等待队列头 wait_event_interruptible(wq,condition) /* 功能:条件不成立则让任务进入浅度睡眠,直到条件成立醒来 wq:等待队列头 condition:C语言表达式 返回:正常唤醒返回0,信号唤醒返回非0(此时读写操作函数应返回-ERESTARTSYS) */ wait_event(wq,condition) //深度睡眠 wake_up_interruptible(wait_queue_head_t *pwq) wake_up(wait_queue_head_t *pwq) /* 1. 读、写用不同的等待队列头rq、wq 2. 无数据可读、可写时调用wait_event_interruptible(rq、wq,条件) 3. 写入数据成功时唤醒rq,读出数据成功唤醒wq */ ```
make
sudo lsmod | grep char
sudo rmmod multimychar
sudo insmod char.ko
cat proc/devices | grep char
ll /dev/mydev*
gcc -o testread testnonblockread.c
./testread /dev/mydev0
gcc -o testblockread testblockread.c
echo hello > /dev/mydev0
验证写可以先把buf写满然后用cat取走数据就会停止阻塞
#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 "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;
/*Read wait queue and write wait queue*/
wait_queue_head_t rq;
wait_queue_head_t wq;
};
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");
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;
if(pmydev->curlen <= 0)
{
if(pfile->f_flags & O_NONBLOCK)
{//non-blocking
printk("O_NONBLOCK No Data Read\n");
return -1;
}
else
{//blocking
wait_event_interruptible(pmydev->rq,pmydev->curlen > 0);
if(ret)
{
printk("Wake up by signal\n");
return -ERESTARTSYS;
}
}
}
if(count > pmydev->curlen)
{
size = pmydev->curlen;
}
else
{
size = count;
}
ret = copy_to_user(puser, pmydev->mydev_buf, size);
if(ret)
{
printk("copy_to_user failed\n");
return -1;
}
memcpy(pmydev->mydev_buf, pmydev->mydev_buf + size, pmydev->curlen - size);
pmydev->curlen = pmydev->curlen - size;
/*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;
if(pmydev->curlen >= BUF_LEN)
{
if(pfile->f_flags & O_NONBLOCK)
{
printk("O_NONBLOCK can not write\n");
return -1;
}
else
{
ret = wait_event_interruptible(pmydev->wq,
pmydev->curlen < BUF_LEN);
if(ret)
{
printk("wake up by signal\n");
return -ERESTARTSYS;
}
}
}
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)
{
printk("copy_from_user failed\n");
return -1;
}
pmydev->curlen += size;
/*Wake up interrupt*/
wake_up_interruptible(&pmydev->rq);
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:
ret = copy_to_user(pret, &pmydev->curlen, sizeof(int));
if(ret)
{
printk("copy_to_user MAXLEN failed\n");
return -1;
}
break;
default:
printk("The cmd is unknow\n");
return -1;
}
return 0;
}
struct file_operations myops = {
.owner = THIS_MODULE,
.open = mychar_open,
.release = mychar_close,
.read = mychar_read,
.write = mychar_write,
.unlocked_ioctl = mychar_ioctl,
};
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);
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);
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include "mychar.h"
#include <sys/ioctl.h>
int main(int argc, char *argv[])
{
int fd = -1;
char buf[8] = "";
int ret = 0;
if(argc < 2)
{
printf("The argument is too few\n");
return 1;
}
fd = open(argv[1], O_RDWR | O_NONBLOCK);
if(fd < 0)
{
printf("open %s failed\n", argv[1]);
return 2;
}
ret = read(fd, buf, 8);
if(ret < 0)
{
printf("read data failed\n");
}
printf("buf = %s\n", buf);
close(fd);
fd = -1;
return 0;
}
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include "mychar.h"
#include <sys/ioctl.h>
int main(int argc, char *argv[])
{
int fd = -1;
char buf[8] = "";
int ret = 0;
if(argc < 2)
{
printf("The argument is too few\n");
return 1;
}
fd = open(argv[1], O_RDWR);
if(fd < 0)
{
printf("open %s failed\n", argv[1]);
return 2;
}
ret = read(fd, buf, 8);
if(ret < 0)
{
printf("read data failed\n");
}
printf("buf = %s\n", buf);
close(fd);
fd = -1;
return 0;
}
三、多路复用
描述符:
1. 文件描述符:设备文件、管道文件
2. socket描述符
3.1 应用层:三套接口select、poll、epoll
select:位运算实现 监控的描述符数量有限(32位机1024,64位机2048) 效率差
poll:链表实现,监控的描述符数量不限 效率差
epoll:效率最高,监控的描述符数量不限
select
```c int select(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout); /* 功能:监听多个描述符,阻塞等待有一个或者多个文件描述符,准备就绪。 内核将没有准备就绪的文件描述符,从集合中清掉了。 参数: nfds 最大文件描述符数 ,加1 readfds 读文件描述符集合 writefds 写文件描述符集合 exceptfds 其他异常的文件描述符集合 timeout 超时时间(NULL) 返回值:当timeout为NULL时返回0,成功:准备好的文件描述的个数 出错:-1 当timeout不为NULL时,如超时设置为0,则select为非阻塞,超时设置 > 0,则无描述符可被操作的情况下阻塞指定长度的时间 */ void FD_CLR(int fd, fd_set *set); //功能:将fd 从集合中清除掉 int FD_ISSET(int fd, fd_set *set); //功能:判断fd 是否存在于集合中 void FD_SET(int fd, fd_set *set); //功能:将fd 添加到集合中 void FD_ZERO(fd_set *set); //功能:将集合清零 //使用模型: while(1) { /*得到最大的描述符maxfd*/ /*FD_ZERO清空描述符集合*/ /*将被监控描述符加到相应集合rfds里 FD_SET*/ /*设置超时*/ ret = select(maxfd+1,&rfds,&wfds,NULL,NULL); if(ret < 0) { if(errno == EINTR)//错误时信号引起的 { continue; } else { break; } } else if(ret == 0) {//超时 //..... } else { //> 0 ret为可被操作的描述符个数 if(FD_ISSET(fd1,&rfds)) {//读数据 //.... } if(FD_ISSET(fd2,&rfds)) {//读数据 //.... } ///..... if(FD_ISSET(fd1,&wfds)) {//写数据 //.... } } } ```
3.2 驱动层:实现poll函数
```c void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p); /*功能:将等待队列头添加至poll_table表中 参数:struct file :设备文件 Wait_queue_head_t :等待队列头 Poll_table :poll_table表 */ /*该函数与select、poll、epoll_wait函数相对应,协助这些多路监控函数判断本设备是否有数据可读写*/ unsigned int xxx_poll(struct file *filp, poll_table *wait) //函数名初始化给struct file_operations的成员.poll { unsigned int mask = 0; /* 1. 将所有等待队列头加入poll_table表中 2. 判断是否可读,如可读则mask |= POLLIN | POLLRDNORM; 3. 判断是否可写,如可写则mask |= POLLOUT | POLLWRNORM; */ return mask; } ```
grep poll_wait ./ -r -n
阻塞在这
#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;
/*Read wait queue and write wait queue*/
wait_queue_head_t rq;
wait_queue_head_t wq;
};
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");
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;
if(pmydev->curlen <= 0)
{
if(pfile->f_flags & O_NONBLOCK)
{//non-blocking
printk("O_NONBLOCK No Data Read\n");
return -1;
}
else
{//blocking
wait_event_interruptible(pmydev->rq,pmydev->curlen > 0);
if(ret)
{
printk("Wake up by signal\n");
return -ERESTARTSYS;
}
}
}
if(count > pmydev->curlen)
{
size = pmydev->curlen;
}
else
{
size = count;
}
ret = copy_to_user(puser, pmydev->mydev_buf, size);
if(ret)
{
printk("copy_to_user failed\n");
return -1;
}
memcpy(pmydev->mydev_buf, pmydev->mydev_buf + size, pmydev->curlen - size);
pmydev->curlen = pmydev->curlen - size;
/*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;
if(pmydev->curlen >= BUF_LEN)
{
if(pfile->f_flags & O_NONBLOCK)
{
printk("O_NONBLOCK can not write\n");
return -1;
}
else
{
ret = wait_event_interruptible(pmydev->wq,
pmydev->curlen < BUF_LEN);
if(ret)
{
printk("wake up by signal\n");
return -ERESTARTSYS;
}
}
}
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)
{
printk("copy_from_user failed\n");
return -1;
}
pmydev->curlen += size;
/*Wake up interrupt*/
wake_up_interruptible(&pmydev->rq);
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:
ret = copy_to_user(pret, &pmydev->curlen, sizeof(int));
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);
if(pmydev->curlen > 0)
{
mask |= POLLIN | POLLRDNORM;
}
if(pmydev->curlen < BUF_LEN)
{
mask |= POLLOUT | POLLWRNORM;
}
return mask;
}
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,
};
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);
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);
#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>
int main(int argc, char *argv[])
{
int fd = -1;
char buf[8] = "";
int ret = 0;
fd_set rfds;
if(argc < 2)
{
printf("The argument is too few\n");
return 1;
}
fd = open(argv[1], O_RDWR);
if(fd < 0)
{
printf("open %s failed\n", argv[1]);
return 2;
}
while(1)
{
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
ret = select(fd + 1,&rfds,NULL,NULL,NULL);
if(ret < 0)
{
if(errno == EINTR)
{
continue;
}
else
{
printf("select error\n");
break;
}
}
if(FD_ISSET(fd, &rfds))
{
read(fd,buf,8);
printf("buf = %s\n", buf);
}
}
ret = read(fd, buf, 8);
if(ret < 0)
{
printf("read data failed\n");
}
printf("buf = %s\n", buf);
close(fd);
fd = -1;
return 0;
}
四、信号驱动
4.1 应用层:信号注册+fcntl
```c signal(SIGIO, input_handler); //注册信号处理函数 fcntl(fd, F_SETOWN, getpid());//将描述符设置给对应进程,好由描述符获知PID oflags = fcntl(fd, F_GETFL); fcntl(fd, F_SETFL, oflags | FASYNC);//将该设备的IO模式设置成信号驱动模式 void input_handler(int signum)//应用自己实现的信号处理函数,在此函数中完成读写 { //读数据 } //应用模板 int main() { int fd = open("/dev/xxxx",O_RDONLY); fcntl(fd, F_SETOWN, getpid()); oflags = fcntl(fd, F_GETFL); fcntl(fd, F_SETFL, oflags | FASYNC); signal(SIGIO,xxxx_handler); //...... } void xxxx_handle(int signo) {//读写数据 } ```
4.2 驱动层:实现fasync函数
```c /*设备结构中添加如下成员*/ struct fasync_struct *pasync_obj; /*应用调用fcntl设置FASYNC时调用该函数产生异步通知结构对象,并将其地址设置到设备结构成员中*/ static int hello_fasync(int fd, struct file *filp, int mode) //函数名初始化给struct file_operations的成员.fasync { struct hello_device *dev = filp->private_data; return fasync_helper(fd, filp, mode, &dev->pasync_obj); } /*写函数中有数据可读时向应用层发信号*/ if (dev->pasync_obj) kill_fasync(&dev->pasync_obj, SIGIO, POLL_IN); /*release函数中释放异步通知结构对象*/ if (dev->pasync_obj) fasync_helper(-1, filp, 0, &dev->pasync_obj); int fasync_helper(int fd, struct file *filp, int mode, struct fasync_struct **pp); /* 功能:产生或释放异步通知结构对象 参数: 返回值:成功为>=0,失败负数 */ void kill_fasync(struct fasync_struct **, int, int); /* 功能:发信号 参数: struct fasync_struct ** 指向保存异步通知结构地址的指针 int 信号 SIGIO/SIGKILL/SIGCHLD/SIGCONT/SIGSTOP int 读写信息POLLIN、POLLOUT */ ```
写数据,信号处理函数会打印出来
#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;
/*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;
if(pmydev->curlen <= 0)
{
if(pfile->f_flags & O_NONBLOCK)
{//non-blocking
printk("O_NONBLOCK No Data Read\n");
return -1;
}
else
{//blocking
wait_event_interruptible(pmydev->rq,pmydev->curlen > 0);
if(ret)
{
printk("Wake up by signal\n");
return -ERESTARTSYS;
}
}
}
if(count > pmydev->curlen)
{
size = pmydev->curlen;
}
else
{
size = count;
}
ret = copy_to_user(puser, pmydev->mydev_buf, size);
if(ret)
{
printk("copy_to_user failed\n");
return -1;
}
memcpy(pmydev->mydev_buf, pmydev->mydev_buf + size, pmydev->curlen - size);
pmydev->curlen = pmydev->curlen - size;
/*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;
if(pmydev->curlen >= BUF_LEN)
{
if(pfile->f_flags & O_NONBLOCK)
{
printk("O_NONBLOCK can not write\n");
return -1;
}
else
{
ret = wait_event_interruptible(pmydev->wq,
pmydev->curlen < BUF_LEN);
if(ret)
{
printk("wake up by signal\n");
return -ERESTARTSYS;
}
}
}
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)
{
printk("copy_from_user failed\n");
return -1;
}
pmydev->curlen += size;
/*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:
ret = copy_to_user(pret, &pmydev->curlen, sizeof(int));
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);
if(pmydev->curlen > 0)
{
mask |= POLLIN | POLLRDNORM;
}
if(pmydev->curlen < BUF_LEN)
{
mask |= POLLOUT | POLLWRNORM;
}
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);
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);
#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;
void sigio_handler(int signo);
int main(int argc, char *argv[])
{
int ret = 0;
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;
}
fcntl(fd, F_SETOWN, getpid());
flg = fcntl(fd, F_GETFL);
flg |= FASYNC;
fcntl(fd, F_SETFL, flg);
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);
}
---------------------------------------------------------------------------------------------------------------------------------好久没写小作文了,
有人说人生有三个境界:
第一层见自己,说的是格局;第二层见天地,说的是眼界;第三层见众生,讲的是良心。
格局大了,才有机会突破自己的圈子,成为更好的自己;
眼界宽了,才能看到更大的天地,公司才会越做越大;
良心有了,就想着去兼济天下,这才是一个伟大企业家该有的责任和使命。
活成自己的样式才是我的人生。不过这碗汤不错先干为敬。最近这驱动学的头都秃了。
人生三大错觉她喜欢我。舔了好久。突然有一天就惊醒了。还是活自己吧。不要在不可能的地方停留太久。那样只会降低你的价值。然后每天幽怨的像个怨妇。20多岁的大好年纪还是先提升自己吧。与看到此篇文章的诸君共勉。为了吸引流量的场面话的就免了。我回都回累了。你们是来学习的还是来吸引粉丝的。虽然有人关注有人喜欢的感觉很好。但是不要本末倒置。我也很喜欢这种感觉。但是现阶段还是先学技术。等我实现理想就去写书。疯狂给热血青年灌鸡汤哈哈。
---------------------------------------------------------------------------------------------------------------------------------