当前位置: 首页 > news >正文

衡水专业制作网站大画册设计网站

衡水专业制作网站,大画册设计网站,电脑系统优化软件排行榜,做网站的挣钱么一、linux信号驱动IO介绍 信号驱动 IO 不需要应用程序查询设备的状态#xff0c;一旦设备准备就绪#xff0c;会触发SIGIO信号#xff0c;进而调用注册的处理函数。 1.1、linux信号驱动IO应用层 如果要实现信号驱动 IO#xff0c;需要应用程序和驱动程序配合#xff0c;应…一、linux信号驱动IO介绍 信号驱动 IO 不需要应用程序查询设备的状态一旦设备准备就绪会触发SIGIO信号进而调用注册的处理函数。 1.1、linux信号驱动IO应用层 如果要实现信号驱动 IO需要应用程序和驱动程序配合应用程序使用信号驱动IO的步骤有三步   步骤 1 注册信号处理函数 应用程序使用 signal 函数来注册SIGIO 信号的信号处理函数。   步骤 2 设置能够接收这个信号的进程   步骤 3 开启信号驱动 IO 通常使用 fcntl 函数的 F_SETFL 命令打开FASYNC 标志。   fcntl 函数如下所示   函数原型     int fcntl(int fd,int cmd, …)   函数功能     fcntl 函数可以用来操作文件描述符   函数参数:     fd: 被操作的文件描述符     cmd: 操作文件描述符的命令cmd 参数决定了要如何操作文件描述符fd     …: 根据 cmd 的参数来决定是不是需要使用第三个参数   操作文件描述符的命令如下表 命令名描述F_DUPFD复制文件描述符F_GETFD获取文件描述符标志F_SETFD设置文件描述符标志F_GETFL获取文件状态标志F_SETFL设置文件状态标志F_GETLK获取文件锁F_SETLK设置文件锁F_SETLKW类似 F_SETLK但等待返回F_GETOWN获取当前接收 SIGIO 和SIGURG 信号的进程ID和进程组 IDF_SETOWN设置当前接收 SIGIO 和SIGURG 信号的进程ID和进程组 ID 1.2、linux信号驱动IO驱动层 当应用程序开启信号驱动 IO 时会触发驱动中的 fasync 函数。所以首先在file_operations结构体中实现 fasync 函数函数原型如下 int (*fasync) (int fd,struct file *filp,int on)在驱动中的 fasync 函数调用 fasync_helper 函数来操作 fasync_struct 结构体fasync_helper函数原型如下 int fasync_helper(int fd,struct file *filp,int on,struct fasync_struct **fapp)当设备准备好的时候驱动程序需要调用 kill_fasync 函数通知应用程序此时应用程序的SIGIO 信号处理函数就会被执行。kill_fasync 负责发送指定的信号函数原型如下 void kill_fasync(struct fasync_struct **fp,int sig,int band)函数参数     fp: 要操作的 fasync_struct     sig: 发送的信号     band: 可读的时候设置成 POLLIN 可写的时候设置成POLLOUT 二、代码示例 2.1、应用层程序 #include stdio.h #include sys/types.h #include sys/stat.h #include fcntl.h #include unistd.h #include stdlib.h #include poll.h #include fcntl.h #include signal.hint fd; char buf1[32] {0}; //SIGIO信号的信号处理函数 static void func(int signum) {read(fd,buf1,32);printf (buf is %s\n,buf1); } int main(int argc, char *argv[]) {int ret;int flags;fd open(/dev/test, O_RDWR); //打开led驱动if (fd 0){perror(open error \n);return fd;}signal(SIGIO,func); //步骤一使用signal函数注册SIGIO信号的信号处理函数//步骤二设置能接收这个信号的进程//fcntl函数用来操作文件描述符//F_SETOWN 设置当前接收的SIGIO的进程IDfcntl(fd,F_SETOWN,getpid()); flags fcntl(fd,F_GETFD); //获取文件描述符标志//步骤三 开启信号驱动IO 使用fcntl函数的F_SETFL命令打开FASYNC标志fcntl(fd,F_SETFL,flags| FASYNC);while(1);close(fd); //关闭文件return 0; }2.2、驱动层程序 #include linux/init.h #include linux/module.h #include linux/kdev_t.h #include linux/fs.h #include linux/cdev.h #include linux/uaccess.h #include linux/io.h #include linux/wait.h #include linux/poll.h #include linux/fcntl.h #include linux/signal.hstruct device_test{dev_t dev_num; //设备号int major ; //主设备号int minor ; //次设备号struct cdev cdev_test; // cdevstruct class *class; //类struct device *device; //设备char kbuf[32];int flag; //标志位struct fasync_struct *fasync; };struct device_test dev1; DECLARE_WAIT_QUEUE_HEAD(read_wq); //定义并初始化等待队列头/*打开设备函数*/ static int cdev_test_open(struct inode *inode, struct file *file) {file-private_datadev1;//设置私有数据return 0; }/*向设备写入数据函数*/ static ssize_t cdev_test_write(struct file *file, const char __user *buf, size_t size, loff_t *off) {struct device_test *test_dev(struct device_test *)file-private_data;if (copy_from_user(test_dev-kbuf, buf, size) ! 0) // copy_from_user:用户空间向内核空间传数据{printk(copy_from_user error\r\n);return -1;}test_dev-flag1;wake_up_interruptible(read_wq);kill_fasync(test_dev-fasync,SIGIO,POLLIN);return 0; }/**从设备读取数据*/ static ssize_t cdev_test_read(struct file *file, char __user *buf, size_t size, loff_t *off) {struct device_test *test_dev(struct device_test *)file-private_data;if(file-f_flags O_NONBLOCK ){if (test_dev-flag !1)return -EAGAIN;}wait_event_interruptible(read_wq,test_dev-flag);if (copy_to_user(buf, test_dev-kbuf, strlen( test_dev-kbuf)) ! 0) // copy_to_user:内核空间向用户空间传数据{printk(copy_to_user error\r\n);return -1;}return 0; }static int cdev_test_release(struct inode *inode, struct file *file) {return 0; }static __poll_t cdev_test_poll(struct file *file, struct poll_table_struct *p){struct device_test *test_dev(struct device_test *)file-private_data; //设置私有数据__poll_t mask0; poll_wait(file,read_wq,p); //应用阻塞if (test_dev-flag 1) {mask | POLLIN;}return mask;}static int cdev_test_fasync (int fd, struct file *file, int on) {struct device_test *test_dev(struct device_test *)file-private_data; //设置私有数据return fasync_helper(fd,file,on,test_dev-fasync); } /*设备操作函数*/ struct file_operations cdev_test_fops {.owner THIS_MODULE, //将owner字段指向本模块可以避免在模块的操作正在被使用时卸载该模块.open cdev_test_open, //将open字段指向chrdev_open(...)函数.read cdev_test_read, //将open字段指向chrdev_read(...)函数.write cdev_test_write, //将open字段指向chrdev_write(...)函数.release cdev_test_release, //将open字段指向chrdev_release(...)函数.poll cdev_test_poll, //将poll字段指向chrdev_poll(...)函数.fasync cdev_test_fasync, //将fasync字段指向cdev_test_fasync(...)函数 };static int __init chr_fops_init(void) //驱动入口函数 {/*注册字符设备驱动*/int ret;/*1 创建设备号*/ret alloc_chrdev_region(dev1.dev_num, 0, 1, alloc_name); //动态分配设备号if (ret 0){goto err_chrdev;}printk(alloc_chrdev_region is ok\n);dev1.major MAJOR(dev1.dev_num); //获取主设备号dev1.minor MINOR(dev1.dev_num); //获取次设备号printk(major is %d \r\n, dev1.major); //打印主设备号printk(minor is %d \r\n, dev1.minor); //打印次设备号/*2 初始化cdev*/dev1.cdev_test.owner THIS_MODULE;cdev_init(dev1.cdev_test, cdev_test_fops);/*3 添加一个cdev,完成字符设备注册到内核*/ret cdev_add(dev1.cdev_test, dev1.dev_num, 1);if(ret0){goto err_chr_add;}/*4 创建类*/dev1. class class_create(THIS_MODULE, test);if(IS_ERR(dev1.class)){retPTR_ERR(dev1.class);goto err_class_create;}/*5 创建设备*/dev1.device device_create(dev1.class, NULL, dev1.dev_num, NULL, test);if(IS_ERR(dev1.device)){retPTR_ERR(dev1.device);goto err_device_create;}return 0;err_device_create:class_destroy(dev1.class); //删除类err_class_create:cdev_del(dev1.cdev_test); //删除cdeverr_chr_add:unregister_chrdev_region(dev1.dev_num, 1); //注销设备号err_chrdev:return ret; }static void __exit chr_fops_exit(void) //驱动出口函数 {/*注销字符设备*/unregister_chrdev_region(dev1.dev_num, 1); //注销设备号cdev_del(dev1.cdev_test); //删除cdevdevice_destroy(dev1.class, dev1.dev_num); //删除设备class_destroy(dev1.class); //删除类 } module_init(chr_fops_init); module_exit(chr_fops_exit);2.3、linux信号驱动IO使用API要点 应用层 signal(SIGIO,func); //步骤一使用signal函数注册SIGIO信号的信号处理函数//步骤二设置能接收这个信号的进程//fcntl函数用来操作文件描述符//F_SETOWN 设置当前接收的SIGIO的进程IDfcntl(fd,F_SETOWN,getpid()); flags fcntl(fd,F_GETFD); //获取文件描述符标志//步骤三 开启信号驱动IO 使用fcntl函数的F_SETFL命令打开FASYNC标志fcntl(fd,F_SETFL,flags| FASYNC);驱动层 kill_fasync(test_dev-fasync,SIGIO,POLLIN);static int cdev_test_fasync (int fd, struct file *file, int on) {struct device_test *test_dev(struct device_test *)file-private_data; //设置私有数据return fasync_helper(fd,file,on,test_dev-fasync); }
http://www.zqtcl.cn/news/461519/

相关文章:

  • 网站建设与管理维护 大学论文铁路建设单位网站
  • 贵州企业展示型网站建设wordpress文章点不开
  • 毕业设计可以做网站吗网页版征信报告查询
  • 企业网站每年的费用钢筋网片每平米重量
  • 做网站是属火的职业吗苏州网站建设信息网络
  • 怎么自己建一个论坛网站如何做中国古城的网站
  • 做网站表格网站建设综合实训案例
  • vs2012 网站开发wordpress好看的页面跳转
  • 阿里去要企业网站建设方案书小程序开发 杭州
  • 微信公众号文档网站开发与优化课程总结
  • 网站建设网课海东营销网站建设公司
  • 仿站工具教程视频宣传片免费模板
  • 美食网站建设项目分析报告莱芜都市网人才招聘
  • js做网站福田网站设计公司
  • 郑州做网站好长春市建设工程信息网站
  • 题库网站怎么做做网站文件夹都起什么名字
  • 河南网站怎么备案东台市住房和建设局网站
  • 有什么手机做网站的asp.net网站安全
  • 怎么做查询网站后台做爰的视频网站
  • 迪虎科技网站建设国际市场那个app可以看
  • 怎么做跳转不影响原网站排名wordpress添加邮箱设置
  • 网站内容规范外贸电商怎么做
  • 郑州做网站齿轮wordpress 文章h标签美化
  • 建设银行网站怎么修改手机号码吗网站建设怎样容易
  • 网站建设风险管理计划书户外媒体网站建设免费
  • 学到什么程度可以做网站网站维护的要求包括
  • 泉州网站设计平台南阳响应式网站
  • 阿里云 企业网站选哪种推广普通话的文字内容
  • 广州市南沙建设局网站中山建网站咨询电话
  • 怎么创建网站快捷方式网络服务器搭建配置与管理 下载