title: How to make the read operation occur only one time in the pSeudo driver here?
body: <p>I am trying to write a kernel driver program. However, when I do the cat operation on cat /dev/pSeudoDrv. It just keeps printing the value. It never exits. I was assuming that the cat operation should exit after reading once. However, it is reading forever. What is the problem here?</p>
  
  <pre><code>#include &lt;linux/init.h&gt;
  #include &lt;linux/module.h&gt; /** needed by all modules **/
  #include &lt;linux/kernel.h&gt;  /** This is for KERN_ALERT **/
  #include &lt;linux/fs.h&gt; /** for file operations **/
  #include &lt;linux/cdev.h&gt;  /** character device **/
  #include &lt;linux/device.h&gt;  /** for sys device registration in /dev/ and /sys/class **/
  /** for copy_to_user **/
  #include &lt;asm/uaccess.h&gt;
  
  
  /** For class registration to work, you need GPL license **/
  MODULE_LICENSE("GPL");
  
  
  static struct cdev basicCdev;
  static struct class *basicDriverClass;
  
  static int  basicMajorNumber = 0;
  
  /** Prototype for read, this will be invoked when the read function is done on to the driver **/
  /** The declaration type is file operations based function pointer - read **/
  static ssize_t basicRead(struct file *filp, char *buffer, size_t length,loff_t *offset);
  
  static int basicOspen(struct inode *inode, struct file *file);   
  
  /** File Operations function pointer table **/
  /** There are plenty of file operations  **/
  
  static struct file_operations fops = {
    .read = basicRead,
    .write = NULL,
    .open = basicOspen,
    .release = NULL
  };
  
  static ssize_t basicRead(struct file *filp, char *buffer, size_t length, loff_t *offset)
  {
       char msg[1024] = "Hello SJ_read\0";
       printk(KERN_ALERT "The Read operation called\r\n");
       copy_to_user( buffer, msg, sizeof(msg) );
       return sizeof(msg);
  }
  
  static int basicOspen(struct inode *inode, struct file *file)
  {
  
     printk("Kernel.Basic Driver Opened now!!\r\n");
  
     return 0;
  }
  
  static void setup_cdev(struct cdev *dev, int minor, struct file_operations *fops)
  {
           int err = -1;
           /** MKDEV call creates a device number i.e. combination of major and minor number **/
           int devno = MKDEV(basicMajorNumber, minor);
           /** Initiliaze character dev with fops **/
           cdev_init(dev, fops);
           /**owner and operations initialized **/
           dev-&gt;owner = THIS_MODULE;
           dev-&gt;ops = fops;
           /** add the character device to the system**/
           /** Here 1 means only 1 minor number, you can give 2 for 2 minor device, the last param is the count of minor number enrolled **/
           err = cdev_add (dev, devno, 1);
  
           if (err)
           {
                   printk (KERN_NOTICE "Couldn't add cdev");
           }
   }
  
  static int chrDriverInit(void)
  {
  
      int result;
      dev_t dev; 
  
      printk("Welcome!! Device Init now..");
  
      /** int alloc_chrdev_region(dev_t *dev, unsigned int firstminor,unsigned int count, char *name);  **/
      /** dev -&gt; The dev_t variable type,which will get the major number that the kernel allocates.  **/
      /**The same name will appear in /proc/devices.  **/
  
      /** it is registering the character device **/   
      /** a major number will be dynamically allocated here **/
      result = alloc_chrdev_region(&amp;dev, 0, 1, "pSeudoDrv");
  
      if( result &lt; 0 )
      {
        printk("Error in allocating device");
        return -1;    
      }
  
      /** From these two if's we are avoiding the manual mknod command to create the /dev/&lt;driver&gt; **/
      /**  creating class, and then device created removes the dependency of calling mknod  **/
      /** A good method - the mknod way is depreciated **/
      /** mknod way is -  mknod /dev/&lt;driver_name&gt; c &lt;majorNumber&gt; &lt;minorNumber&gt;
  
  
      /** add the driver to /sys/class/chardrv **/
      if ((basicDriverClass = class_create(THIS_MODULE, "chardrv")) == NULL)    //$ls /sys/class
      {
          unregister_chrdev_region(dev, 1);
          return -1;
      }
  
       /** add the driver to /dev/pSeudoDrv -- here **/
      if (device_create(basicDriverClass, NULL, dev, NULL, "pSeudoDrv") == NULL) //$ls /dev/
      {
          class_destroy(basicDriverClass);
          unregister_chrdev_region(dev, 1);
          return -1;
      }
  
       /** let's see what major number was assigned by the Kernel **/
       basicMajorNumber = MAJOR(dev);
       printk("Kernel assigned major number is %d ..\r\n",basicMajorNumber );
  
      /** Now setup the cdev **/
      setup_cdev(&amp;basicCdev, 0, &amp;fops);
  
      return 0;   
  
  }
  
  
  static void chrDriverExit(void)
  {
      /** A reverse - destroy mechansim -- the way it was created **/
      printk("Releasing Simple Devs -- %s\r\n",  __FUNCTION__);
      /** delete the character driver added **/
      cdev_del(&amp;basicCdev);
      /** destroy the device created **/
      device_destroy(basicDriverClass, MKDEV(basicMajorNumber, 0));
      /** destroy the class created **/
      class_destroy(basicDriverClass);
      /** unregister the chr dev **/
      unregister_chrdev(basicMajorNumber, "pSeudoDrv");
  
  }
  
  
  module_init(chrDriverInit);
  module_exit(chrDriverExit);
  </code></pre>