473,406 Members | 2,956 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,406 software developers and data experts.

c event handling - how?

Hi,
How do I generate an event and got it handled by a handler? I know
how to do it in C++ or Java but I got no idea how to do it in C. What
is the best approach to write a function that will take a function
pointer, a pointer to user data (what is it?) and then I notify it
when a certain event happens?

I'm new to C, btw :-)

Thanks a lot for helping.
Nov 13 '05 #1
18 24783
ph******@myway.com (Phui Hock) wrote in
news:a9**************************@posting.google.c om:
Hi,
How do I generate an event and got it handled by a handler? I know
how to do it in C++ or Java but I got no idea how to do it in C. What
is the best approach to write a function that will take a function
pointer, a pointer to user data (what is it?) and then I notify it
when a certain event happens?


You mean call back? I don't think that you can in C. There are no threads,
interrtups, or tasks in C. You can see if setjmp/longjmp() are of interest
but I doubt standard C will allow you to do what you want without
platform-specific extensions (which are off-topic here).

--
- Mark ->
--
Nov 13 '05 #2
Mark A. Odell wrote:
ph******@myway.com (Phui Hock) wrote in
news:a9**************************@posting.google.c om:

Hi,
How do I generate an event and got it handled by a handler? I know
how to do it in C++ or Java but I got no idea how to do it in C. What
is the best approach to write a function that will take a function
pointer, a pointer to user data (what is it?) and then I notify it
when a certain event happens?

You mean call back? I don't think that you can in C.


Well, you can.
There are no threads,
Correct
interrtups,
Sort of wrong. Hardware level interrupts aren't supported as such, but an
abstraction of interrupts (as a general concept) /is/ supported.
or tasks in C. You can see if setjmp/longjmp() are of interest
but I doubt standard C will allow you to do what you want without
platform-specific extensions (which are off-topic here).


See the signal() and raise() functions in the Standard, along with signal.h

--
Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Nov 13 '05 #3
Mark A. Odell wrote:
ph******@myway.com (Phui Hock) wrote in
news:a9**************************@posting.google.c om:

Hi,
How do I generate an event and got it handled by a handler? I know
how to do it in C++ or Java but I got no idea how to do it in C. What
is the best approach to write a function that will take a function
pointer, a pointer to user data (what is it?) and then I notify it
when a certain event happens?

You mean call back?


What the OP asks for include, but is not restricted to, callbacks.
I don't think that you can in C.


Do what ? callbacks ? yes you can. Have a look at the (standard) qsort()
function. It takes a callback function as lsat argument.

(snip)

Bruno

Nov 13 '05 #4
Bruno Desthuilliers <bd***********@removeme.free.fr> wrote in
news:3f**********************@news.free.fr:
You mean call back?


What the OP asks for include, but is not restricted to, callbacks.
I don't think that you can in C.


Do what ? callbacks ? yes you can. Have a look at the (standard) qsort()
function. It takes a callback function as lsat argument.


Not what I meant, of course you can pass a function a pointer to a
function but the function you just called with this pointer can't call you
back without recursion. By callback I meant called by some other task or
an interrupt service routine. Apparently signal() and raise() can do some
level of this as Lew pointed out.

--
- Mark ->
--
Nov 13 '05 #5
Phui Hock wrote:
How do I generate an event and get it handled by a handler?
I know how to do it in C++ or Java
but I have no idea how to do it in C.
Show us how you would do it in C++.
What is the best approach to write a function
that will take a function pointer,
a pointer to user data (what is it?)
and then I notify it when a certain event happens?


Show us an example of what you mean in C++.

Nov 13 '05 #6
Mark A. Odell wrote:
Bruno Desthuilliers <bd***********@removeme.free.fr> wrote in
news:3f**********************@news.free.fr:

You mean call back?


What the OP asks for include, but is not restricted to, callbacks.

I don't think that you can in C.


Do what ? callbacks ? yes you can. Have a look at the (standard) qsort()
function. It takes a callback function as lsat argument.

Not what I meant, of course you can pass a function a pointer to a
function but the function you just called with this pointer can't call you
back without recursion. By callback I meant called by some other task or
an interrupt service routine. Apparently signal() and raise() can do some
level of this as Lew pointed out.


Specifically, signal() accepts a pointer to the function to be "called
back", and a value indicating the signal that will invoke the callback.

raise() accepts a value that will be used as the value of a signal. When
raise() is invoked, the signal is "sent", and asychronously, the callback
function associated to that signal by a previous signal() call will be called.

There are limitations on what the callback function can and cannot do, so
don't go coding complex logic into it (i.e. no printf() statements, etc.).

--
Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Nov 13 '05 #7
E. Robert Tisdale <E.**************@jpl.nasa.gov> wrote:
Phui Hock wrote:
How do I generate an event and get it handled by a handler?
I know how to do it in C++ or Java
but I have no idea how to do it in C. Show us how you would do it in C++.
Not here.
What is the best approach to write a function
that will take a function pointer,
a pointer to user data (what is it?)
and then I notify it when a certain event happens?

Show us an example of what you mean in C++.


Not here.

Alex
Nov 13 '05 #8
Mark A. Odell wrote:
Bruno Desthuilliers <bd***********@removeme.free.fr> wrote in
news:3f**********************@news.free.fr:

You mean call back?


What the OP asks for include, but is not restricted to, callbacks.

I don't think that you can in C.


Do what ? callbacks ? yes you can. Have a look at the (standard) qsort()
function. It takes a callback function as lsat argument.

Not what I meant, of course you can pass a function a pointer to a
function


I sure guess you know this !-)

I just wanted to clarify that point for the OP and C beginners reading this.

(sorry, I s/c/sh/ould have stated this more explicitely).

Bruno

Nov 13 '05 #9
PH
>By callback I meant called by some other task or
an interrupt service routine.
What I've learned from another is to create a table and map events to
function pointers (array or linked list). So I do this:-

typedef void (*callback) (void *);
struct event_handlers {
callback cb;
struct event_handlers *next;
} listeners[SIZE];

And I have an event registration function like this:-
void register_event(int event, callback cb) ...

When an event happens:-
event_handlers *handler = listener[event]; /* event is mapped to
event_handers */
for(; handler != NULL; handler = handler->next)
handler->cb(data);

I also learned that I can create a hash table to map events to function
pointers. As the number of new events grow, we can realloc to accommodate
those.

Is this the best approach?
Apparently signal() and raise() can do some
level of this as Lew pointed out.


With signal() and raise(), I can't pass my own data to the target function
(except via global variable, which I think, is ugly).
Nov 13 '05 #10
buddy

in c u can use the setvect and getvect functions to apture standard
interrupts and make them point to your code thus making event handling
much simpler....but do remember to call the original isr at the end of
your routine so that the interrupt is not lost and also set the vector
to the original pointer at the end of your program unless you want to
reboot every time your program ends...
u did not specify the type of event handling you are looking for..
do contact me if you need more information on this topic

hp*****@vcustomer.net
Nov 13 '05 #11
Anuj Heer <hp*****@vcustomer.net> scribbled the following:
buddy in c u can use the setvect and getvect functions to apture standard
interrupts and make them point to your code thus making event handling
much simpler....but do remember to call the original isr at the end of
your routine so that the interrupt is not lost and also set the vector
to the original pointer at the end of your program unless you want to
reboot every time your program ends...
u did not specify the type of event handling you are looking for..
do contact me if you need more information on this topic hp*****@vcustomer.net


The C language does not define setvect, getvect, isr or anything
like that. You want a system-specific newsgroup.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I am looking for myself. Have you seen me somewhere?"
- Anon
Nov 13 '05 #12
Anuj Heer wrote:
buddy

in c u can use the setvect and getvect functions to apture standard
interrupts


C has no functions called setvect or getvect.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #13

"Anuj Heer" <hp*****@vcustomer.net> wrote in message
news:86**************************@posting.google.c om...
buddy

in c u can use the setvect and getvect functions to apture standard
interrupts and make them point to your code thus making event handling
much simpler....but do remember to call the original isr at the end of
your routine so that the interrupt is not lost and also set the vector
to the original pointer at the end of your program unless you want to
reboot every time your program ends...
u did not specify the type of event handling you are looking for..
do contact me if you need more information on this topic


Sigh. Yet another "all the world's an Intel box" person.
Please read the FAQ.

-Mike
Nov 13 '05 #14
Anuj Heer wrote:

in c u can use the setvect and getvect functions to apture standard
interrupts and make them point to your code thus making event handling
much simpler....but do remember to call the original isr at the end of
your routine so that the interrupt is not lost and also set the vector
to the original pointer at the end of your program unless you want to
reboot every time your program ends...


Please stop giving such erroneous information. There is a saying
about keeping silent and allowing people to wonder if you are an
idiot, and speaking up and confirming their suspicions.

It is bad enough that someone may actually use your
misinformation, but in addition you are causing all sorts of extra
traffic here simply to correct and warn about it.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #15
In <2p******************@newsread1.news.pas.earthlink .net> "Mike Wahler" <mk******@mkwahler.net> writes:

"Anuj Heer" <hp*****@vcustomer.net> wrote in message
news:86**************************@posting.google. com...
buddy

in c u can use the setvect and getvect functions to apture standard
interrupts and make them point to your code thus making event handling


Sigh. Yet another "all the world's an Intel box" person.


Well, I'm using an Intel box too, but I can't find any trace of setvect
or getvect on it. I must be missing something...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #16
Dan Pop <Da*****@cern.ch> scribbled the following:
In <2p******************@newsread1.news.pas.earthlink .net> "Mike Wahler" <mk******@mkwahler.net> writes:
"Anuj Heer" <hp*****@vcustomer.net> wrote in message
news:86**************************@posting.google .com...
buddy

in c u can use the setvect and getvect functions to apture standard
interrupts and make them point to your code thus making event handling
Sigh. Yet another "all the world's an Intel box" person.

Well, I'm using an Intel box too, but I can't find any trace of setvect
or getvect on it. I must be missing something...


So all the world is not just an Intel box, it's a very special kind of
Intel box, then.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"We sorcerers don't like to eat our words, so to say."
- Sparrowhawk
Nov 13 '05 #17
On Fri, 21 Nov 2003 15:43:53 -0500, Lew Pitcher <Le*********@td.com>
wrote:
<snip>
Specifically, signal() accepts a pointer to the function to be "called
back", and a value indicating the signal that will invoke the callback.

raise() accepts a value that will be used as the value of a signal. When
raise() is invoked, the signal is "sent", and asychronously, the callback
function associated to that signal by a previous signal() call will be called.
Handling of raise'd signals (and abort) is synchronous; 7.14.2.1p2. It
is signals caused from outside the process, or at least thread -- in
particular, in a POSIX environment, by kill -- that can be
asynchronous, but that is out of scope of standard C and clc.
There are limitations on what the callback function can and cannot do, so
don't go coding complex logic into it (i.e. no printf() statements, etc.).


A signal handler only called for/by a raise'd signal (or abort) can do
anything (an ordinary function can) because those can only have
occurred in user code. A handler for a "true" signal like SIGSEGV is
not guaranteed to be able to use anything in the library, although in
practice the purely computational stuff like string.h, ctype.h, *abs
and *div, and probably math.h, complex.h and tgmath.h, will work, as
almost certainly will stdarg.h and the compile-time stuff. But in at
least some environments a program can't prevent asynchronous
occurrence of a handled signal, so the handler must always be safe.

- David.Thompson1 at worldnet.att.net
Nov 13 '05 #18
In article <b4********************************@4ax.com>,
Dave Thompson <da*************@worldnet.att.net> wrote:
On Fri, 21 Nov 2003 15:43:53 -0500, Lew Pitcher <Le*********@td.com>
wrote:

There are limitations on what the callback function can and cannot do, so
don't go coding complex logic into it (i.e. no printf() statements, etc.).


A signal handler only called for/by a raise'd signal (or abort) can do
anything (an ordinary function can) because those can only have
occurred in user code.


Except call raise() (N869 7.14.1.1#4).

(I'm not sure whether a "reasonable implementation" would have problems
with this - the obvious way of implementing it is to do something
like "sighandler_funcs[signum](signum);" (except in POSIXish systems,
where the man pages I have access to document it as being equivalent to
"kill(getpid(),sig)"), but something that does weird stuff inside raise()
need not jump through hoops to make it reentrant.)
dave

--
Nov 13 '05 #19

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

Similar topics

4
by: hillcountry74 | last post by:
Hi, I'm a newbie and trying to understand event handling in c#. I have understood handling events using delelgate objects. But not this method- "Event handling by overriding the virtual...
3
by: Ashok Kumar K | last post by:
Hi all, Where can I get some insight on using the __hook, __unhook, event_source and event_receiver for specifically COM events. The documentation given in MSDN is very minimal. I have the...
9
by: Sridhar | last post by:
Hi, I have created a web page which includes a place holder. I also have a dropdown list in that webpage. when I select one of the choices in that dropdown list, It will load a user control...
2
by: Paul E. Orman | last post by:
I have a piece of VB code (.NET 1.1 - VB 2003) that loads data from a database through a timer. So the timer is setup and from it I call the procedure that loads the latest records from the...
3
by: johncee | last post by:
Greetings, I created a base class that has a datagrid. I've made it generic as possible so that any derived classes pass some info to the base constructor (including a SQL select stmt) &...
4
by: reggiestyles | last post by:
Hi, I've got a question about prototype and event handling. I've got several div's (dynamic number) on a page that I want to set as active or inactive (basically, I'm using scriptaculous'...
2
by: a | last post by:
Hi I have a 3-column listview. It has to return the value of the cell when the user click the listview. The SelectedIndexChanged event and ItemSelectionChanged Event only return the row...
3
by: a | last post by:
Hi, I have a 3-column listview. It has to return the value of the cell when the user click the listview. The SelectedIndexChanged event and ItemSelectionChanged Event only return the row...
1
by: stmfc | last post by:
hi, for an event handling mechanism, we need an event object. (the object where the event actually occur) and we need an event handler, and we need a registration of the event handler to the...
5
by: Klaudiusz Bryja | last post by:
Hi, This is for NetCF 2.0. I need to create event handling code which using reflection. I have some parameters in XML which describe how event should be handled. I have code to create...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.