POLL in different OSes

Donn Cave donn at u.washington.edu
Tue Oct 23 13:32:46 EDT 2001


Quoth Lucio Torre <lucio at movilogic.com>:
| At 10:18 AM 23/10/2001 +0200, Gerhard H=E4ring wrote:
...
|> Wouldn't it be possible to emulate poll() with select()? I'd be glad to
|> see a patch appear at Sourceforge if that's possible :-)

| i think i could do that. i can make a .c with the code that amulates poll=20
| from select, if someone else is willing to make it a patch or whatever it=20
| should be to get into python.
|
| btw: is it worth it? poll is supposed to be faster, so why emulate it? it=20
| would have no reason to exist!

I would rather discourage people from using poll in python, since select
appears to work everywhere poll does, but not vice versa.  Seems like
speed of one vs. the other would be irrelevant in the context of a
Python program.

But from C, I know poll is more fun to use, and there was a time when
a few System V 3.2 platforms with STREAMS network support didn't have
select, so here's a working if limited poll.c from those days.

	Donn Cave, donn at u.washington.edu
----------------------------------------
#ifndef _POLL_H_H_
#define _POLL_H_H_
/*
**  Emulate poll() for those platforms (e.g., Ultrix) that don't have it.
**  Usually select() and poll() are the same functionally, differing only
**  in the calling interface, but in a few cases they behave differently,
**  possibly where the platform implements STREAMS devices and one or the
**  other has some compatibility bug.  Poll() has a cleaner programming
**  interface, anyway.
*/

#ifdef HAVE_POLL

# include <sys/poll.h>

#else

struct pollfd {
	int fd;
	short events;
	short revents;
};

#define POLLIN 001
#define POLLPRI 002
#define POLLOUT 004
#define POLLNORM POLLIN
#define POLLERR 010
#define POLLHUP 020
#define POLLNVAL 040

int poll PROTO((struct pollfd *, int, int));

#endif
#endif /* _POLL_H_H_ */
----------------------------------------
/*
 *  prt
 *
 *  Copyright 1994 University of Washington
 *
 *  Permission is hereby granted to copy this software, and to
 *  use and redistribute it, except that this notice may not be
 *  removed.  The University of Washington does not guarantee
 *  that this software is suitable for any purpose and will not
 *  be held liable for any damage it may cause.
 */

/*
**  emulate poll() for those platforms (Ultrix) that don't have it.
*/

#include "osdefs.h"

#ifdef HAVE_POLL
int DontLoadMe;
#else
#include <sys/types.h>
#include <sys/time.h>
#include "poll.h"

void bzero();

int
poll(fds, nfds, timo)
	struct pollfd *fds;
	unsigned long nfds;
	int timo;
{
    struct timeval timeout, *toptr;
    fd_set ifds, ofds, efds, *ip, *op, *ep;
    int i, rc, n;
    FD_ZERO(&ifds);
    FD_ZERO(&ofds);
    FD_ZERO(&efds);
    for (i = 0, n = -1, op = ip = 0; i < nfds; ++i) {
	fds[i].revents = 0;
	if (fds[i].fd < 0)
		continue;
	if (fds[i].fd > n)
		n = fds[i].fd;
	if (fds[i].events & (POLLIN|POLLPRI)) {
		ip = &ifds;
		FD_SET(fds[i].fd, ip);
	}
	if (fds[i].events & POLLOUT) {
		op = &ofds;
		FD_SET(fds[i].fd, op);
	}
	FD_SET(fds[i].fd, &efds);
    }
    if (timo < 0)
	toptr = 0;
    else {
	toptr = &timeout;
	timeout.tv_sec = timo / 1000;
	timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000;
    }
    rc = select(++n, ip, op, &efds, toptr);
    if (rc <= 0)
	return rc;

    for (i = 0, n = 0; i < nfds; ++i) {
	if (fds[i].fd < 0) continue;
	if (fds[i].events & (POLLIN|POLLPRI) && FD_ISSET(i, &ifds))
		fds[i].revents |= POLLIN;
	if (fds[i].events & POLLOUT && FD_ISSET(i, &ofds))
		fds[i].revents |= POLLOUT;
	if (FD_ISSET(i, &efds))
		/* Some error was detected ... should be some way to know. */
		fds[i].revents |= POLLHUP;
    }
    return rc;
}
#endif /* ultrix */



More information about the Python-list mailing list