I am new to linux kernel programming. I wrote a simple kernel module and char device. I defined the open(), release(), read() and write() methods of device. I initialize my module with insmod and removed it with rmmod and all works correct. Now I want to check my read() write() methods of the device. Could you please tell me how to write a user program which should implement the read/write methods of my char device ? Thank you.
2 Answers
4
votes
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:
- write with the
echoshell command:echo 42 > /dev/char_device - read with the
catcommand or a specified number of bytes with theheadcommand (or withdd) and convert to hexadecimal withod -xif necessary: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; you can also use read and write syscalls, but fread and fwrite are standard C library functions that wrap read and write.