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

商标转让价格一般多少钱seo技术顾问阿亮

商标转让价格一般多少钱,seo技术顾问阿亮,李笑来做的一个网站,丰台企业网站建设文章目录 非阻塞 IO(NIO)模型驱动程序应用程序模块使用 非阻塞 IO(NIO) 上一节中 https://blog.csdn.net/tyustli/article/details/135140523,使用等待队列头实现了阻塞 IO 程序使用时,阻塞 IO 和非阻塞 IO 的区别在于文件打开的时候是否使用了 O_NONB…

文章目录

  • 非阻塞 IO(NIO)
    • 模型
    • 驱动程序
    • 应用程序
    • 模块使用

非阻塞 IO(NIO)

上一节中 https://blog.csdn.net/tyustli/article/details/135140523,使用等待队列头实现了阻塞 IO

程序使用时,阻塞 IO 和非阻塞 IO 的区别在于文件打开的时候是否使用了
O_NONBLOCK 标志位。

  • O_RDWR 默认以阻塞的方式打开
  • O_NONBLOCK 以非阻塞的方式打开

模型

在这里插入图片描述

驱动程序

文件打卡时,文件打开标志存放在文件结构体 struct file f_flags 字段
文件结构体原型在 linux/fs.h 文件中

struct file {union {struct llist_node	f_llist;struct rcu_head 	f_rcuhead;unsigned int 		f_iocb_flags;};/** Protects f_ep, f_flags.* Must not be taken from IRQ context.*/spinlock_t		f_lock;fmode_t			f_mode;atomic_long_t		f_count;struct mutex		f_pos_lock;loff_t			f_pos;unsigned int		f_flags;struct fown_struct	f_owner;const struct cred	*f_cred;struct file_ra_state	f_ra;struct path		f_path;struct inode		*f_inode;	/* cached value */const struct file_operations	*f_op;u64			f_version;
#ifdef CONFIG_SECURITYvoid			*f_security;
#endif/* needed for tty driver, and maybe others */void			*private_data;#ifdef CONFIG_EPOLL/* Used by fs/eventpoll.c to link all the hooks to this file */struct hlist_head	*f_ep;
#endif /* #ifdef CONFIG_EPOLL */struct address_space	*f_mapping;errseq_t		f_wb_err;errseq_t		f_sb_err; /* for syncfs */
} __randomize_layout__attribute__((aligned(4)));	/* lest something weird decides that 2 is OK */

所以驱动中需要判断文件打开标志是否支持非阻塞方式

    new_chrdev_t *dev = (new_chrdev_t *)file->private_data;if (file->f_flags & O_NONBLOCK) {if (!dev->flag) {return -EAGAIN;}}

驱动程序源码

#include "asm-generic/errno-base.h"
#include "linux/device/class.h"
#include "linux/export.h"
#include "linux/uaccess.h"
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/wait.h>#define CHRDEVBASE_NAME "chrdevbase" /* 设备名 */
#define CHRDEVBASE_NUM 1             /* 设备数目 */static char *string_test = "kernel data this tyustli test";typedef struct {dev_t dev_id;          /* 设备号 */struct cdev c_dev;     /* cdev */struct class *class;   /* 类 */struct device *device; /* 设备 */int major;             /* 主设备号 */int minor;             /* 次设备号 */int flag;              /* read flag */char write_buf[100];char read_buf[100];wait_queue_head_t read_wait; /* 读等待队列头 */
} new_chrdev_t;static new_chrdev_t new_chrdev1;static int chrdevbase_open(struct inode *inode, struct file *file)
{file->private_data =container_of(inode->i_cdev, new_chrdev_t, c_dev); /* 设置私有数据 */printk("k: chrdevbase open\r\n");return 0;
}static ssize_t chrdevbase_read(struct file *file, char __user *buf,size_t count, loff_t *ppos)
{unsigned long ret = 0;new_chrdev_t *dev = (new_chrdev_t *)file->private_data;if (file->f_flags & O_NONBLOCK) {if (!dev->flag) {return -EAGAIN;}}wait_event_interruptible(dev->read_wait, dev->flag);dev->flag = 0;memcpy(dev->read_buf, string_test, strlen(string_test));ret = copy_to_user(buf, dev->read_buf, count);if (ret == 0) {printk("k: read data success\r\n");} else {printk("k: read data failed ret = %ld\r\n", ret);}return ret;
}static ssize_t chrdevbase_write(struct file *file, const char __user *buf,size_t count, loff_t *ppos)
{unsigned long ret = 0;new_chrdev_t *dev = (new_chrdev_t *)file->private_data;ret = copy_from_user(dev->write_buf, buf, count);if (ret == 0) {printk("k: write data success write data is: %s\r\n", dev->write_buf);} else {printk("k: write data failed ret = %ld\r\n", ret);}dev->flag = 1;wake_up_interruptible(&dev->read_wait);return count;
}static int chrdevbase_release(struct inode *inode, struct file *file)
{printk("k: chrdevbase release\r\n");return 0;
}static struct file_operations chrdevbase_fops = {.owner = THIS_MODULE,.open = chrdevbase_open,.read = chrdevbase_read,.write = chrdevbase_write,.release = chrdevbase_release,
};static int __init chrdevbase_init(void)
{int err = 0;err = alloc_chrdev_region(&new_chrdev1.dev_id, 0, CHRDEVBASE_NUM,CHRDEVBASE_NAME);if (err < 0) {printk("k: alloc chrdev region failed err = %d\r\n", err);goto err_chrdev;}/* get major 1 and minor 1 */new_chrdev1.major = MAJOR(new_chrdev1.dev_id);new_chrdev1.minor = MINOR(new_chrdev1.dev_id);printk("k: newcheled major=%d,minor=%d\r\n", new_chrdev1.major,new_chrdev1.minor);new_chrdev1.c_dev.owner = THIS_MODULE;cdev_init(&new_chrdev1.c_dev, &chrdevbase_fops);err = cdev_add(&new_chrdev1.c_dev, new_chrdev1.dev_id, 1);if (err < 0) {printk("k: cdev add failed err = %d\r\n", err);goto err_cdev_add;}new_chrdev1.class = class_create("chr_test1");if (IS_ERR(new_chrdev1.class)) {err = PTR_ERR(new_chrdev1.class);goto err_class_create;}new_chrdev1.device = device_create(new_chrdev1.class, NULL,new_chrdev1.dev_id, NULL, "chr_test1");if (IS_ERR(new_chrdev1.device)) {err = PTR_ERR(new_chrdev1.device);goto err_device_create;}/* 初始化等待队列头 */init_waitqueue_head(&new_chrdev1.read_wait);new_chrdev1.flag = 0;printk("k: base module init\r\n");return 0;err_device_create:class_destroy(new_chrdev1.class);
err_class_create:cdev_del(&new_chrdev1.c_dev);
err_cdev_add:unregister_chrdev_region(new_chrdev1.dev_id, CHRDEVBASE_NUM);
err_chrdev:return err;
}static void __exit chrdevbase_exit(void)
{device_destroy(new_chrdev1.class, new_chrdev1.dev_id);class_destroy(new_chrdev1.class);cdev_del(&new_chrdev1.c_dev);unregister_chrdev_region(new_chrdev1.dev_id, CHRDEVBASE_NUM);printk("k: base module exit!\r\n");
}module_init(chrdevbase_init);
module_exit(chrdevbase_exit);MODULE_LICENSE("GPL");
MODULE_AUTHOR("tyustli");
MODULE_INFO(intree, "Y"); /* loading out-of-tree module taints kernel */

应用程序

打开文件时使用 O_NONBLOCK 非阻塞方式打开文件

fd = open(filename, O_RDWR | O_NONBLOCK);

如果文件读取失败,循环读取

        retvalue = -1;while (retvalue < 0) {retvalue = read(fd, readbuf, 50);if (retvalue < 0) {printf("u: read file %s failed!\r\n", filename);sleep(3);} else {/*  读取成功,打印出读取成功的数据 */printf("u: read data:%s\r\n", readbuf);}}

完整应用程序代码

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"static char usrdata[] = { "user data!" };int main(int argc, char *argv[])
{int fd, retvalue;char *filename;char readbuf[100], writebuf[100];if (argc != 3) {printf("u: error Usage!\r\n");return -1;}filename = argv[1];/* 打开驱动文件 */fd = open(filename, O_RDWR | O_NONBLOCK);if (fd < 0) {printf("u: can't open file %s\r\n", filename);return -1;}/* 从驱动 文件读取数据 */if (atoi(argv[2]) == 1) {retvalue = -1;while (retvalue < 0) {retvalue = read(fd, readbuf, 50);if (retvalue < 0) {printf("u: read file %s failed!\r\n", filename);sleep(3);} else {/*  读取成功,打印出读取成功的数据 */printf("u: read data:%s\r\n", readbuf);}}}/* 向设备驱动写数据 */if (atoi(argv[2]) == 2) {memcpy(writebuf, usrdata, sizeof(usrdata));retvalue = write(fd, writebuf, 50);if (retvalue < 0) {printf("u: write file %s failed!\r\n", filename);}}/* 关闭设备 */retvalue = close(fd);if (retvalue < 0) {printf("u: can't close file %s\r\n", filename);return -1;}return 0;
}

模块使用

模块安装

modprobe my_module

查看设备节点

ls /dev
~ # ls /dev/
chr_test1        ptypc            tty32            tty7
console          ptypd            tty33            tty8
cpu_dma_latency  ptype            tty34            tty9
full             ptypf            tty35            ttyAMA0
gpiochip0        random           tty36            ttyAMA1
gpiochip1        root             tty37            ttyAMA2
gpiochip2        rtc0             tty38            ttyAMA3
gpiochip3        snd              tty39            ttyp0
hwrng            tty              tty4             ttyp1

模块使用

~ # /lib/modules/6.5.7+/my_app /dev/chr_test1 1 &
~ # k: chrdevbase open
u: read file /dev/chr_test1 failed!
u: read file /dev/chr_test1 failed!
~ # /lib/modules/6.5.7+/my_app /dev/chr_test1 2
k: chrdevbase open
k: write data success write data is: user data!
k: chrdevbase release
~ # k: read data success
k: chrdevbase release
u: read data:kernel data this tyustli test[1]+  Done                       /lib/modules/6.5.7+/my_app /dev/chr_test1 1
~ # 
http://www.zhongyajixie.com/news/25833.html

相关文章:

  • 在外汇局网站做登记报告必应搜索引擎怎么样
  • 跨境平台哪个最好做?seo外链建设的方法有
  • 怎样做公司的网站首页seo优化方式包括
  • 网站开发需求问卷新乡网站推广
  • 网站正能量不用下载直接进入主页可以吗b站推广网站2024年
  • 在国外做网站赌博犯法吗百度推广多少钱一天
  • 做加盟的网站建设杭州网站推广与优化
  • 怎么区分营销型网站某产品网络营销推广方案
  • 用django怎么做网站重庆seo排名软件
  • 北京度seo排名小红书seo是什么
  • 公司网站首页制作教程郑州网站设计有哪些
  • 网站建设APP的软件南宁网络推广培训机构
  • 新余哪有做网站的公司网络营销案例
  • vs中做网站怎么设置图片标准国际最新消息
  • 做特卖的网站国外搜索网站排名
  • 外贸网站logo搜外友链
  • jsp动态网站开发参考文献软件开发工资一般多少
  • 如何制作奶茶 简单seo视频教程我要自学网
  • 电脑装机网站搜外滴滴友链
  • 微网站 方案微信软文范例
  • 一级造价师注册查询系统平台入口厦门seo蜘蛛屯
  • 杭州app开发价格表seo优化员
  • 学做宝宝衣服的网站日本今日新闻头条
  • 抖音代运营服务项目内容优化大师下载
  • 网站推广的方法搜索引擎我的百度账号登录
  • 代做道具网站写一篇软文多少钱
  • 用代码做家乡网站沈阳网站优化
  • 湖南网站建设的公司seo建站优化推广
  • api网站网址大全热门关键词
  • 天津住房和城乡建设委员会网站附近广告公司