linux设备文件
一、前言
在调用了alloc_chrdev_region函数或register_chrdev_region函数之后可以在/proc/devices中看到该设备的主设备号,比如我注册的hello模块的主设备号为1024,如下图:

现在使用lsmod能看到驱动名,使用cat /proc/devices能看到设备名,那怎么来使用这个设备呢,这个时候我们还需要一个设备文件,这个设备文件就是在应用程序中用open函数打开的文件。
二、创建设备文件
方法一:手动创建
使用mknod指令,mknod用法:mknod <filename> <type> <major> <minor>
filename:设备文件名
type:设备类型
major:主设备号
minor:次设备号
如:mknod /dev/haha c 1024 0
这就是说创建了一个/dev/haha的文件,类型是字符设备,主设备号为1024,次设备号为0
方法二:自动创建
三、struct file
有了设备文件之后,便可以用open打开这个设备文件,然后用read,write等函数来操作这个文件,但是在用户态中怎么调用到内核的东西呢。
系统每打开一个文件在内核空间都有一个相关联的struct file,它由内核在打开文件时创建,在关闭文件后释放。
struct file的定义如下:
1 struct file {
2 union {
3 struct llist_node fu_llist;
4 struct rcu_head fu_rcuhead;
5 } f_u;
6 struct path f_path;
7 struct inode *f_inode; /* cached value */
8 const struct file_operations *f_op;
9
10 /*
11 * Protects f_ep_links, f_flags.
12 * Must not be taken from IRQ context.
13 */
14 spinlock_t f_lock;
15 atomic_long_t f_count;
16 unsigned int f_flags;
17 fmode_t f_mode;
18 struct mutex f_pos_lock;
19 loff_t f_pos;
20 struct fown_struct f_owner;
21 const struct cred *f_cred;
22 struct file_ra_state f_ra;
23
24 u64 f_version;
25 #ifdef CONFIG_SECURITY
26 void *f_security;
27 #endif
28 /* needed for tty driver, and maybe others */
29 void *private_data;
30
31 #ifdef CONFIG_EPOLL
32 /* Used by fs/eventpoll.c to link all the hooks to this file */
33 struct list_head f_ep_links;
34 struct list_head f_tfile_llink;
35 #endif /* #ifdef CONFIG_EPOLL */
36 struct address_space *f_mapping;
37 } __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */
其中有一个重要成员就是 const struct file_operations *f_op;
struct file_operations的定义如下:
1 struct file_operations {
2 struct module *owner;
3 loff_t (*llseek) (struct file *, loff_t, int);
4 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
5 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
6 ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
7 ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
8 int (*iterate) (struct file *, struct dir_context *);
9 unsigned int (*poll) (struct file *, struct poll_table_struct *);
10 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
11 long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
12 int (*mmap) (struct file *, struct vm_area_struct *);
13 int (*open) (struct inode *, struct file *);
14 int (*flush) (struct file *, fl_owner_t id);
15 int (*release) (struct inode *, struct file *);
16 int (*fsync) (struct file *, loff_t, loff_t, int datasync);
17 int (*aio_fsync) (struct kiocb *, int datasync);
18 int (*fasync) (int, struct file *, int);
19 int (*lock) (struct file *, int, struct file_lock *);
20 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
21 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
22 int (*check_flags)(int);
23 int (*flock) (struct file *, int, struct file_lock *);
24 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
25 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
26 int (*setlease)(struct file *, long, struct file_lock **, void **);
27 long (*fallocate)(struct file *file, int mode, loff_t offset,
28 loff_t len);
29 void (*show_fdinfo)(struct seq_file *m, struct file *f);
30 #ifndef CONFIG_MMU
31 unsigned (*mmap_capabilities)(struct file *);
32 #endif
33 };
这里面就包含了打开文件之后可以对文件的操作,在用open打开一个文件之后获得一个文件描述符fd,比如要对该文件进行写操作,则调用write函数,实际上会调用到 file_operations中的read函数,而在内核驱动中是需要自己编写read函数的,这样就能实现应用和内核之间的交互。
linux设备文件的更多相关文章
- (转载)使用 udev 高效、动态地管理 Linux 设备文件
概述: Linux 用户常常会很难鉴别同一类型的设备名,比如 eth0, eth1, sda, sdb 等等.通过观察这些设备的内核设备名称,用户通常能知道这些是什么类型的设备,但是不知道哪一个设备是 ...
- 嵌入式 使用udev高效、动态地管理Linux 设备文件
本文以通俗的方法阐述 udev 及相关术语的概念.udev 的配置文件和规则文件,然后以 Red Hat Enterprise Server 为平台演示一些管理设备文件和查询设备信息的实例.本文会使那 ...
- 【转】使用 udev 高效、动态地管理 Linux 设备文件
简介: 本文以通俗的方法阐述 udev 及相关术语的概念.udev 的配置文件和规则文件,然后以 Red Hat Enterprise Server 为平台演示一些管理设备文件和查询设备信息的实例.本 ...
- 使用 udev 高效、动态地管理 Linux 设备文件
本文转自:https://www.ibm.com/developerworks/cn/linux/l-cn-udev/index.html 概述: Linux 用户常常会很难鉴别同一类型的设备名,比如 ...
- 使用 udev 管理 Linux 设备文件
本文以通俗的方法阐述 udev 及相关术语的概念.udev 的配置文件和规则文件,然后以 Red Hat Enterprise Server 为平台演示一些管理设备文件和查询设备信息的实例.本文会使那 ...
- Linux设备文件简介(转载)
Linux 中的设备有2种类型:字符设备(无缓冲且只能顺序存取).块设备(有缓冲且可以随机存取).每个字符设备和块设备都必须有主.次设备号,主设备号相同的设 备是同类设备(使用同一个驱动程序).这些设 ...
- Linux设备文件自动生成
第一种是使用mknod手工创建:# mknod <devfilename> <devtype> <major> <minor> 第二种是自动创建设备节点 ...
- Linux设备文件三大结构:inode,file,file_operations
驱动程序就是向下控制硬件,向上提供接口,这里的向上提供的接口最终对应到应用层有三种方式:设备文件,/proc,/sys,其中最常用的就是使用设备文件,而Linux设备中用的最多的就是字符设备,本文就以 ...
- linux 设备文件和设备之间联系的建立
<设备驱动模型> 注:几乎所有的设备结构体都包含"strcut kobject kobj"和"srtuct list_head list"该结构体 ...
- Linux设备文件简介
转:http://www.360doc.com/content/11/0418/00/5087210_110410837.shtml 版权声明 本 文作者是一位自由软件爱好者,所以本文虽然不是软件,但 ...
随机推荐
- sqli-labs第5关布尔盲注pyhton脚本
import requests import os #此函数先判断数据库长度 def length(url,str): num = 1 while True: str_num = '%d' %num ...
- 推荐系统实践 0x12 什么是Embedding
做过深度学习的小伙伴,大家应该多多少少都听说过Embedding,这么火的Embedding到底是什么呢?这篇文章就用来介绍Embedding.另外,基于深度学习的推荐系统方法或者论文还没有结束,我打 ...
- Python编程学习爆破一些简单加密的文件
前言 这里只是分享一下Python如何生成爆破字典,对于简单的一些加密文件,咱们可以跑字典获取正确的密码,比如zip加密文件.这里小编给大家简单的介绍一下爆破字典的方法,希望对大家学习Python有帮 ...
- Git 仓库拆分
方案对比 subtree 使用命令 git subtree split -P dirPath -b branchName 将目标文件夹的代码都保存到指定分支.试了下,该方案虽然保留了 commit,但 ...
- Cannot assign requested address 和 SO_REUSEADDR 参数
1. 错误 今天项目中出现了大量的java.net.ConnectException: Cannot assign requested address (connect failed) 错误. 刚开始 ...
- 152. Maximum Product Subarray动态规划连乘最大子串
Find the contiguous subarray within an array (containing at least one number)which has the largest p ...
- Mac Arduino ESP8266 ESP32 搭建开发环境
目录 1.安装Arduino 2.搭建开发板管理器 3.可能出现的错误 1.安装Arduino Arduino下载. 官方下载地址:Arduino官方网站 Arduino中文社区:下载地址 安装方式: ...
- stm32学习笔记之SysTick的思考
原文来自--SevenZ的笔记.http://blog.21ic.com/user1/8247/archives/2011/85920.html ? 首先我们要明白什么是SysTick定时器? Sys ...
- Kali实现靶机远程控制
任务一.使用netcat建立监听并连接 1. 在ubuntu开启本地监听,在kali中连接 2. 在kali中开启监听,在ubuntu中反弹连接 任务二.使用met ...
- Java高并发与多线程(一)-----概念
其实之前一直想专门写一篇,单独说一说Java的多线程与高并发,但是一直以来,都没有想到能够用什么比较有趣的表现形式去表达出来,而且网上充斥着很多类似的博客,有好的又不好的,有简介的有繁琐的,所以也一直 ...