~cpp
/**
code written by eternalbleu@gmail.com
desc: ctrl + c handling
**/
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void handler(int sig);
int main(int argc, char **argv)
{
int state;
int num = 0;
signal(SIGINT, handler);
while(1)
{
printf("%d : waiting \n", num ++);
sleep(2);
if(num > 5)
break;
}
return 0;
}
void handler(int sig)
{
// signal(SIGINT, handler);
printf("which signal is passed : %d \n", sig);
}
~cpp
/**
code written by eternalbleu@gmail.com
desc: ctrl + c handling
**/
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void handler(int sig);
int main(int argc, char **argv)
{
int state;
int num = 0;
struct sigaction act;
act.sa_handler = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
state = sigaction(SIGINT, &act, 0);
if (state != 0)
{
puts("sigaction() error");
exit(1);
}
while(1)
{
printf("%d : waiting \n", num ++);
sleep(2);
if(num > 5)
break;
}
return 0;
}
void handler(int sig)
{
printf("which signal is passed : %d \n", sig);
}
당연히 이해를 위해서는 함수 포인터를 알아야하며, 그 이외에는 굉장히 단순하다는 것을 알 수 있다.
~cpp
/**
code written by eternalbleu@gmail.com
desc: timer program that use SIGALRM signal.
**/
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void timer(int sig);
int main(int argc, char **argv)
{
int state;
int num = 0;
struct sigaction act;
act.sa_handler = timer;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
state = sigaction(SIGALRM, &act, 0);
if (state != 0) {
puts("sigaction() error");
exit(1);
}
alarm(5);
while(1) {
puts("waiting");
sleep(2);
}
return 0;
}
void timer(int sig)
{
puts("this is time for you to reserve");
exit(0);
}