473,837 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(watcherRu nning==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 programmaticall y?

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 1960
Bill <an**@myspace.c omwrote in news:2007012523 415775249-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(watcherRu nning==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 programmaticall y?

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 programmaticall y?
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
2622
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 event.... objEventLog.EnableRaisingEvents = Tru ....It starts fin I have the same issue when stopping the service because of below line of code in my OnStop even
6
5920
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 the span in inverse video, essentially selecting the item. Here is a short sample that demonstrates the issue: <html><head> <script type="text/javascript">
0
1433
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 move on to the next one in the list. The processing routine could take 1-2 minutes to finish processing one request. My question is this: If the service is stopped while the process routine is running, how can I make sure the processing of that...
1
1547
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 to do it? Thank you in advance, with best regards,
2
2261
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 System.Threading.ThreadPool.QueueUserWorkItem ( new System.Threading.WaitCallback( _timer_Elapsed ) );
5
17504
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 exception - from OnStop(): the SCM waits for a long time, reports that the service didn't respond in a timely fashion and then leaves the status as stopping. - from Dispose(): the SCM displays an error dialog with the exception details and...
6
8128
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 functions __init__, SvcStop, and SvcDoRun (which contains my server code). After registering the service, I am able to start it with no problems. However, it never stops correctly (net stop returns "service could not be stopped") and service is left...
4
1675
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 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){
0
1465
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 MessageBox.Show("Operations"); FormDialog frm = new FormDialog(); frm.Show() //not ShowDialog() //now the code should be stopping, but i don't know how. MessageBox.Show("Operations"); I need something such as "ReadLine()" in console application, which is...
0
9691
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10891
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10582
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9418
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7009
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5677
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4480
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3128
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.