创建 UNIX“特殊字符”文件
假设我想本着 /dev/zero 的精神创建一个文件 /dev/7 ,每当读取该文件时都会生成字符“7”。我应该如何去做这样的事情?我需要修改内核吗?
Suppose I want to create, in the spirit of /dev/zero, a file /dev/seven that produces the character '7' whenever it is read from. How should I go about doing something like this? Would I need to modify the kernel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,您需要为该特殊字符设备创建一个驱动程序。
对于 Linux,我建议您阅读 Jonathan Corbet、Alessandro Rubini 和 Greg Kroah 撰写的 Linux 设备驱动程序 -哈特曼。 (第 3 章讨论了 char 驱动程序,但至少也要阅读前两章。)
Yes, you'd need to create a driver for that special character device.
For linux, I'd suggest you read Linux Device Drivers by Jonathan Corbet, Alessandro Rubini, and Greg Kroah-Hartman. (Chapter 3 talks about char drivers, but do read at least the first two chapters also.)
设备驱动程序不是必需的,一个 fifo 特殊文件加上生成
7
流的用户程序就完全能够提供此行为。总而言之,您将需要大约五行 shell 脚本。
A device driver is not necessary, a fifo special file plus a user program generating the stream of
7
is perfectly able to provide this behavior.You'll need about a five line shell script, all told.
这里有一个为 Linux 创建设备驱动程序的简短示例,可能有助于您入门:
There's a short example of creating a device driver for Linux here, which might help to get you started: