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

做网站比较好的软件百度引擎提交入口

做网站比较好的软件,百度引擎提交入口,wordpress自定义登录界面,html网站素材linux内核驱动——字符设备实现两个终端单向收发 参考 Ubuntu18.04添加内核模块&#xff08;字符设备&#xff09; 创建内核驱动文件chat_dev.c&#xff1a; #include <linux/init.h> //定义了module_init #include <linux/module.h> //最基本的头文件&a…

linux内核驱动——字符设备实现两个终端单向收发

参考

Ubuntu18.04添加内核模块(字符设备)

  • 创建内核驱动文件chat_dev.c
#include <linux/init.h>     //定义了module_init
#include <linux/module.h>   //最基本的头文件,其中定义了MODULE_LICENSE这一类宏
#include <linux/fs.h>       // file_operations结构体定义在该头文件中
#include <linux/device.h>    //class、device结构体的定义位置
#include <linux/sched/signal.h>
#include <linux/uaccess.h>    // copy_to_user & copy_from_user 
#include <linux/mutex.h>static const char* device_name = "chat_driver";  //  定义设备名
static const char* device_path = "chat";  //  定义设备文件名  /dev/chat
static struct class *chat_class;    //定义class结构体
static struct device *chat_dev;    //定义device结构体//定义了open函数
static int chat_drv_open (struct inode *inode, struct file *file)
{printk("open\n");return 0;
}static char words[1000];  //聊天内容缓存
static int words_cnt = 0;  //聊天内容缓存字节数
static struct mutex words_mutex; //互斥锁
//read函数
static ssize_t chat_drv_read(struct file *file, char __user *buf, size_t size, loff_t *fpos) {printk("read begin\n");ssize_t cnt = 0;  //返回值,表示读取字节长度mutex_lock(&words_mutex);  //上锁if(words_cnt > 0){copy_to_user(buf, words, words_cnt); //数据拷贝到用户态printk("words_cnt=%d\n", words_cnt);cnt = words_cnt;words_cnt = 0;  //读取后缓存清空}mutex_unlock(&words_mutex); //解锁printk("read finish\n");return cnt;
}//定义了write函数
static ssize_t chat_drv_write (struct file *file, const char __user *buf, size_t size, loff_t * ppos)
{printk("write begin\n");mutex_lock(&words_mutex); //上锁copy_from_user(words, buf, size);  //从用户态拷贝数据words_cnt = size;       //更新缓存字节长度printk("words_cnt=%d\n", words_cnt);printk("words=%s\n", words);mutex_unlock(&words_mutex); //解锁printk("write finish\n");return 0;
}//在file_operations中注册open和write函数
static struct file_operations chat_drv_fo =
{.owner  =  THIS_MODULE,//将对应的函数关联在file_operations的结构体中.open   =  chat_drv_open,    .read    = chat_drv_read,  .write  =  chat_drv_write,
};static int dev_id = 0;     //初始化的设备号0
//init驱动的入口函数
static int __init chat_drv_init(void)
{      //注册设备,实际是将file_operations结构体放到内核的制定数组中,以便管理//在register_chrdev中制定dev_id作为主设备号,若dev_id为0则自动分配一个主设备号dev_id = register_chrdev(dev_id, device_name , &chat_drv_fo);chat_class = class_create(THIS_MODULE, device_path);    //初始化class结构体,指定设备文件名chat_dev = device_create(chat_class, NULL, MKDEV(dev_id, 0), NULL, device_path);// 根据class来初始化device,会创建出对应的设备文件mutex_init(&words_mutex);printk("init\n");return 0;
}//驱动的出口函数
static void __exit chat_drv_exit(void)
{printk("exit\n");unregister_chrdev(dev_id, device_name);  //卸载设备,实际是将file_operations结构体从内核维护的相关数组中以主设备号作为索引删除device_unregister(chat_dev); // 后创建的先卸载class_destroy(chat_class);
}//内核将通过这个宏,来直到这个驱动的入口和出口函数
module_init(chat_drv_init);  
module_exit(chat_drv_exit);MODULE_LICENSE("GPL");  //指定协议
  • 编译并加载内核驱动,详细可以看参考

  • 创建发送端测试程序write.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>int main(int argc, char **argv)
{int fd;char words[1000];fd = open("/dev/chat",  O_RDWR);  //根据设备描述符打开设备if(fd < 0)          //打开失败printf("can't open\n");  while(scanf("%s", words))  //可以自己输入{size_t len = strlen(words);words[len] = '\0';// printf("%s\n", words);write(fd, words, len+1);  //根据文件描述符调用write}return 0;
}
  • 创建接收端测试程序read.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>int main(int argc, char **argv)
{int fd;char words[1000];fd = open("/dev/chat",  O_RDWR);  //根据设备描述符打开设备if(fd < 0)          //打开失败printf("can't open\n");  while(1){int ret = read(fd, words, 5);  //根据文件描述符调用write,这里的5实际上没有用处if(ret > 0)printf("%s\n", words); sleep(1); }return 0;
}
  • 编译两个测试程序,并分别在两个终端运行:
gcc write.c -o write
gcc read.c -o read
  • 结果如下:
$ sudo ./write
nihao
qpwoeiruty
12222222222222rrrrrrrrrrrrrrrrrrrrrrrrrrrr
$ sudo ./read
nihao
qpwoeiruty
12222222222222rrrrrrrrrrrrrrrrrrrrrrrrrrrr
http://www.dt0577.cn/news/6725.html

相关文章:

  • 网站建设与推广方案模板打开百度一下搜索
  • 中国建筑网官网人员名单刷神马seo排名首页排名
  • 网站开发方案网站搭建公司
  • 如何做网站推广北京网站建设公司案例
  • 孝感个人网站建设aso优化服务平台
  • 网站建设模板平台淘宝一个关键词要刷多久
  • 初创企业的建站流程搜索引擎外部优化有哪些渠道
  • 免费域名注册解析seo排名需要多少钱
  • 做网站自己有模板要花多少钱营销策略分析
  • 电子元器件网站建设湖南seo优化推荐
  • 红河网站建设营销软件网
  • 网页布局照着别的网站做会侵权吗如何自己做推广
  • 有什么网站可以免费建站免费建网站成人教育培训机构
  • 哈尔滨专业官网建站企业2022年五月份热点事件
  • 做企业网站的第一步需要啥网站建设案例
  • 用java做网页如何建立网站百度一下百度首页登录
  • 网站建设的面试要求域名估价
  • web前端网页设计报告黑帽seo寄生虫
  • 广州做网站企业三只松鼠网络营销案例分析
  • 网站制作中心微信公众号小程序怎么做
  • 长图制作网站网址收录大全
  • 使用wordpress需要懂什么语言seo专业学校
  • 展示型网站与营销型网站产品线上推广渠道
  • 网页界面设计的构成要素网站外链的优化方法
  • 怎么做一个网站 免费seo研究中心怎么了
  • 自做刷赞网站网站排名怎么做
  • 甘肃网站建设网站制作外链网盘网站
  • 中企动力做网站好吗百度联盟点击广告赚钱
  • 成都网站建设开发好用搜索引擎排名
  • 江门市建设工程投标网站百度网站安全检测