Reputation: 22114
I read about add signal() function in the signal handler function can over write the default behaviour:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
void signalHandler();
int main(void) {
signal(SIGUSR1, signalHandler);
sleep(60);
printf("I wake up");
return 0;
}
void signalHandler() {
signal(SIGUSR1, signalHandler);// I add this line to overwrite the default behaviour
printf("I received the signal");
}
And I trigger it with another process
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
kill(5061, SIGUSR1); // 5061 is the receiver pid, kill(argv[1], SIGUSR1) doesn't working, when I get 5061 as parameter
puts("send the signal ");
return 0;
}
The receiver process wake up
as soon as it receiver the SIGUSR1
signal. How can I make the receiver continue sleep
even when it receive the signal from other process?
BTW why kill(5061, SIGUSR1);
5061 is the receiver pid, kill(argv[1], SIGUSR1)
doesn't work, when I get 5061 as parameter?
Upvotes: 0
Views: 3703
Reputation: 1
Your question (why kill(argv[1], SIGUSR1)
does not work) actually is not related to signals, but to basic C programming. Please compile with gcc -Wall -g
(i.e. all warnings and debugging information) on Linux, and improve your code till no warning is given.
Please read carefully the kill(2) man page (on Linux, after installing packages like manpages
and manpages-dev
and man-db
on Ubuntu or Debian, you can type man 2 kill
to read it on your computer). Read also carefully signal(2) and signal(7) man pages. Read these man pages several times.
Then, understand that the kill syscall is declared as
int kill(pid_t pid, int sig);
and since pid_t
is some integral type, you need to convert argv[1]
(which is a string, i.e. a char*
) to some integer (and kill(argv[1], SIGUSR1)
should not even compile without errors, since argv[1]
is not some integer but a string). So please use:
kill((pid_t) atoi(argv[1]), SIGUSR1);
The man page says also that you should use signal(SIGUSR1, SIG_DFL)
to restore the default behavior, and signal(SIGUSR1, SIG_IGN)
to ignore that signal.
Of course, you should better use sigaction(2) as the man page of signal(2)
tells you.
At last, please take the habit of reading man pages and spend hours to read good books like Advanced Linux Programming and Advanced Unix Programming. They explain things much better than we can explain in a few minutes. If you are not familiar with C programming, read also some good book on it.
Upvotes: 3
Reputation: 17332
I haven't tried it before, but you could use sigaction(2) instead and set the SA_RESTART
flag, this should make syscalls restartable.
Edit: Actually this won't work, according to the man page of signal(7) not all system calls are restartable:
The sleep(3) function is also never restarted if interrupted by a handler, but gives a success return: the number of seconds remaining to sleep.
So you should call the sleep
function again with the remaining time instead.
Upvotes: 2
Reputation: 409482
From the sleep(3)
manual page:
Return Value
Zero if the requested time has elapsed, or the number of seconds left to sleep, if the call was interrupted by a signal handler.
So instead of just calling sleep
you have to check the return value and call in a loop:
int sleep_time = 60;
while ((sleep_time = sleep(sleep_time)) > 0)
;
Upvotes: 5