我们可以使用轮询功能的命名管道? [英] can we use poll function with unnamed pipes?

查看:107
本文介绍了我们可以使用轮询功能的命名管道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个程序,我需要监测无名管道两端的某些事件。我可以使用命名管道与轮询功能。

I am trying to write a program where i need to monitor ends of unnamed pipe for certain events. Can i use unnamed pipes with poll function.

如果有,可以请你告诉我与功能描述poll函数语法

If yes, can you please show me the syntax for poll function with functional descriptors

推荐答案

要使用调查检查 readfd 可读或 writefd 是可写的:

An example of poll

To use poll to check if readfd is readable or writefd is writable:

int readfd;
int writefd;

// initialize readfd & writefd, ...
// e.g. with: open(2), socket(2), pipe(2), dup(2) syscalls

struct pollfd fdtab[2];

memset (fdtab, 0, sizeof(fdtab)); // not necessary, but I am paranoid

// first slot for readfd polled for input
fdtab[0].fd = readfd;
fdtab[0].events = POLLIN;
fdtab[0].revents = 0;

// second slot with writefd polled for output
fdtab[1].fd = writefd;
fdtab[1].events = POLLOUT;
fdtab[1].revents = 0;

// do the poll(2) syscall with a 100 millisecond timeout
int retpoll = poll(fdtab, 2, 100);

if (retpoll > 0) {
   if (fdtab[0].revents & POLLIN) {
      /* read from readfd, 
         since you can read from it without being blocked */
   }
   if (fdtab[1].revents & POLLOUT) {
      /* write to writefd,
         since you can write to it without being blocked */
   }
}
else if (retpoll == 0) {
   /* the poll has timed out, nothing can be read or written */
}
else {
   /* the poll failed */
   perror("poll failed");
}

这篇关于我们可以使用轮询功能的命名管道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆