1.初始化
使用
fd = open(“/dev/poll”, flags, (mode_t)mode);获得文件描述符
2.增加事件
struct pollfd *events;
pwrite(fd, events,sizeof(struct pollfd) * eventsnum, 0) 直接往里边写
3.删除事件
使用 POLLREMOVE 事件,然后把这个时间用增加时间的方法写入/dev/poll,系统会自动将我们要删除的描述符时间删除
4.监听事件
struct dvpoll { struct pollfd *dp_fds; int dp_nfds; int dp_timeout; }struct dvpoll dvp;
res = ioctl(devpollop->dpfd, DP_POLL, &dvp);返回事件发生的个数
用个for遍历一遍即可
for (i = 0; i < res; i++) {
int which = 0;
int what = dvp.dp_fds[i].revents;
if (what & POLLHUP)
what |= POLLIN | POLLOUT;
else if (what & POLLERR)
what |= POLLIN | POLLOUT;
if (what & POLLIN)
which |= EV_READ;
if (what & POLLOUT)
which |= EV_WRITE;
if (!which)
continue;
。。。
}