Subject: Re: Importing kqueue's from FreeBSD...
To: Jonathan Stone <jonathan@DSG.Stanford.EDU>
From: Luke Mewburn <lukem@wasabisystems.com>
List: tech-kern
Date: 03/16/2001 08:20:17
On Thu, Mar 15, 2001 at 01:02:12PM -0800, Jonathan Stone wrote:
> In message <20010316075619.F6988@wasabisystems.com>Luke Mewburn writes
> >On Thu, Mar 15, 2001 at 04:59:25PM -0000, eeh@netbsd.org wrote:
> >
> >I don't think it would be appropriate to hold back incorporation
> >of this work because it does have every event time that anyone can
>                                                ^^^^
> >think of right now.
> 
> "type"?

yup. `oops'.


> What's the userlevel api? Can it easily support explicit
> event-notification mechanism like the one (iirc) Gaurav Banga and Jeff
> Mogul proposed as a replacement for poll()/select(), for
> single-threaded apps with thousands of "interesting" objects?

yup. kqueue is designed explicitly for this. e.g, watching 10000's of
fd's where only a few actually have outstanding interesting events.

the following code fragment will print the number of times SIGCHLD
is delivered to the process:

	int kq;
	struct kevent event[1];

	    /* create kqueue */
	kq = kqueue();
	if (kq < 1)
		err(1, "kqueue");

	    /* setup event to watch for */
	event[0].ident = SIGCHLD;
	event[0].filter = EVFILT_SIGNAL;
	event[0].flags = EV_ADD | EV_ENABLE;
	n = kevent(kq, event, 1, NULL, 0, NULL);
	if (n < 0)
		err(1, "kevent(1)");

	    /* ... do stuff ... */
        sleep(10);

	    /* look for events outstanding */
	for (;;) {
		n = kevent(kq, NULL, 0, event, 1, NULL);
		if (n < 0)
			err(1, "kevent(2)");
		printf("kevent flags: 0x%x, data: %ld (times signal posted)\n",
		    event[0].flags, event[0].data);
	}


read jlemon's paper and skim through jlemon's slides. it does a good job
of explaining this.