c event handling - how?

Discussion in 'C Programming' started by Phui Hock, Nov 21, 2003.

  1. Phui Hock

    Phui Hock Guest

    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.
    Phui Hock, Nov 21, 2003
    #1
    1. Advertising

  2. (Phui Hock) wrote in
    news::

    > 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 ->
    --
    Mark A. Odell, Nov 21, 2003
    #2
    1. Advertising

  3. Phui Hock

    Lew Pitcher Guest

    Mark A. Odell wrote:

    > (Phui Hock) wrote in
    > news::
    >
    >
    >>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)
    Lew Pitcher, Nov 21, 2003
    #3
  4. Bruno Desthuilliers <> wrote in
    news:3fbe71eb$0$9292$:

    >> 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 ->
    --
    Mark A. Odell, Nov 21, 2003
    #4
  5. Mark A. Odell wrote:
    > (Phui Hock) wrote in
    > news::
    >
    >
    >>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
    Bruno Desthuilliers, Nov 21, 2003
    #5
  6. 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++.
    E. Robert Tisdale, Nov 21, 2003
    #6
  7. Phui Hock

    Lew Pitcher Guest

    Mark A. Odell wrote:

    > Bruno Desthuilliers <> wrote in
    > news:3fbe71eb$0$9292$:
    >
    >
    >>>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)
    Lew Pitcher, Nov 21, 2003
    #7
  8. Phui Hock

    Alex Guest

    E. Robert Tisdale <> 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
    Alex, Nov 21, 2003
    #8
  9. Mark A. Odell wrote:
    > Bruno Desthuilliers <> wrote in
    > news:3fbe71eb$0$9292$:
    >
    >
    >>>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
    Bruno Desthuilliers, Nov 21, 2003
    #9
  10. Phui Hock

    PH Guest

    >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).
    PH, Nov 22, 2003
    #10
  11. Phui Hock

    Anuj Heer Guest

    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

    Anuj Heer, Nov 22, 2003
    #11
  12. Anuj Heer <> 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


    >


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

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "I am looking for myself. Have you seen me somewhere?"
    - Anon
    Joona I Palaste, Nov 22, 2003
    #12
  13. Richard Heathfield, Nov 22, 2003
    #13
  14. Phui Hock

    Mike Wahler Guest

    "Anuj Heer" <> wrote in message
    news:...
    > 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
    Mike Wahler, Nov 23, 2003
    #14
  15. Phui Hock

    CBFalconer Guest

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Nov 23, 2003
    #15
  16. Phui Hock

    Dan Pop Guest

    In <2pTvb.11188$> "Mike Wahler" <> writes:


    >"Anuj Heer" <> wrote in message
    >news:...
    >> 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:
    Dan Pop, Nov 24, 2003
    #16
  17. Dan Pop <> scribbled the following:
    > In <2pTvb.11188$> "Mike Wahler" <> writes:
    >>"Anuj Heer" <> wrote in message
    >>news:...
    >>> 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "We sorcerers don't like to eat our words, so to say."
    - Sparrowhawk
    Joona I Palaste, Nov 24, 2003
    #17
  18. On Fri, 21 Nov 2003 15:43:53 -0500, Lew Pitcher <>
    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
    Dave Thompson, Nov 27, 2003
    #18
  19. In article <>,
    Dave Thompson <> wrote:
    >On Fri, 21 Nov 2003 15:43:53 -0500, Lew Pitcher <>
    >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

    --
    Dave Vandervies, Dec 3, 2003
    #19
    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. sunil

    Pager and Event Handling

    sunil, Jul 18, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    377
    sunil
    Jul 18, 2003
  2. Makarand

    ASP.NET Event handling.

    Makarand, Jul 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    406
    Makarand
    Jul 24, 2003
  3. Mark Tarver
    Replies:
    22
    Views:
    1,280
    J Kenneth King
    Apr 26, 2009
  4. Peter
    Replies:
    34
    Views:
    1,918
    James Kanze
    Oct 17, 2009
  5. Iñaki Baz Castillo
    Replies:
    1
    Views:
    176
    Iñaki Baz Castillo
    Apr 15, 2008
Loading...

Share This Page