Linux drivers--blocking and non-blocking I/o__linux

Source: Internet
Author: User

1. Blocking and non-blocking I/O

Defined:

Blocking: When performing a device operation, suspend the process if the resource is not available, and then do so after you know the conditions for the operation. The suspended process enters hibernation and is removed from the scheduler's run queue until the condition is satisfied.

Non-blocking: A process that is non-blocking does not hang when it cannot satisfy an operation, it either abandons it, or keeps querying, knowing that it can operate.

1.1 Waiting Queues

Function: You can use the wait queue to wake up the blocking process

(1) Define "Wait Queue header"

wait_queue_head_t My_queue;

(2) Initialize "Wait Queue header"

Init_waitqueue_head (&my_queue);

(3) Define wait queues

Declare_wait_queue_head (name)

(4) Adding/removing Waiting queues

void Fastcall add_wait_queue(wait_queue_head_t *q,wait_queue_t *wait);

void Fastcall remove_wait_queue(wait_queue_head_t *q,wait_queue_t *wait);

(5) Waiting for events

Wait_event (queue,condition)

Wait_event_interruptible (queue,condition)

Wait_event_timeout (Queue,condition,timeout)

Wait_event_interruptible_timeout (Queue,condition,timeout)

Note: Timeout means the timeout to wait until the time of arrival, whether or not satisfied, is returned.

(5) Wake queue

void Wake_up (wait_queue_head_t *queue);

void Wake_up_interruptible (wait_queue_head_t *queue);

In pairs appear:

A. WAKE_UP () and wait_event () or wait_event_timeout () used in pairs

B. Wake_up_interruptible () and wait_event_interruptible or wait_event_interruptible_timeout () used in pairs

Attention:

1, WAKE_UP () can wake the process in task_interruptible and task_uninterruptible (they are in hibernation)

2, wake_up_interruptible () can only wake the process in task_interruptible

(6) Sleep on waiting queues

sleep_on (wait_queue_head_t *q);

interruptible_sleep_on (wait_queue_head_t *q);

1.2 Globalfifo device drivers that support blocking operations

Conditions: First, the Globalfifo global memory into a FIFO, similar to the pipeline technology, only to the FIFO to write data in order to read from the FIFO data.

In the driver read (), write () function, the FILP->F_FLAGS flag is accessed using non-blocking.

1.3 Validating Globalfifo Read and write in user space

Whenever the Echo process writes a string of data to/dev/globalfifo (that is, a device file), the cat process immediately displays the string

2. Polling Operation

2.1 concept and function of rotation

Applications typically use Select () and poll () system calls to query whether or not to block access to a device

2.2 Polling programming in applications

function 1:select () function

struct Timeval

{

int tv_sec;

int tv_usec;

};

Fd_zero (); Clear a File descriptor

Fd_set (); Add a file descriptor to a file descriptor set

FD_CLR (); Set a file descriptor from a file descriptor

Fd_isset () to determine whether a file descriptor is placed

2.3 Polling programming in device driver

function 1:poll () function--

function 2:poll_wait () function-->: Add the current process to the waiting queue (poll_table)

2.4 Globalfifo drivers that support polling operations

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.