吃饭前写个linux select/poll/epoll代码示例玩玩

972 篇文章 329 订阅
148 篇文章 34 订阅

      等朋友吃饭,还有1个小时, 写个select, poll, epoll程序玩玩:

include <stdio.h>
#include <unistd.h>
#include <sys/time.h>

int play_select()
{
	fd_set rfds;
	FD_ZERO(&rfds);
	FD_SET(STDIN_FILENO, &rfds);

	fd_set wfds;
	FD_ZERO(&wfds);
	FD_SET(STDOUT_FILENO, &wfds);
	
	struct timeval tv;
	tv.tv_sec = 5;
	tv.tv_usec = 0;
	int retval = select(STDOUT_FILENO + 1, &rfds, &wfds, NULL, &tv);
	if (retval < 0)
	{
		printf("select error\n");
		return -1;
	}

	if (retval == 0)
	{
		printf("time out\n");
		return -1;
	}


	// 需要对所有句柄进行轮询遍历
	if(FD_ISSET(STDIN_FILENO, &rfds))
	{
		printf("can read\n");
	}

	if(FD_ISSET(STDOUT_FILENO, &wfds))
	{
		printf("can write\n");
	}

	return 0;	   
}



#include <stdio.h>
#include <unistd.h>
#include <poll.h>

int play_poll()
{
	pollfd pfd[2];
	int nfds = 2;
	
	pfd[0].fd = STDIN_FILENO;
	pfd[0].events = POLLIN;

	pfd[1].fd = STDOUT_FILENO;
	pfd[1].events = POLLOUT;

	int timeout_ms = 5000; // 5000毫秒
	int retval = poll(pfd, nfds, timeout_ms);
	if (retval < 0)
	{
		printf("poll error\n");
		return -1;
	}

	if (retval == 0)
	{
		printf("time out\n");
		return -1;
	}


	// 需要对所有句柄进行轮询遍历
	if(pfd[0].revents & POLLIN)
	{
		printf("can read\n");
	}

	if(pfd[1].revents & POLLOUT)
	{
		printf("can write\n");
	}

	return 0;
}



#include <stdio.h>
#include <unistd.h>
#include <sys/epoll.h>

int play_epoll()
{
	epoll_event evReq[10];
	epoll_event evRsp[10];

	evReq[0].data.fd = STDIN_FILENO;
	evReq[0].events = EPOLLIN;

	evReq[1].data.fd = STDOUT_FILENO;
	evReq[1].events = EPOLLOUT;

	// 创建管理句柄
	int epollFd = epoll_create(10);

	// 添加以便于管理
	epoll_ctl(epollFd, EPOLL_CTL_ADD, evReq[0].data.fd, evReq);
	epoll_ctl(epollFd, EPOLL_CTL_ADD, evReq[1].data.fd, evReq + 1);

	int timeout_ms = 5000; // 5000毫秒
	int retval = epoll_wait(epollFd, evRsp, 10, timeout_ms);
	if (retval < 0)
	{
		printf("epoll error\n");
		close(epollFd);
		return -1;
	}

	if (retval == 0)
	{
		printf("time out\n");
		close(epollFd);
		return -1;
	}

	// 无需遍历所有句柄,只需直接取结果就行,如下evRsp[i]对应的句柄一定是"就绪的(active)"
	for (int i = 0; i < retval; ++i) 
	{
		printf("active fd is %d\n", evRsp[i].data.fd);
	}
	
	close(epollFd);
	return 0;
}


int main()
{
	play_select();
	play_poll();
	play_epoll();

	return 0;
}

       测了一下, 正常work.

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值