Stopping a kqueue

Discussion in 'C++' started by Bill, Jan 25, 2007.

  1. Bill

    Bill Guest

    Hi all,

    I've got some code which monitors a file by way of the kqueue/kevent
    mechanism but I'm having trouble getting it to shut down cleanly.

    My thread dedicated to the kqueue is basically a while-loop which
    blocks on the kevent() call until an event is posted, at which point it
    rattles through and does what it's supposed to. So far so good.

    while(watcherRunning==true){
    n = kevent(kq, NULL, 0, out_list, 255, NULL);
    //got an event, do something with it.
    }

    The problem comes when I want to stop monitoring the file. As far as I
    can see, the only way to get it to stop is to set "watcherRunning" to
    false (from within a separate thread) and then actually make a change
    to the file in order to trigger a kevent and unblock the thread!
    Surely there must be a nicer way to do it?

    Is there any way to trigger/post a kevent programmatically?

    I don't really want to set a timeout on the event blocking as that
    isn't really practical and it also turns it into a poll thereby
    defeating the purpose of using a kqueue in the first place!

    I'm tearing my hair out, can anyone help please?

    Thanks a million for any help.

    W
     
    Bill, Jan 25, 2007
    #1
    1. Advertising

  2. Bill

    Andre Kostur Guest

    Bill <> wrote in news:2007012523415775249-anon@myspacecom:

    > Hi all,
    >
    > I've got some code which monitors a file by way of the kqueue/kevent
    > mechanism but I'm having trouble getting it to shut down cleanly.


    [snip]

    >
    > I'm tearing my hair out, can anyone help please?
    >


    Yes, probably. But not here. In comp.lang.c++, only Standard C++ is
    discussed. You will get a better response if you post to whatever
    newsgroup is oriented towards whatever toolkit that kqueue/kevent comes
    from. (I can't even point you to a better newsgroup as I have no idea
    where kqueue/kevent even comes from, or even what platform it runs on. All
    of which is off-topic for this newsgroup.)
     
    Andre Kostur, Jan 25, 2007
    #2
    1. Advertising

  3. Bill

    Larry Smith Guest

    Bill wrote:
    > Hi all,
    >
    > I've got some code which monitors a file by way of the kqueue/kevent
    > mechanism but I'm having trouble getting it to shut down cleanly.
    >
    > My thread dedicated to the kqueue is basically a while-loop which blocks
    > on the kevent() call until an event is posted, at which point it rattles
    > through and does what it's supposed to. So far so good.
    >
    > while(watcherRunning==true){
    > n = kevent(kq, NULL, 0, out_list, 255, NULL);
    > //got an event, do something with it.
    > }
    >
    > The problem comes when I want to stop monitoring the file. As far as I
    > can see, the only way to get it to stop is to set "watcherRunning" to
    > false (from within a separate thread) and then actually make a change to
    > the file in order to trigger a kevent and unblock the thread! Surely
    > there must be a nicer way to do it?
    >
    > Is there any way to trigger/post a kevent programmatically?
    >
    > I don't really want to set a timeout on the event blocking as that isn't
    > really practical and it also turns it into a poll thereby defeating the
    > purpose of using a kqueue in the first place!
    >
    > I'm tearing my hair out, can anyone help please?
    >
    > Thanks a million for any help.
    >
    > W
    >


    You'll have to ask in a newsgroup for one of the BSD's
    or Mac OSX. I believe that kqueue originates from free-BSD.
     
    Larry Smith, Jan 26, 2007
    #3
  4. Bill

    Dizzy Guest

    Bill wrote:

    > The problem comes when I want to stop monitoring the file. As far as I
    > can see, the only way to get it to stop is to set "watcherRunning" to
    > false (from within a separate thread) and then actually make a change
    > to the file in order to trigger a kevent and unblock the thread!
    > Surely there must be a nicer way to do it?
    >
    > Is there any way to trigger/post a kevent programmatically?


    This is offtopic.

    The general solution for event loops is to not block indifinetly (don't give
    NULL as last argument to kevent) on waiting for an event but use a timeout
    value that won't kill your CPU (so not extremly low) but will still provide
    enough time resolution so that you can stop the loop in the timeframe you
    need it (not very high). Example, my event handling loops timeout on 20
    milliseconds which is enough that the CPU still shows ~0% usage if no
    events are comming but still short enough that I can do several things that
    are needed to be done at the same time (in your case just to exit ASAP). Of
    course you will need to modify the code after the kevent call to be aware
    that kevent now can also just timeout (return without any event) and this
    is not an error.

    > I don't really want to set a timeout on the event blocking as that
    > isn't really practical and it also turns it into a poll thereby
    > defeating the purpose of using a kqueue in the first place!


    How come it turns it into a poll ? The big difference between poll(2) and
    kevent(2) is not the timeout usage (hell, you can use no timeout for
    poll(2) too thus it's the same thing) but the fact that poll(2) returns the
    WHOLE structure back which you have to scan in liniar time for the events
    while kevent always returns just the events thus the API itself is fast not
    the function done in the kernel (I would expect both to do use some common
    code up to some point). But if you mean it turns into a "poll" (as a
    concept not the syscall) that's not right either. You are waiting for some
    time to be notified about events and there are 2 cases:
    1. almost no event will come in which case you will get a lot of timeouts
    but this in reality doesn't result from any performance loss (if
    implemented well), like I said, a 20ms timeout still makes my daemons show
    as not using any CPU but sleeping most of the time which is what you want
    to achieve right ?
    2. a lot of events comming in which case the timeout is irrelevant as kevent
    most of the time will return without timeout

    > I'm tearing my hair out, can anyone help please?


    Next time, just look into any (open source) server handling event loop out
    there and you will see most (if not all) of them using timeouts on I/O
    multiplexing. I really fail to see what's your problem with that when it's
    cleanly solving the initial problem.

    --
    Dizzy
    http://dizzy.roedu.net
     
    Dizzy, Jan 26, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill

    closing a kqueue

    Bill, Jan 25, 2007, in forum: C Programming
    Replies:
    4
    Views:
    385
  2. Ritesh Nadhani

    Re: Using select.kqueue()

    Ritesh Nadhani, Sep 3, 2009, in forum: Python
    Replies:
    0
    Views:
    516
    Ritesh Nadhani
    Sep 3, 2009
  3. Ritesh Nadhani

    select.kqueue.control changelist argument

    Ritesh Nadhani, Sep 3, 2009, in forum: Python
    Replies:
    0
    Views:
    312
    Ritesh Nadhani
    Sep 3, 2009
  4. harijay
    Replies:
    0
    Views:
    318
    harijay
    Jan 25, 2011
  5. Curt Sampson
    Replies:
    1
    Views:
    148
    Francis Cianfrocca
    Feb 5, 2008
Loading...

Share This Page