Thread: signal function definited twice with no error: how is it possible?

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    4

    signal function definited twice with no error: how is it possible?

    Hi,
    I'm using a library in my code in a file called "my_signal.c".
    In this file I've defined this function:
    Code:
    Sigfunc * signal(int signo, Sigfunc *func){
        struct sigaction    act, oact;
    ...
    }
    Since the signal function is also in file signal.h and I included it in "my_sygnal.h" file, I'm wondering why the compiler did not say anything about this "double declaration" of the function with the same name.

    Thanks,
    Massimo

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you include signal.h in your C file? If not, the "official" definition isn't seen. (And if you #defined things the same, then even if you did the function matches the prototype, so then you're playing linker roulette as to which version gets called.)

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    4
    Quote Originally Posted by tabstop View Post
    Did you include signal.h in your C file? If not, the "official" definition isn't seen. (And if you #defined things the same, then even if you did the function matches the prototype, so then you're playing linker roulette as to which version gets called.)
    So the situation is this one:

    In file my_signal.c I have included:
    Code:
    #include        "basic.h"
    #include        "my_signal.h"
    while in basic.h I have included:
    #include <signal.h>

    I'm attaching the source code (it a very simple echo client/server) so not much to read.

    Thanks,
    M
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    Dec 2013
    Posts
    4
    Anyone, please?
    Massimo

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Maybe people would be more ready to help if you post your source code in the message. Personally I can't be bothered to download and rummage around in a zip file when most people just post the relevant code directly to the forum.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I haven't bothred to look at your zip file, but I suspect from your (hazy) description I know what is happening.

    If any of your source files (directly or indirectly) #include's both your my_signal.h and <signal.h>, the code will not compile (the compiler will complain about signal being multiply declared).

    However, if none of your source files #include both headers, the compiler will not have any reason to complain. I therefore suspect one of your claims about #include'ing those files is incorrect. For example, you have commented out one of the #include directives you described.

    If it gets past the compiler then, with most implementations, the functions declared in <signal.h> are typically defined in a library. Then it comes down to link order: if an object file contains the definition of your signal function, the linker will not attempt to use the standard library version. So the program will build correctly.

    If the declarations of your version and the standard version are compatible (which they might be, depending on what Sigfunc is) then your program will run okay. If they are not compatible, then you will probably get unintended behaviours sometimes (such as intermittent program crashes).

    Why are you trying to implement a function in the standard library? Unless you are working for a compiler vendor (in which case you need to implement the standard library), re-implementing functions from the standard library is a really bad idea.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Dec 2013
    Posts
    4
    Hi, first of all, I'm sorry about my previous posts. I'll be less lazy (I just tried to be coincise, maybe too much). So the significant source code follows (probably it is just necessary to look at my_signal.h source code:

    my_signal.h:
    Code:
    #include <signal.h>
    #include <sys/wait.h>
    
    typedef void Sigfunc(int); 
    
    Sigfunc* signal(int , Sigfunc *); 
    
    void manage_zombie (int segnale);
    my_signal.c:
    Code:
    #include        "basic.h"
    #include        "my_signal.h"
    
    Sigfunc * signal(int signo, Sigfunc *func){
            struct sigaction        act, oact;
    
            act.sa_handler = func;
            sigemptyset(&act.sa_mask);
            act.sa_flags = 0;
            if (signo != SIGALRM) {
               act.sa_flags |= SA_RESTART;
            }
            if (sigaction(signo, &act, &oact) < 0)
                    return(SIG_ERR);
            return(oact.sa_handler);
    }
    basic.h
    Code:
    #include        <signal.h>      
    #include        <stdio.h>       
    #include        <stdlib.h>
    I'm wondering if this is the case described by Grumpy.

    In particular, I'm a little confuse in my_signal.h:
    - it includes "signal.h"
    - it declares a "signal" function

    why does not the compile complain about that?

    Thanks,
    Massimo

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Isn't signal already defined by signal.h? Check here for the details:

    man page Signal section 3

    Probably it's best if you name your version something else to avoid naming conflicts.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by mcanonic View Post
    In particular, I'm a little confuse in my_signal.h:
    - it includes "signal.h"
    - it declares a "signal" function

    why does not the compile complain about that?

    Thanks,
    Massimo
    As mentioned above, the function you define and the standard function have the same "type" (function taking int and pointer-to-void-function-with-int-parameter returning pointer-to-void-function-with-int-parameter) so there is no conflict. Note that the standard headers don't have any code in them (because you don't put code in .h files), so the compiler only sees one function of that name and is perfectly happy. Which one the linker calls is up to the linker. (C can't really complain about people defining functions that exist in the standard library, because they keep adding to the standard library all the time, so the names are not absolutely reserved.)

  10. #10
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Signal handler function - pointer to this gets lost
    By maus in forum C++ Programming
    Replies: 1
    Last Post: 07-01-2009, 09:10 AM
  2. Help with Signal Function
    By cjohnman in forum C Programming
    Replies: 2
    Last Post: 04-10-2008, 10:18 AM
  3. signal 10 error
    By Fox101 in forum C Programming
    Replies: 3
    Last Post: 10-27-2007, 11:47 PM
  4. error signal handling
    By bizzu in forum C Programming
    Replies: 5
    Last Post: 04-29-2002, 03:12 PM
  5. THe 'signal' function question
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 03-22-2002, 01:29 AM