Karen Tsirunyan
Karen Tsirunyan

Reputation: 1998

Checking simple char device read/write functions in LINUX

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.

Upvotes: 2

Views: 14007

Answers (2)

ouah
ouah

Reputation: 145829

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.

Upvotes: 4

Some programmer dude
Some programmer dude

Reputation: 409176

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.

Upvotes: 3

Related Questions