简体   繁体   English

在LINUX中检查简单的char设备读/写功能

[英]Checking simple char device read/write functions in LINUX

I am new to linux kernel programming. 我是Linux内核编程的新手。 I wrote a simple kernel module and char device. 我写了一个简单的内核模块和char设备。 I defined the open(), release(), read() and write() methods of device. 我定义了设备的open(),release(),read()和write()方法。 I initialize my module with insmod and removed it with rmmod and all works correct. 我使用insmod初始化我的模块,并使用rmmod将其删除,所有工作均正常。 Now I want to check my read() write() methods of the device. 现在,我要检查设备的read()write()方法。 Could you please tell me how to write a user program which should implement the read/write methods of my char device ? 您能否告诉我如何编写一个应实现char设备的读/写方法的用户程序? Thank you. 谢谢。

You have to create a device file in /dev if it's not already automatically created. 如果尚未自动创建设备文件,则必须在/dev创建它。 Then you can write a program that opens and reads from/writes to that file. 然后,您可以编写一个程序,该程序可以打开并读取/写入该文件。

The first test you can do when you have a character device and you want to check your implementation of read and write syscalls is to: 当你有一个字符设备,并要检查您的实现,你可以做的第一测试readwrite系统调用是:

  • write with the echo shell command: echo 42 > /dev/char_device 使用echo shell命令编写: echo 42 > /dev/char_device
  • read with the cat command or a specified number of bytes with the head command (or with dd ) and convert to hexadecimal with od -x if necessary: head -8 /dev/char_device | od -x 使用cat命令或使用head命令(或使用dd )读取指定字节数,并在必要时使用od -x转换为十六进制: head -8 /dev/char_device | od -x head -8 /dev/char_device | od -x

Now to write a program in C, just use fopen to open the file and use fread and fwrite to read and write data; 现在用C编写程序,只需使用fopen打开文件,并使用freadfwrite读取和写入数据; you can also use read and write syscalls, but fread and fwrite are standard C library functions that wrap read and write . 你也可以使用readwrite系统调用,但freadfwrite是包装标准C库函数readwrite

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM