Skip to content

Instantly share code, notes, and snippets.

@2minchul
Last active August 5, 2019 05:03
Show Gist options
  • Save 2minchul/35fa389f2b36b6e5e607f0a49a2b0611 to your computer and use it in GitHub Desktop.
Save 2minchul/35fa389f2b36b6e5e607f0a49a2b0611 to your computer and use it in GitHub Desktop.
signal example for C
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
static void sig_usr(int);
int main(void) {
//printf("program start\n");
signal(SIGUSR1, sig_usr);
signal(SIGUSR2, sig_usr);
for (;;) pause();
}
static void sig_usr(int signo) { /* argument is signal number */
if (signo == SIGUSR1)
printf("절전모드\n");
else if (signo == SIGUSR2)
printf("일반모드\n");
else {
printf("received signal %d\n", signo);
exit(signo);
}
return;
}
#include <sys/types.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
int save_mode = 10;
int common_mode = 12;
pid_t target_pid = atoi(argv[1]);
printf("target pid: %d\n", target_pid);
while (1) {
kill(target_pid, save_mode);
sleep(1);
kill(target_pid, common_mode);
sleep(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment