proc 下创建与应用交互的可读写节点
内核版本:Linux-4.14
随便写了个 proc 下节点的测试程序,可以用来与应用层交互。
也可以单独的用来做调试打印使用,例如封装个 my_printk 将信息单独存在节点内,然后可以在应用层 cat 出来查看。
#include <linux/uaccess.h>
#include <linux/proc_fs.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <asm/irq.h>
#include <asm/io.h> #define MSG_BUF_LEN 1024 static unsigned char msg_buf[MSG_BUF_LEN] = {};
static unsigned int msg_len = ;
static DECLARE_WAIT_QUEUE_HEAD(proc_msg_waitq); static ssize_t proc_msg_read(struct file *file, char __user *user_buf,
size_t count, loff_t *ppos)
{
unsigned int ret = ; /* 如果 msg_len 为 0,则等待数据 */
wait_event_interruptible(proc_msg_waitq, msg_len);
copy_to_user(user_buf, msg_buf, msg_len);
ret = msg_len;
msg_len = ; return ret;
} static ssize_t proc_msg_write(struct file *file, const char __user *user_buf,
size_t count, loff_t *ppos)
{
if(copy_from_user(msg_buf, user_buf, count)) {
return -EFAULT;
}
msg_len = count;
wake_up_interruptible(&proc_msg_waitq); return count;
} const struct file_operations proc_msg_operations = {
.read = proc_msg_read,
.write = proc_msg_write,
}; static int proc_msg_init(void)
{
/* 在 proc 下创建了 proc_msg */
proc_create("proc_msg", S_IRUSR, NULL, &proc_msg_operations); return ;
} static void proc_msg_exit(void)
{
remove_proc_entry("proc_msg", NULL);
} module_init(proc_msg_init);
module_exit(proc_msg_exit);
MODULE_LICENSE("GPL");
编译脚本如下:
KERN_DIR = /home/lance/nanopi/linux-4.14/ all:
make -C $(KERN_DIR) M=`pwd` modules
rm -rf *.o *.bak *.order *.mod.c *.symvers clean:
make -C $(KERN_DIR) M=`pwd` modules clean obj-m += proc_msg.o
安装模块测试:
/ # insmod proc_msg.ko
/ # echo hello > /proc/proc_msg
/ # cat /proc/proc_msg
hello
成功输出节点内信息。
proc 下创建与应用交互的可读写节点的更多相关文章
- centos环境下创建数据库和表的方法
centos环境下创建数据库和表的方法 //查询数据库的命令: mysql> SHOW DATABASES; +--------------------+ | Database ...
- Android下创建一个输入法
输入法是一种可以让用户输入文字的控件.Android提供了一套可扩展的输入法框架,使得应用程序可以让用户选择各种类型的输入法,比如基于触屏的键盘输入或者基于语音.当安装了特定输入法之后,用户即可在系统 ...
- 设备驱动基础学习--/proc下增加节点
在需要创建一个由一系列数据顺序组合而成的/proc虚拟文件或一个较大的/proc虚拟文件时,推荐使用seq_file接口. 数据结构struct seq_fille定义在include/linux/s ...
- 微软Azure 经典模式下创建内部负载均衡(ILB)
微软Azure 经典模式下创建内部负载均衡(ILB) 使用之前一定要注意自己的Azure的模式,老版的为cloud service模式,新版为ARM模式(资源组模式) 本文适用于cloud servi ...
- 无法在“EntityFramework”已存在的情况下创建影像复制该文件的解决方案
问题产生的原因:你项目正在生成中你就打开浏览器预览了,导致这个问题解决方案:右击重新生成项目,等生成后再打开 “/”应用程序中的服务器错误. 无法在“EntityFramework”已存在的情况下创建 ...
- Linux:Ubuntu16.04下创建Wifi热点
Linux:Ubuntu16.04下创建Wifi热点 说明: 1.Ubuntu16.04里面可以直接创建热点,而不用像以前的版本,还要其他辅助工具. 2.本篇文章转载自编程人生 具体步骤如下: 1. ...
- 在Mac下创建ASP.NET Core Web API
在Mac下创建ASP.NET Core Web API 这系列文章是参考了.NET Core文档和源码,可能有人要问,直接看官方的英文文档不就可以了吗,为什么还要写这些文章呢? 原因如下: 官方文档涉 ...
- 解决Oracle在scott用户下创建视图(VIEW)权限不足的方法
问题描述:在scott用户下创建视图的时候,报错:权限不足.(其他用户以此类推)解决方法: 以dba用户登录 sqlplus / as sysdba 赋予scott用户创建VIEW的权限 grant ...
- windows环境下创建 .文件夹
一.windows环境下创建 .文件夹 1.新建一个文件夹 2.重命名为.properties.(名字前后都加点) 二.windows环境下创建 .文件 1.上面的方法对文件同样适用 2.运行CMD, ...
随机推荐
- HTML5游戏引擎深度测评
https://zhuanlan.zhihu.com/p/20768495 最近看到网上一篇文章,标题叫做<2016年 最火的 15 款 HTML5 游戏引擎>.目前针对HTML5游戏的解 ...
- 面向IO编程--一切皆文件
in Unix, everything is a file.This simplifies the manipulation of data and devices into a set of cor ...
- speedscope + node inspect 分析node应用调用
生成一个简单的express 项目 使用脚手架工具 npm install -g express-generator@4 express . 启动使用inspect命令 node --inspect ...
- Xor-matic Number of the Graph-CodeForces - 724G
Xor-matic Number of the Graph-CodeForces - 724G 线性基棒题 建议做这题前先看看线性基的概念,然后A掉这道题--->路径最大异或和 这两个题都用到了 ...
- Flink入门介绍
什么是Flink Apache Flink是一个分布式大数据处理引擎,可以对有限数据流和无限数据流进行有状态计算.可部署在各种集群环境,对各种大小的数据规模进行快速计算. Flink特性 支持高吞吐. ...
- KAFKA && zookeeper 集群安装
服务器:#vim /etc/hosts10.16.166.90 sh-xxx-xxx-xxx-online-0110.16.168.220 sh-xx-xxx-xxx-online-0210.16.1 ...
- 【Gamma】Scrum Meeting 5
目录 写在前面 进度情况 任务进度表 照片 写在前面 例会时间:6.1 22:45-23:00 例会地点:微信群语音通话 代码进度记录github在这里 临近期末,团队成员课程压力均较大,需要较多时间 ...
- Spring Cloud 学习--Hystrix应用
上一篇介绍了Hystrix基本功能和单独使用的方式,今天继续学习如何将Hystrix融入SpringCloud组件中去. 在Ribbon上使用熔断器 在 pom.xml 文件中引入 hystrix 的 ...
- 安装tensorflow-gpu2.0(windows)
anaconda安装见前一篇https://www.cnblogs.com/wintersoft/p/11609188.html https://mirrors.tuna.tsinghua.edu.c ...
- Debian 9安装java与设置环境变量
安装默认JRE / JDK 先更新软件包索引: apt update 检查是否已安装Java: java -version 如果当前未安装Java,您将看到以下输出: Output-bash: jav ...