473,326 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Stopping a kqueue

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

Jan 25 '07 #1
3 1946
Bill <an**@myspace.comwrote 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.)
Jan 26 '07 #2
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.

Jan 26 '07 #3
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

Jan 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: CG | last post by:
Hi I have developed a Windows Service When I try to start the Service it tells me that it cannot start as there may not be any work to do When I comment out below line of code in my OnStart...
6
by: Dave | last post by:
I have a situation where I want to react to a ctrl-click on a <span> and it works in Netscape and Firefox browsers but in IE I have a problem. In IE I do catch the ctrl-click but IE also renders...
0
by: Glen Wolinsky | last post by:
I am creating a Windows service that will check a request queue (database) for pending requests. It will then process each individual request until completed, wait a set time interval and then...
1
by: juleni | last post by:
Hello, is there a possibility starting or stopping postgres database and creating database instance by JAVA? Is there some API available for this? If yes, can you please write some example, how...
2
by: matteo | last post by:
Hi everyboby, i wrote a c# service that every XXX minute launch a working thread on timer events, something like this: private void timer_Elapsed ( object status ) { // Worker thread...
5
by: chris.hearson | last post by:
How do I programmatically prevent a service from stopping? I want to be able to keep my service running (started), under certain conditions, when a user tries to stop it. I have tried throwing an...
6
by: D | last post by:
I have a simple file server utility that I wish to configure as a Windows service - using the examples of the Python Win32 book, I configured a class for the service, along with the main class...
4
by: Bill | last post by:
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...
0
by: eldamiani | last post by:
Ineed a help. I want to ask, how I can do dialog box window which is a normal form, but causes stopping the code execution in form which call out this dialog box. I have ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.