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

From: Alan Curry
Date: Sat Mar 05 2005 - 02:45:51 EST


cranium2003 writes the following:
>
>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.
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/