/dev/random does not block /dev/random does not block unix unix

/dev/random does not block


That's not special to /dev/random, that's just the behaviour of read. The parameter is a buffer size and read will return what's available up to that size.

Consider using a FILE* and fread instead to read one block of 100 bytes.


In short, unless there's no data to be read, read will not block. It will always return the amount it could read or an error, even if that's less data than you wanted.


Your program is reading 100 bytes from /dev/random. If you restart it with some manual action (e.g. typing the command, or simply the arrow-up key for bash), that manual action (sequence of keypresses) is feeding the entropy pool of random(4). So you would never be in the blocking case.

And the semantics of the read(2) syscall applied to /dev/random is that it will try to read some bytes. If at least one byte has been read, the read(2) syscall succeeds and does not block.

Also, as I commented, recent enough hardware and recent enough kernel has a good enough random source which might maje random(4) never block.