Home > Software engineering >  How optimized are writes to /dev/null in Linux?
How optimized are writes to /dev/null in Linux?

Time:06-04

In a Linux userland program, if I open(2) /dev/null (or do something equivalent, like redirecting stdout to /dev/null), and then write(2) to the resulting handle, how deep into the kernel does my request to write get propagated before being discarded? Does a memory copy of the data happen? Does a kernel context switch happen?

If I use writev(2) against the /dev/null handle instead, is the kernel smart enough to skip walking the passed iovec?

CodePudding user response:

Since null is a char device, a write passes a pointer to the data and a size. write just returns the count passed in, no data copy occurs. See: https://elixir.bootlin.com/linux/v5.18/source/drivers/char/mem.c#L453

  • Related