0

I wonder if select()/poll() works if the communication is unbuffered on both sides (reading and writing side). Unbuffered means no kernel buffer. The data is copied from user-space memory into user-space memory. The data is copied from the buffer provided in write() directly into the one provided by read().

Now let's assume program A wants to read and program B writes.

Both programs call poll() to wait until they can read/write.

But poll() will never return on both sides. Program A will never be able to read because the other side is not writing. And program B will never be able to write because the other side is not reading.

I want to know if this can happen. If there is always a kernel buffer then of course it will never happen.

int fd[] = {..., ...}; // this could be a pipe, file, fifo, tcp socket, local socket, ... (without any kernel buffer)

pid_t p = fork();
if (p == 0)
{
  struct pollfd pfd;
  pfd.fd = fd[0];
  pfd.events = POLLIN;
  pfd.revents = 0;
  poll(&pfd, 1, -1); // poll will never return as the other side is not writing
}
else
{
  struct pollfd pfd;
  pfd.fd = fd[1];
  pfd.events = POLLOUT;
  pfd.revents = 0;
  poll(&pfd, 1, -1); // poll will never return as the other side is not reading
}

I simply want to know if this is possible (poll() not returning on both sides).

zomega
  • 1,538
  • 8
  • 26
  • 1
    What could be the rationale for using poll/select on non blocking IO? They are intended to allow a program to wait for read/write ready events on multiple channels... – Serge Ballesta Apr 24 '22 at 08:23
  • @SergeBallesta You are right, I removed the non-blocking stuff... – zomega Apr 24 '22 at 08:32
  • Please show with code, just what you mean by buffered/unbuffered? – hyde Apr 24 '22 at 08:52
  • @hyde I mean no kernel buffer. What happens if both sides use select/poll and there is no kernel buffer? Will select/poll ever return? – zomega Apr 24 '22 at 08:54
  • not sure what you want to achieve, but you may consider `splice()` – Ilian Zapryanov Apr 24 '22 at 08:56
  • @IlianZapryanov I am trying to write a code which can read from any file descriptor. It uses poll(). I am a little concerned it may fail in some rare cases when the other side is waiting for me to call read(). But I am waiting the other side to call write(). So nothing will happen... – zomega Apr 24 '22 at 09:01
  • @somega `poll` shall be ok for you, `splice` is for zero-copy and avoid roundtrips, you may consider it in the future and/or a combination of the two. – Ilian Zapryanov Apr 24 '22 at 09:02
  • 3
    I think I will add some code or else nobody will understand... – zomega Apr 24 '22 at 09:04
  • Example code would really clarify this. Sounds like you are asking, if doing `select` on O_DIRECT pipe will make `select` on other end to return with active fd? Why don't you test it, and then ask "how to make this work" if it doesn't work, or "why does this work" if it does work? – hyde Apr 24 '22 at 09:11
  • I added some code... – zomega Apr 24 '22 at 09:21
  • O_DIRECT does not mean the data is not buffered. See https://stackoverflow.com/a/49462406/1216776 – stark Apr 24 '22 at 09:32

0 Answers0