Skip to content

Instantly share code, notes, and snippets.

@master-q
Created May 23, 2012 13:32
Show Gist options
  • Save master-q/2775250 to your computer and use it in GitHub Desktop.
Save master-q/2775250 to your computer and use it in GitHub Desktop.
daemonから子プロセスへSIGUSR1を送信
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <syslog.h>
int catch_sigusr1 = 0;
int catch_sigbus = 0;
void sig_handler(int signum) {
if (signum == SIGUSR1) {
catch_sigusr1++;
}
}
void sigsender(pid_t pid) {
sleep(3);
if (kill(pid, SIGUSR1)) {
printf("*** error kill() %d.\n", errno);
}
}
void sigwaiter() {
syslog(LOG_INFO, "sigwaiter: start\n");
while(1) {
sleep(1);
if (catch_sigusr1) {
printf("...killed.\n");
syslog(LOG_INFO, "sigwaiter: killed sigusr1=%d\n", catch_sigusr1);
exit(0);
}
}
}
void set_sigaction(int signum, void (*handler)(int)) {
struct sigaction act;
act.sa_handler = handler;
sigemptyset (&act.sa_mask);
act.sa_flags = SA_RESTART | SA_NOCLDSTOP;
if (sigaction(SIGUSR1, &act, NULL)) {
printf("*** error sigaction() %d.\n", errno);
}
}
int main()
{
pid_t pid_wait;
/* sig setup */
set_sigaction(SIGUSR1, &sig_handler);
if (daemon(1, 0)) {
printf("*** daemon() error.\n");
}
/* wait */
pid_wait = fork();
if (pid_wait == 0) {
sigwaiter();
}
/* sender */
printf("pid_wait=%d\n", pid_wait);
sigsender(pid_wait);
waitpid(pid_wait, NULL, 0);
printf("Bye!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment