linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* strace on cat /proc/my_file gives results by calling read twice why?
@ 2005-03-05  7:04 cranium2003
  0 siblings, 0 replies; 2+ messages in thread
From: cranium2003 @ 2005-03-05  7:04 UTC (permalink / raw)
  To: kernel newbies, kernerl mail



hello,
      I have a /proc file kernel module with its proc
read function as
static char mybuf[1024];
ssize_t my_file_read(struct file *filp, char *buf,
size_t count, loff_t *ppos)
{
   printk("Reading a from a /proc file....\n");
   static int b_read = 0;

    int len = strlen(mybuf);

    if (len == 0 || b_read) {
        b_read = 0;
        return 0;
    }

    if (len > count - 1)
        len = count - 1;

    copy_to_user(buf, mybuf, len);
    put_user(0, &buf[len]);
    ++len;
    b_read = 1;
    return len;
}

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?
          Also i check by writing to it as cat >
/proc/my_file
and got single result for write function call not
twice as for read.

regards,
cranium


	
		
__________________________________ 
Celebrate Yahoo!'s 10th Birthday! 
Yahoo! Netrospective: 100 Moments of the Web 
http://birthday.yahoo.com/netrospective/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: strace on cat /proc/my_file gives results by calling read twice why?
       [not found] <no.id>
@ 2005-03-05  7:43 ` Alan Curry
  0 siblings, 0 replies; 2+ messages in thread
From: Alan Curry @ 2005-03-05  7:43 UTC (permalink / raw)
  To: cranium2003; +Cc: kernel newbies, kernerl mail

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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-03-05  7:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-05  7:04 strace on cat /proc/my_file gives results by calling read twice why? cranium2003
     [not found] <no.id>
2005-03-05  7:43 ` Alan Curry

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).