java.nio and SelectionKey.interestOps(int i)

Kurt Miller kurt at
Fri Jan 23 20:50:50 PST 2009

Martin Kihlgren wrote:
> Well that certainly explains it!
> I tried your test program, and it freezes before any output on both my
> Linux 2.6.21 and my OS X Tiger.

Sorry I forgot the BSD's use bidirectional pipes and Linux, and OS X
have half duplex pipes. I wrote to the wrong fd for a half duplex pipe.
The test program should not freeze, it should complete without error
indicating the kernel doesn't write over the event field, or print an
error message. Below is a new version that should be portable and work
on OS X, Linux, etc.

> On the other hand, now that I know what the problem is, I can simply
> create a task for the thread doing the select, to change the
> interestOps between selects instead. That way the overwrite won't
> happen, and my program works again.
> Thanks for the help and explanation - it really did help, and now my
> project works fine on BSD systems as well :D

Your welcome. :-)

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

struct pollfd fds[2];
int pipefds[2];

static void *
thread(void *arg) {
 fds[0].events = POLLIN | POLLOUT;
 write(pipefds[1], "", 1);
 return NULL;

main(int argc, char *argv[]) {
 pthread_t tid;

 if (pipe(pipefds) != 0)
   err(1, "pipe failed");

 fds[0].fd = pipefds[0];
 fds[0].events = POLLIN;

 fds[1].fd = pipefds[1];
 fds[1].events = POLLIN;

 if (pthread_create(&tid, NULL, thread, NULL) != 0)
   err(1, "pthread_create failed");

 poll(fds, 2, -1);

 if (fds[0].events != (POLLIN | POLLOUT)) {
   printf("events overwritten by kernel!\n");
   return 1;

 return 0;

More information about the bsd-port-dev mailing list