Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"cat /dev/random" versus "tail -f /dev/random"

Statement

cat /dev/random

keeps producing output, as expected, but

tail -f /dev/random

hangs (at least on OSX and SUSE). Why the latter statement hangs?

like image 564
Pedja Avatar asked Feb 26 '16 18:02

Pedja


People also ask

What is cat dev random?

cat /dev/urandom will give you a stream of random bytes between 0 and 255 , not all of those values are valid text characters. Because the terminal window was feed invalid data it was never expected to handle it could get the terminal application in to a "broken" state. Follow this answer to receive notifications.

What is the difference between Dev random and Dev urandom?

There is no difference between /dev/random and /dev/urandom; both behave identically. Apple's iOS also uses Yarrow.

Is Dev urandom random?

The /dev/urandom device provides a reliable source of random output, however the output will not be generated from an equal amount of random input if insufficient input is available.

How do I get a random number from Dev urandom?

Generating random numbers You can use /dev/urandom to generate pseudo-random numbers on the command line like this. Commands like this that pull data from /dev/urandom and use od to process it can generate nearly random numbers. Run the same command numerous times and you'll see that you get a range of numbers.


1 Answers

tail -f does several things:

  1. Find the end of the stream, either by reading until reaching an EOF or by doing a seek to the end (an operation not available on /dev/random).
  2. Back up a certain length (possibly by retaining a buffer of an appropriate length of contents recently read, possibly by retaining a list of seek positions for the last N lines during the initial scan, or by some other means), and print the contents between that point and the end.
  3. Continue to print new contents past that point as such contents become available.

If there is no end -- as is the case for /dev/random -- that first step will never complete.

cat does not need to find an end to seek back from it, and so it has no point of failure associated.

like image 55
Charles Duffy Avatar answered Sep 21 '22 18:09

Charles Duffy