10. Blocking and non-blocking IO in the drive

Source: Internet
Author: User

Blocking, which is not acquired when the resource is fetched, suspends the current process (sleep, which takes the current process away from the scheduler and does not dispatch the current process) until the condition is met. On the other hand, non-blocking is a non-blocking process that, even if it cannot get to the resource, is either discarded directly or is continuously queried until it is satisfied.

When the upper read or write, the hope is blocked to get resources, then the bottom of the driver should be to block the way to achieve, and when it is expected in a non-blocking way, when it is not slow to meet the time, immediately return, and the application layer received –eagain return value.

1, blocking in the drive

The blocking operation of the underlying driver is implemented by means of a wait queue, which defines a waiting queue header, essentially a doubly linked list, in which the queue is subsequently hooked up to a different node, which is the waiting task, and when the waiting queue header is awakened at a certain point in the program, Then the task node of the doubly linked list that is hooked up to the queue header will be awakened.

1.1. API

(1) Defining the wait queue header

wait_queue_head_t Myqueue;

(2) Initialize the queue header

Init_waitqueue_head (&myqueue);

You can actually define and initialize the macro code as a whole: declare-waitqueuq (name) name. is the definition and the initialized head.

(3) Defining the wait task

Declare_waitqueue (name, tsk)

Defines a wait task, name. While Tsk is a task process, it is generally set to current, which is the process that is currently set.

(4) Add/Remove queues

void Add_wait_queue (wait_queue_head_t *myqueue, wait_queue_t * wait);

void Remove_wait_queue (wait_queue_head_t *myqueue, wait_queue_t * wait)

The wait queue (wait) is added to the doubly linked list with Myqueue as the head,

(5) Waiting for events

Wait_event (myqueue, condition)

will be myqueue in the queue, all blocking, waiting for condition to be true, can not be interrupted

Wait_event_interruptible (Wq, condition), is the first to wait and can be interrupted

(6) Wake-up queue

WAKE_UP (wait_queue_head_t *myqueue)

The wake-up wait queue header wakes all processes in the queue.

2, the driving non-blocking

The non-blocking implementation in the driver can be judged by the F_FLAGS flag in the struct file, when F_flags & O_nonblock (because the last applied flag bit was set, not only non-blocking, but also other, so with, Instead of the equals sign) is not equal to zero, and returns immediately when the resource is not available. There is also the use of a select or poll internship.

int select (int Nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout);

NFD: is the largest plus one in the detection file descriptor,

Fd_set: Is the file set of the detection file, can be seen, according to read, write, abnormal, separate detection

Timeout: Blocking time,

Select makes real-time detection of the specified file, returns immediately when a change in file status is detected, and then blocks directly when no detection is detected, but the blocking is not ongoing and the blocking time is specified by timeout. Over time, even if not detected, also to return

2.1. Setting the file Set

void fd_clr (int FD, fd_set *set); Empty file Set
int fd_isset (int FD, fd_set *set); Detects files, and is set to file sets
void Fd_set (int FD, fd_set *set); Set the file descriptor to a file set
void Fd_zero (Fd_set *set); Empty

2.2, the bottom poll

When the upper layer invokes select or poll, Epoll, the underlying poll is called,

unsigned int (*poll) (struct file *, struct poll_table_struct *);

The first parameter, which is the file to be opened, the related parameter, and the second parameter, is the table that is polled by the device. Inside the poll function, when returning, you need to return the following:

Pollin: Indicates device readable, reference is application layer,

Pollout: Indicates that the device is writable,

Pollerr: Error

Pollrdnorm: Must be set,

It is generally:

unsigned int (*poll) (struct file *, struct poll_table_struct *)

{

if (XXXXX)

Mask |= Pollin | Pollrdnorm;

Else

Mask |= Pollout | Pollrdnorm;

}

10. Blocking and non-blocking IO in the drive

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.