Signal handler declaration confusion - help!

Discussion in 'C Programming' started by Charles Sullivan, Sep 5, 2006.

  1. I have existing code in which the signal() and signal
    handler functions appear (for example) like this:

    ----------------------
    #include <signal.h>
    void my_handler(int signo);
    signal(SIGALRM, my_handler);
    ---------------------

    It appears to work on all Unix-like platforms reported on
    (including Linux, various BSDs, Mac OSX, SYSV, AT&TSysVr4).
    However on AT&TSysVr4 there are compiler warnings for the
    signal() statements:
    "warning: argument is incompatible with prototype: arg #2"

    According to the signal man page (which I received from a
    user of this OS):

    ------------------
    #include <signal.h>
    void (*signal(int sig, void (*disp)(int)))(int);
    -----------------

    I thought I would just change the declaration of the
    signal handler to match the OS (ATTSYSVR4 is defined at
    configuration) like this:

    -------------------
    #include <signal.h>

    #ifdef ATTSYSVR4
    typedef void (*disp)(int) SIGHANDLER;
    #else
    typedef void SIGHANDLER;
    #endif

    SIGHANDLER my_handler(int signo);
    ------------------

    Does that look correct? I don't have access to a
    computer running this OS and can't try it myself.

    Thanks for your help.

    Regards,
    Charles Sullivan
    Charles Sullivan, Sep 5, 2006
    #1
    1. Advertising

  2. On Tue, 05 Sep 2006 20:32:02 GMT, Charles Sullivan
    <> wrote:

    <snip: using signal() but warning on AT&TSysVr4>
    > According to the signal man page (which I received from a
    > user of this OS):
    >
    > ------------------
    > #include <signal.h>
    > void (*signal(int sig, void (*disp)(int)))(int);
    > -----------------
    >
    > I thought I would just change the declaration of the
    > signal handler to match the OS (ATTSYSVR4 is defined at
    > configuration) like this:
    >
    > -------------------
    > #include <signal.h>
    >
    > #ifdef ATTSYSVR4
    > typedef void (*disp)(int) SIGHANDLER;
    > #else
    > typedef void SIGHANDLER;
    > #endif
    >
    > SIGHANDLER my_handler(int signo);
    > ------------------
    >
    > Does that look correct? I don't have access to a
    > computer running this OS and can't try it myself.
    >

    No. Your first new typedef is illegal syntax (any OS).

    To create a typedef for the same type as the second parameter to
    signal shown would be:
    typedef void (*SIGHANDLER) (int);

    But that is not the type your handler should have; you want the actual
    function type which _decays_ to that pointer type:
    typedef void SIGHANDLER (int);
    ...
    SIGHANDLER my_handler;
    and that is equivalent to what you already have, which is also the
    type defined by the C standard; so this isn't the error, or the fix.

    Perhaps your user can get you a copy of the (relevant parts of) their
    <signal.h> (and anything else it uses), or someone else who uses this
    system can do so, or someone who knows about it can tell you, since
    clearly it is looking for something different than your user has
    gotten from the man page and the standard specifies. Especially watch
    out for any possibility that their implementation of the standard
    conditionally compiles different declarations depending on the
    environment settings, as many do, that you look at the one in use --
    or conversely, you find an environment that can be selected that
    provides the standard and preferred type.

    Or perhaps just possibly the compiler in use on that system is so old
    it doesn't automatically decay functions as required by C89. If so,
    you could use
    signal (SIGALRM, &my_handler) /* note & */
    and that will also work fine on any standard system.

    - David.Thompson1 at worldnet.att.net
    Dave Thompson, Sep 21, 2006
    #2
    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. Michael Pronath
    Replies:
    1
    Views:
    1,141
    Diez B. Roggisch
    Jan 3, 2005
  2. Jack Orenstein

    threading.Thread vs. signal.signal

    Jack Orenstein, Sep 18, 2005, in forum: Python
    Replies:
    0
    Views:
    452
    Jack Orenstein
    Sep 18, 2005
  3. Weng Tianxiang
    Replies:
    2
    Views:
    637
    Jonathan Bromley
    Jan 30, 2007
  4. Nicolas Moreau
    Replies:
    9
    Views:
    3,086
  5. dibacco73
    Replies:
    1
    Views:
    630
    joris
    Feb 12, 2009
Loading...

Share This Page