Linux Poll机制

本文深入探讨Linux的Poll机制,详细解释了Poll用于检测文件描述符事件的功能,包括读写事件类型及其意义。文章还介绍了Poll的核心数据结构、API调用流程,特别是poll函数和poll_wait函数的工作原理,并提供了驱动源码和应用程序示例,展示了如何在应用中使用Poll进行读写操作。
摘要由CSDN通过智能技术生成

一、Poll什么?

        Poll某个fd特定的event。

二、event的总类包括?

POLLIN – This bit must be set if the device can be read without blocking.

POLLRDNORM – This bit must be set if “normal” data is available for reading. A readable device returns (POLLIN | POLLRDNORM).

POLLRDBAND – This bit indicates that out-of-band data is available for reading from the device. It is currently used only in one place in the Linux kernel (the DECnet code) and is not generally applicable to device drivers.

POLLPRI – High-priority data (out-of-band) can be read without blocking. This bit causes select to report that an exception condition occurred on the file because select reports out-of-band data as an exception condition.

POLLHUP – When a process reading this device sees end-of-file, the driver must set POLLHUP (hang-up). A process calling select will be told that the device is readable, as dictated by the select functionality.

POLLERR – An error condition has occurred on the device. When a poll is invoked, the device is reported as both readable and writable, since both read and write will return an error code without blocking.

POLLOUT – This bit is set in the return value if the device can be written to without blocking.

POLLWRNORM – This bit has the same meaning as POLLOUT, and sometimes it actually is the same number. A writable device returns (POLLOUT | POLLWRNORM).

POLLWRBAND – Like POLLRDBAND, this bit means that data with nonzero priority can be written to the device. Only the datagram implementation of the poll uses this bit since a datagram can transmit out-of-band data.

三、Poll机制核心数据结构

struct pollfd {
    int   fd;         /* file descriptor */
    short events;     /* requested events */
    short revents;    /* returned events */
};

四、Poll机制的API说明

1、应用层的poll函数

int poll(struct pollfd *fds, nfds_t nfds, int timeout);

Where,

fds – file descriptors array

nfds – number of file descriptors

timeout – specifies the number of milliseconds that poll() should block waiting for a file descriptor to 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

denglin12315

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值