/dev/primes is what I want without writing C code just python which I have known and easily written the program.

The device file should be similar to /dev/urandom that outputs primes instead of random stream!!

I am excited if linux could do it!

  • 1
    If you implement it in userspace, you can make its user interface similar to /dev/urandom in that you could use something like a pipe (see mkfifo) and write a Python program that continuously writes primes to this pipe. It will block until the pipe is full. Consuming from a /dev/primes pipe would be similar to consuming /dev/urandom. – filbranden Jun 17 '19 at 0:38
  • 2
    Having said that, a character interface is probably a very poor interface for getting primes, since you usually want to get prime numbers of more than one byte (I'm thinking large primes) and with a character device or a pipe there's a lot that could go wrong, a consumer could do a short read, get it misaligned. There are better ways to create an interface to a prime generator that makes it hard to misuse it. But probably the best interface is to simply do it in-process, using a library. That library would read from /dev/urandom to get entropy to generate primes! – filbranden Jun 17 '19 at 0:47
  • 1
    Wow.  (1) If I had felt like writing an answer, I would have written an answer.  I didn’t feel like writing an answer, so I wrote a comment.  Comments are not supposed to be complete answers.  (2) Since you seem to want to be able to generate a random stream of primes (or a stream of random primes), I suggested an approach that could help you achieve the result you want.  (3) You didn’t ask how to write a Python program that reads /dev/urandom and outputs only primes, so I felt no urge to explain how to do it. … (Cont’d) – Scott Jun 17 '19 at 18:29
  • 1
    (Cont’d) …  (4) Now that you are (sort-of) asking that, my response is: it should be straightforward.  Do some research, and try to do it. If you get stuck, ask us a specific question.   (5) The English of your question is unclear, but your first paragraph seems to be saying that you can easily write Python programs.  If that’s true, I wouldn’t expect you to need hints.   (6) You say “I never said I wanted a new device.”  Before you asked this question, did you do a search for /dev/randomprime or something similar?  … (Cont’d) – Scott Jun 17 '19 at 18:29
  • 1
    (Cont’d) …  If you had, you would have found nothing, because there’s no such thing.  So, by saying that you want a pseudo device like /dev/random that outputs primes, you are saying that you want a new device. (7) You say “I never said I wanted a new device ... I could use a device file instead.” … Well, “In Unix, everything is a file.”  We often say “device” when we mean “device file” or “pseudo-device”.  Did you believe that I was talking about a piece of hardware? – Scott Jun 17 '19 at 18:29

What you want is not possible.

Character devices are provided by Linux kernel modules.
Linux kernel modules are written in C.
You can, in theory, use a programming language other than C. But your program needs to be compiled into a binary that can be loaded into the kernel. In particular, you need to define an initialization handler, an exit handler, as well as the functions dev_open, dev_read, dev_release with well-defined signatures.
You cannot do this in Python (truly exotic approaches may exist, but I do not know of any).

For more information on how to write a Linux kernel module in C, confer to any guide (contents not inserted here due to copyright) you like.

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.