2
votes

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

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 echo shell command: 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

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.

3
votes

You have to create a device file in /dev if it's not already automatically created. Then you can write a program that opens and reads from/writes to that file.