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 ...
- 10-22-2007 #1Just 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:
where handleSigHup is simply:Code:signal(SIGHUP,(SignalHandler) handleSigHup);
i wrote code as simple as this to try it..and this is the error it gave me:Code:int handleSigHup() { printf("inside signal handler\n"); return 0; }
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
- 10-22-2007 #2
- Avoid casting wherever you can. It covers a multitude of sins. You don't need it for signal handling.
- 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.
- 10-22-2007 #3Just Joined!
- Join Date
- Oct 2007
- Posts
- 60
ERRORS: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; }
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
- 10-22-2007 #4If you end up using signals seriously, you'll probably find yourself using sigaction() instead of signal().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; }
Hope this helps.
- 10-23-2007 #5Just 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?
- 10-23-2007 #6Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
As wje_lf mentioned you can use sigaction() instead of signal(), try this (not tested):
RegardsCode:#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; }
- 10-23-2007 #7Just Joined!
- Join Date
- Oct 2007
- Posts
- 60
- 10-23-2007 #8
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:
If you run the above program and try to kill it with this command from another terminal: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; }
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.Code:kill SIGHUP 23245
The kill command is implemented internally in bash, so you can find out about it by entering this at the command line:
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 sayingCode:help kill
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).Code:SIGHUP
- 10-23-2007 #9Just 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-23-2007 #10
Good, so that's solved.
But remember, since the kill command is implemented internally in bash, technically you want to rely on
instead ofCode:help kill
for the syntax. This is true for other bash commands as well. For example, try this:Code:man kill
The first one is the free-standing program, and the second one is the bash command.Code:$(which echo) --version echo --version
For a list of interesting bash commands (for which you might want to get help), do this at the command line:
Hope this, um, helps.Code:help


Reply With Quote
