views:

257

answers:

2

I'm currently writing up some CPPunit tests for a program that tests a hardware communication port (yes, I'm writing unit tests for a tester app ;-) ). One of the classes I'm testing is basically a wrapper around the device's file descriptor; I make read() and write() calls on the file descriptor. Is there a device file/driver on Linux where if you write() to it, when you read() from it you will read back what you wrote (like a FIFO queue device)? If I had this device file, it would be really simple to plop that in place of the actual device file in my wrapper and would make it easy to emulate "loopback" scenarios that might occur on real-world hardware.


Essentially what I'm looking for would be a sort of hypothetical "/dev/echo" device.

Ex: The device would behave as follows:

open("/dev/echo", O_RDRW);

write(fdEcho, 123, sizeof(int));
write(fdEcho, 456, sizeof(int));
write(fdEcho, 789, sizeof(int));

read(fdEcho, iData,  sizeof(int);  // returns 123
read(fdEcho, iData2, sizeof(int);  // returns 456
read(fdEcho, iData3, sizeof(int);  // returns 789
+4  A: 

Why don't you use a UNIX pipe in the filesystem?

mkfifo /dev/test
echo test > /dev/test

on a second terminal:

cat /dev/test

Then the first will unblock and the second will show the text!

Christian
+1 ... didn't see you there
David Zaslavsky
+2  A: 

You can make one with

mknod /dev/echo p

First argument is the filename (it can be any filename, doesn't have to be in /dev), second argument is p for "pipe".

Reference: http://www.unix.com/unix-dummies-questions-answers/22205-unix-pipe.html

You could also look into the pipe() C function: http://www.manpagez.com/man/2/pipe/

David Zaslavsky
+1 for the good info references on pipes!
sheepsimulator