湟中县公司网站建设如何快速建一个网站
web/
2025/10/8 5:10:15/
文章来源:
湟中县公司网站建设,如何快速建一个网站,企业宣传册模板百度云,企业国际网站建设linux内核中存在一个信号SIGIO#xff0c;这个信号就是用于实现信号驱动IO的。当应用程序中想要以信号驱动IO的模型读写硬件数据时#xff0c;首先注册一个SIGIO信号的信号处理函数,当硬件数据就绪#xff0c;硬件会发起一个中断#xff0c;在硬件的中断处理函数中向当前进…linux内核中存在一个信号SIGIO这个信号就是用于实现信号驱动IO的。当应用程序中想要以信号驱动IO的模型读写硬件数据时首先注册一个SIGIO信号的信号处理函数,当硬件数据就绪硬件会发起一个中断在硬件的中断处理函数中向当前进程发送SIGIO信号此时进程捕获到SIGIO信号执行信号处理函数在信号处理函数中将准备好的硬件数据读走.
对于应用程序主程序的执行和SIGIO信号的发送的过程是一个异步的过程信号驱动IO是唯一一种异步IO。
异步操作是指在执行操作期间不会阻塞进程或线程的操作。在驱动开发中异步操作通常是通过使用工作队列、定时器、中断处理程序等机制来实现的。 驱动代码
#include linux/init.h
#include linux/module.h
#includelinux/fs.h
#includelinux/io.h
#includelinux/device.h
#includelinux/uaccess.h
#includelinux/poll.h
struct class *cls;
struct device *dev;
unsigned int major;//定义一个变量保存主设备号
char kbuf[128]{0};
struct fasync_struct *fapp;//定义一个异步对象指针
//封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{printk(%s:%s:%d\n,__FILE__,__func__,__LINE__);return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{printk(%s:%s:%d\n,__FILE__,__func__,__LINE__);if(sizesizeof(kbuf))//用户的需求内核满足不了{sizesizeof(kbuf);}long ret;retcopy_to_user(ubuf,kbuf,size);if(ret){printk(copy_to_user filed\n);return -EIO;}return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{printk(%s:%s:%d\n,__FILE__,__func__,__LINE__);if(sizesizeof(kbuf))//用户的需求内核满足不了{sizesizeof(kbuf);}long ret;retcopy_from_user(kbuf,ubuf,size);//表示模拟硬件数据就绪if(ret){printk(copy_from_user filed\n);return -EIO;}//发送信号kill_fasync(fapp,SIGIO,POLL_IN);return 0;
}
//封装fasync操作方法
int mycdev_fasync(int fd, struct file * file, int on)
{//完成发生信号之前的准备工作fasync_helper(fd,file,on,fapp);return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{printk(%s:%s:%d\n,__FILE__,__func__,__LINE__);return 0;
}
//定义一个操作方法结构体变量并且初始化
struct file_operations fops{.openmycdev_open,.releasemycdev_close,.readmycdev_read,.fasyncmycdev_fasync,.writemycdev_write,
};
static int __init mycdev_init(void)
{//注册字符设备驱动majorregister_chrdev(0,mychrdev,fops);if(major0){printk(注册字符设备驱动失败\n);return major;}printk(注册字符设备驱动成功major%d\n,major);// 向上提交目录cls class_create(THIS_MODULE, myled);if (IS_ERR(cls)){printk(向上提交目录失败\n);return -PTR_ERR(cls);}printk(向上提交目录信息成功\n);// 向上提交设备节点信息dev device_create(cls, NULL, MKDEV(major, 0), NULL, mycdev);if (IS_ERR(dev)){printk(向上提交设备节点信息失败\n);return -PTR_ERR(dev);}printk(向上提交设备节点成功\n);return 0;
}
static void __exit mycdev_exit(void)
{// 销毁节点信息device_destroy(cls, MKDEV(major, 0));// 销毁目录信息class_destroy(cls);//注销字符设备驱动unregister_chrdev(major,mychrdev);}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE(GPL);应用程序-读数据
#include stdio.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include unistd.h
#include stdlib.h
#include string.h
#include sys/wait.h
#include sys/ioctl.h
#include sys/select.h
#include sys/epoll.h
#include signal.h
/* According to earlier standards */
#include sys/time.hchar buf[128] {0};
int fd;
// 定义信号处理函数
void sigio_handler(int sig)
{// 读取硬件数据read(fd, buf, sizeof(buf));printf(buf:%s\n, buf);
}
int main(int argc, char const *argv[])
{// 打开文件fd open(/dev/mycdev, O_RDWR);if (fd 0){printf(打开设备文件失败\n);exit(-1);}// 注册SIGIO的信号处理函数signal(SIGIO, sigio_handler);// 回调驱动中的fasync方法完成驱动中发生信号之前的准备工作int flags fcntl(fd, F_GETFL); // 获取文件描述符的相关属性fcntl(fd, F_SETFL, flags | FASYNC); // 当文件描述符中有FASYNC这个标志时驱动中fasync方法就会被调用// 设置文件描述符fd对应的驱动发生SIGIO信号只发送给当前进程fcntl(fd, F_SETOWN, getpid());while (1){printf(aaaaa\n);sleep(1);}return 0;
}应用程序-模拟中断
#includestdlib.h
#includestdio.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#includeunistd.h
#includestring.hint main(int argc, char const *argv[])
{int a,b;char buf[128]hello world;int fdopen(/dev/mycdev,O_RDWR);if(fd0){printf(打开设备文件失败\n);exit(-1);}write(fd,buf,sizeof(buf));close(fd);return 0;
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/88885.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!