How is /dev/zero created and how can I make variants like /dev/one?

8

1

I read the mknod man page, which is (as far as I can tell) what you would use to make a character device like /dev/zero, but I don't see how you would get it to yield an infinite stream of zero bits (or another pattern). What is the procedure for creating such character devices?

Blacklight Shining

Posted 2012-10-01T06:57:20.873

Reputation: 2 127

They're implemented in the kernel. Look at zero_lseek, read_zero, write_zero, mmap_zero in drivers/char/mem.c. – David Schwartz – 2012-10-01T07:04:49.167

Oh…so I would have to modify the kernel? – Blacklight Shining – 2012-10-01T07:07:42.873

3Not necessarily, it depends on precisely what you want to do. Most likely, you would at least need to implement a module. – David Schwartz – 2012-10-01T07:18:04.853

What about /dev/one? – Blacklight Shining – 2012-10-01T07:45:12.267

Answers

3

All mknod does is associate a device file with a device driver. There are device drivers that implement interaction with actual devices, and there are device drivers that just react to read-write requests in useful ways. If you want to you can sit down and write a driver that returns the lyrics of the Star Spangeled Banner. But it's a matter of coding, not finding the right arguments for mknod.

Isaac Rabinovitch

Posted 2012-10-01T06:57:20.873

Reputation: 2 645

4

mknod creates the device node, but the VFS detects accesses to the device node and reroutes them to the appropriate driver within the kernel for handling. All device nodes, from /dev/null to /dev/sdX to /dev/ttyXX to /dev/videoX are handled this way.

Ignacio Vazquez-Abrams

Posted 2012-10-01T06:57:20.873

Reputation: 100 516

0

If you just want to re-create the /dev/zero abilities but with a character other than zero/null, you can use tr to change all the zero's ("\000" in octal) into something else.

So to spit out an endless stream of "a"s for example, you could do:

cat /dev/zero | tr "\000" "\141" | head -c 20
aaaaaaaaaaaaaaaaaaaa

Or skip the cat and output an M's worth (1024*1024) with

head -c 1M /dev/zero |tr "\000" "\141"

"\141" being the "a" character.

See this site http://www.asciitable.com/ for a quick short page of ASCII - Octal codes. It's actually this image here:
enter image description here

(I know, old "solved" question, but I found it while searching for an endless stream of different characters, so this "solves" the "how to make /dev/one or /dev/[different character]" problem.)

Xen2050

Posted 2012-10-01T06:57:20.873

Reputation: 12 097

I dunno…based on unscientific I-just-tried-it-right-now, head | tr >file has varying degrees of overhead. Depending on the system, it takes either one, one and a half, or two times as long as just head >file. – Blacklight Shining – 2015-12-16T09:56:41.790

So it does, tr's a workhorse I guess. Trying head -c 3G ... > /dev/null took under a second, but adding the pipe to tr took 21 seconds. Still about 150mb/sec so probably faster than most hard drives could keep up to. But it works, and there is no built-in /dev/one or /dev/a, etc – Xen2050 – 2015-12-16T16:04:49.737

Hence my asking after one! – Blacklight Shining – 2015-12-17T00:23:44.467