title: Why does cat call read() twice when once was enough?
body: <p>I am new to Linux kernel module. I am learning char driver module based on a web course. I have a very simple module that creates a <code>/dev/chardevexample</code>, and I have a question for my understanding:</p>
  
  <p>When I do <code>echo "hello4" &gt; /dev/chardevexample</code>, I see the <code>write</code> execute exactly once as expected. However, when I do <code>cat /dev/chardevexample</code>, I see the read executed <em>two</em> times.</p>
  
  <p>I see this both in my code and in the course material. All the data was returned in the first <code>read()</code>, so why does <code>cat</code> call it again?</p>
  
  <p>All the things I did so far are as follows:</p>
  
  <ol>
  <li><code>insmod chardev.ko</code> to load my module</li>
  <li><code>echo "hello4" &gt; /dev/chardevexample</code>. This is the write and I see it happening exactly once in dmesg  </li>
  <li><code>cat /dev/chardevexample</code>. This is the read, and <code>dmesg</code> shows it happening twice. </li>
  <li><p>I did <code>strace cat /dev/chardevexample</code>, and I indeed see the function call being called twice for read. There is a write in between as well  </p>
  
  <pre><code>read(3, "hello4\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 131072) = 4096
  write(1, "hello4\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"...,    4096hello4) = 4096
  read(3, "", 131072)
  </code></pre></li>
  <li><p><code>dmesg</code> after read (cat command) </p>
  
  <pre><code>[909836.517402] DEBUG-device_read: To User  hello4 and bytes_to_do 4096 ppos 0 # Read #1 
  [909836.517428] DEBUG-device_read: Data send to app hello4, nbytes=4096        # Read #1   
  [909836.519086] DEBUG-device_read: To User   and bytes_to_do 0 ppos 4096       # Read #2  
  [909836.519093] DEBUG-device_read: Data send to app hello4, nbytes=0           # Read #2
  </code></pre></li>
  <li><p>Code snippet for read, write and file_operations is attached. Any
  guidance would help. I searched extensively and couldn't understand.
  Hence the post.</p>
  
  <pre><code>/*!
   * @brief Write to device from userspace to kernel space
   * @returns     Number of bytes written
   */
  
  static ssize_t device_write(struct file *file,  //!&lt; File pointer
                                  const char *buf,//!&lt; from for copy_from_user. Takes 'buf' from user space and writes to 
                                                  //!&lt; kernel space in 'buffer'. Happens on fwrite or write 
                                  size_t lbuf,    //!&lt; length of buffer
                                  loff_t *ppos)   //!&lt; position to write to
  {
          int nbytes = lbuf - copy_from_user(
                                             buffer + *ppos,      /* to */
                                             buf,                 /* from */
                                             lbuf);               /* how many bytes */
          *ppos += nbytes;
          buffer[strcspn(buffer, "\n")] = 0; // Remove End of line character
          pr_info("Recieved data \"%s\" from apps, nbytes=%d\n", buffer, nbytes);
          return nbytes;
  }
  
  /*!
   * @brief Read from device - from kernel space to user space
   * @returns     Number of bytes read
   */
  static ssize_t device_read(struct file *file,//!&lt; File pointer
                             char *buf,   //!&lt; for copy_to_user. buf is 'to' from buffer
                         size_t lbuf, //!&lt; Length of buffer
                         loff_t *ppos)//!&lt; Position {
      int nbytes;
      int maxbytes;
      int bytes_to_do;
      maxbytes = PAGE_SIZE -  *ppos;
      if(maxbytes &gt;lbuf)
              bytes_to_do = lbuf;
      else
              bytes_to_do = maxbytes;
  
      buffer[strcspn(buffer, "\n")] = 0; // Remove End of line character
      printk("DEBUG-device_read: To User  %s and bytes_to_do %d ppos %lld\n", buffer + *ppos, bytes_to_do, *ppos);
      nbytes = bytes_to_do - copy_to_user(
                                      buf, /* to */
                                      buffer + *ppos, /* from */
                                      bytes_to_do); /* how many bytes*/
      *ppos += nbytes;
      pr_info("DEBUG-device_read: Data send to app %s, nbytes=%d\n", buffer, nbytes);
      return nbytes;} /* Every Device is like a file - this is device file operation */ static struct file_operations device_fops = {
          .owner = THIS_MODULE,
          .write = device_write,
          .open  = device_open,
          .read  = device_read,};
  </code></pre></li>
  </ol>