Skip to contentSkip to conversation

strace on cat /proc/my_file gives results by calling read twice why?

DISCUSSION Alan Curry 1 participant 1 message Mar 5, 2005

lore
Share & tools
lore

Alan Curry

author
#0 lore raw

cranium2003 writes the following:

Quoted 6 lines

when i strace cat /proc/my_file i found message
printing twice
Reading a from a /proc file....
Reading a from a /proc file....
Why that happening?

The first read returns some data and returns the number of bytes, and the second one indicates that EOF has been reached by returning 0. A single read call can't do both of those things, so cat needs to do 2 reads.

This has nothing to do with /proc or Linux; it is a logical consequence of the way read() is defined, and the job cat is supposed to do: it copies entire files to stdout, so it has to read until EOF. Only if the input file is empty will it see the EOF on the first read.