Blocking and non-blocking for Linux Device Driver Programming

Source: Internet
Author: User
Blocking and non-blocking of Linux Device Driver Programming-general Linux technology-Linux programming and kernel information. The following is a detailed description. A blocking operation means that when a device operation fails to obtain the resource, the process suspends until the operation meets the operational conditions. A non-blocking process does not stop when it cannot perform device operations. The suspended process enters the sleep state and is removed from the running queue of the scheduler until the waiting conditions are met.

In the Linux driver, we can use the wait queue to implement blocking operations. Wait queue appeared in the Linux kernel as a basic function unit for a long time. It uses the queue as the basic data structure and is closely integrated with the process scheduling mechanism, it can be used to implement the core asynchronous event notification mechanism. The waiting queue can be used to synchronize access to system resources. The Linux semaphore described in the previous section is also implemented by the waiting queue in the kernel.

Next we will redefine the device "globalvar", which can be opened by multiple processes, but this process or other processes can read this data only once a process writes data, otherwise, it will be blocked.
CODE: # include
# Include
# Include
# Include
# Include
# Include
MODULE_LICENSE ("GPL ");

# Define MAJOR_NUM 254

Static ssize_t globalvar_read (struct file *, char *, size_t, loff_t *);
Static ssize_t globalvar_write (struct file *, const char *, size_t, loff_t *);

Struct file_operations globalvar_fops =
{
Read: globalvar_read, write: globalvar_write,
};

Static int global_var = 0;
Static struct semaphore sem;
Static wait_queue_head_t outq;
Static int flag = 0;

Static int _ init globalvar_init (void)
{
Int ret;
Ret = register_chrdev (MAJOR_NUM, "globalvar", & globalvar_fops );
If (ret)
{
Printk ("globalvar register failure ");
}
Else
{
Printk ("globalvar register success ");
Init_MUTEX (& sem );
Init_waitqueue_head (& outq );
}
Return ret;
}

Static void _ exit globalvar_exit (void)
{
Int ret;
Ret = unregister_chrdev (MAJOR_NUM, "globalvar ");
If (ret)
{
Printk ("globalvar unregister failure ");
}
Else
{
Printk ("globalvar unregister success ");
}
}

Static ssize_t globalvar_read (struct file * filp, char * buf, size_t len, loff_t * off)
{
// Wait for data to be obtained
If (wait_event_interruptible (outq, flag! = 0 ))
{
Return-ERESTARTSYS;
}

If (down_interruptible (& sem ))
{
Return-ERESTARTSYS;
}

Flag = 0;
If (copy_to_user (buf, & global_var, sizeof (int )))
{
Up (& sem );
Return-EFAULT;
}
Up (& sem );
Return sizeof (int );
}

Static ssize_t globalvar_write (struct file * filp, const char * buf, size_t len, loff_t * off)
{
If (down_interruptible (& sem ))
{
Return-ERESTARTSYS;
}
If (copy_from_user (& global_var, buf, sizeof (int )))
{
Up (& sem );
Return-EFAULT;
}
Up (& sem );
Flag = 1;
// Notification data available
Wake_up_interruptible (& outq );
Return sizeof (int );
}

Module_init (globalvar_init );
Module_exit (globalvar_exit );
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.