Find the answer to your Linux question:
Results 1 to 10 of 10
hey.. so i read this pdf file about signals and how to handle them by ignoring them, setting their default behavior, or assigning a function to handle them..and then i ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60

    SignalHandler error

    hey..
    so i read this pdf file about signals and how to handle them by ignoring them, setting their default behavior, or assigning a function to handle them..and then i tried writing a little program to try it..i included #include <signal.h> if you're wondering
    the code is:
    Code:
    signal(SIGHUP,(SignalHandler) handleSigHup);
    where handleSigHup is simply:
    Code:
    int handleSigHup()
    {
    	printf("inside signal handler\n");
    	return 0;
    }
    i wrote code as simple as this to try it..and this is the error it gave me:
    lister.c: In function ‘main’:
    error: ‘SignalHandler’ undeclared (first use in this function)
    error: (Each undeclared identifier is reported only once
    error: for each function it appears in.)
    error: expected ‘)’ before ‘handleSigHup’

    the error is in the casting made on the function...any idea what's missing?..thanks

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    1. Avoid casting wherever you can. It covers a multitude of sins. You don't need it for signal handling.
    2. Could you please post a program which duplicates this error, tiny enough for us not to choke on it, but complete (all #includes, etc.) enough so we can compile it and get your error message? Once you've pasted the code into your editing window, highlight the code and then click the # icon above your editing window, to maximize legibility.

  3. #3
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include  <ctype.h>
    #include  <signal.h>
    int handleSigHup()
    {
    printf("inside handler\n");
    return 0;
    }
    
    int main()
    {
    signal(SIGHUP,(SignalHandler)handleSigHup);
    return 0;
    }
    ERRORS:
    trial.c: In function ‘main’:
    trial.c:15: error: ‘SignalHandler’ undeclared (first use in this function)
    trial.c:15: error: (Each undeclared identifier is reported only once
    trial.c:15: error: for each function it appears in.)
    trial.c:15: error: expected ‘)’ before ‘handleSigHup

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include  <ctype.h>
    #include  <signal.h>
    void handleSigHup(int the_signal)
    {
    printf("got signal %d\n",the_signal);
    }
    
    int main()
    {
    signal(SIGHUP,handleSigHup);
    return 0;
    }
    If you end up using signals seriously, you'll probably find yourself using sigaction() instead of signal().

    Hope this helps.

  5. #5
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60
    no..it didn't work..i tried it on fedora, and it compiled..i tried to run it, and i openned another terminal and typed the command "kill SIGHUP 5131" where 5131 was the process id of the program i wrote..i did this while the progarm was running, and when i entered the command, my program on the first terminal typed: "Terminated" and exited the program, where it should have printed the printf instruction in your code..
    the second terminal (which i called the kill command from) , gave me this response:
    "bash: kill: SIGHUP: arguments must be process or job IDs"

    shouldnt the function handleSigHup() in the code above handle the SIGHUP signal instead of allowing it to terminate the program?

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    As wje_lf mentioned you can use sigaction() instead of signal(), try this (not tested):

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include  <signal.h>
    
    void handleSigHup(int the_signal)
    {
      printf("got signal %d\n",the_signal);
    }
    
    int main ()
    {
      struct sigaction sa;
    
      memset (&sa, 0, sizeof (sa));
      sa.sa_handler = &handleSigHup;
      sigaction (SIGHUP, &sa, NULL);
      return 0;
    }
    Regards

  7. #7
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60
    Quote Originally Posted by Franklin52 View Post
    As wje_lf mentioned you can use sigaction() instead of signal(), try this (not tested):

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include  <signal.h>
    
    void handleSigHup(int the_signal)
    {
      printf("got signal %d\n",the_signal);
    }
    
    int main ()
    {
      struct sigaction sa;
    
      memset (&sa, 0, sizeof (sa));
      sa.sa_handler = &handleSigHup;
      sigaction (SIGHUP, &sa, NULL);
      return 0;
    }
    Regards

    tried it too..same output..main program terminates, and other terminal prints out
    bash: kill: SIGHUP: arguments must be process or job IDs

  8. #8
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I expanded slightly the program I posted in post #4 so that it waits around for signals instead of exiting immediately. Here is that expanded program:
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include  <ctype.h>
    #include  <signal.h>
    void handleSigHup(int the_signal)
    {
    printf("got signal %d\n",the_signal);
    }
    
    int main()
    {
    signal(SIGHUP,handleSigHup);
    for(;;)
    {
    sleep(1);
    }
    return 0;
    }
    If you run the above program and try to kill it with this command from another terminal:
    Code:
    kill SIGHUP 23245
    then it will also die immediately. Why? Because of the syntax of the kill command. It takes one or more process id's, preceded by an optional signal specification.

    The kill command is implemented internally in bash, so you can find out about it by entering this at the command line:
    Code:
    help kill
    You'll see that there are several ways of specifing which signal you want. Each one of those ways starts with a hyphen. So when you specified the signal you wanted by saying
    Code:
    SIGHUP
    bash didn't recognize that as a signal name. It recognized that as a badly formed process id (and complained about that). It then went on to the next process id on the command line, and sent to that process not the SIGHUP signal, but the default SIGTERM signal. Since you weren't catching the SIGTERM signal, your process, um, TERMinated. (With apologies to my governator).

  9. #9
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60
    ya u were right..it was the hyphen..lol..even though i did read the "man kill" but it was just a stupid mistake i guess..thanks

  10. #10
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Good, so that's solved.

    But remember, since the kill command is implemented internally in bash, technically you want to rely on
    Code:
    help kill
    instead of
    Code:
    man kill
    for the syntax. This is true for other bash commands as well. For example, try this:
    Code:
    $(which echo) --version
    echo --version
    The first one is the free-standing program, and the second one is the bash command.

    For a list of interesting bash commands (for which you might want to get help), do this at the command line:
    Code:
    help
    Hope this, um, helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...