玩下linux中的的poll函数------可以用任何描述符号fd而不限于网络socket

本文介绍了Linux系统中与select函数类似的poll函数,用于轮询检测多个描述符状态。建议先了解select再学习poll。通过示例展示了poll函数的使用,包括读取用户输入的情况,并指出poll与select的相似之处。
摘要由CSDN通过智能技术生成

       前面我们玩了linux中的select函数, 今天我们来介绍一个与之类似的函数, 作用就是做轮询检测, 建议先看select,  再看poll.  至于函数原型和返回值, 就没有必要单独说了, 网上一搜一大堆。

       直接上菜:

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <fcntl.h> 
#include <sys/poll.h>  

#if 0
struct pollfd
{
	int fd;          //文件描述符
	short events;    //请求的事件
	short revents;   //返回的事件
};
#endif

int main()  
{
	pollfd fds[10];
	fds[0].fd = STDIN_FILENO;  // 标准输入描述符
	fds[0].events = POLLIN;    // 检测读

	int nfds = 1;              // 待监听的描述符的个数
	int timeoutMS = 5000;      // 毫秒
	int i
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值