1

I am writing a program for which I will create a script to generate test data in a specific format.

  1. How can I create a /dev/myprogram device that would work similar to /dev/random (preferably in the subdirectory of /dev/, e.g.: /dev/test/myprogram)?
  2. Can I attach any script to such device or do I have to make a C program using some (which?) low level APIs?
  3. Do I have to return one byte at a time or can I setup the device to always return specified amount of bytes, greater than 1?

I am using Ubuntu 14.04, though I am looking for a generic answer that would work on any *nix system.

2

It's not really possible to make a device without the help of the kernel (by writing your own kernel driver module); although there is FUSE, and lesser known and not commonly supported CUSE for character devices in user space, and NBD could be used for block devices.

If it's sufficient to be similar, you could use named pipes using mkfifo and then have some process that writes "random" data to this device, allowing other processes to read the same data back from it. You could replace /dev/random and /dev/urandom with such a named pipe and most programs would happily read their "random" data from it, as few programs think to check that they're really talking to the real random device. (until the random ioctl is more widely used)

# mkfifo /dev/foobar
# while true; do dd if=/dev/urandom of=/dev/foobar; done &
# hexdump -C -n 16 /dev/foobar
00000000  ec ec 91 4b 9a 62 95 b0  95 7f 44 16 21 8d cd de  |...K.b....D.!...|
0

(this is not exactly what you asked but...) Sometimes bash $RANDOM can do the job...

Ex: generate 20 dices

for a in {1..20}; do echo "$(($RANDOM % 6 + 1))"; done

Or if you prefer

declare -a A=(⚀  ⚁  ⚂  ⚃  ⚄  ⚅ )
for a in {1..20}; do echo ${A[$(($RANDOM % 6))]}; done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.