1. signal ? ¶
μν€ νλμμ μ μλ₯Ό λΉλ¦¬μλ©΄ μκ·Έλμ ν νλ‘μΈμμ κΈ°ν λ€λ₯Έ νλ‘μΈμ€ μ¬μ΄μ μ μ‘λλ λΉλκΈ°μ μ΄λ²€νΈ λΌκ³ νλ€.
μ λμ€ μμ€ν
μμ μ΄λ€ μ΄λ²€νΈ(event)κ° λ°μνλ©΄ μ΄κ²μ νλ‘μΈμ€ μ¬μ΄μ μ리λ μλ¨μΌλ‘ μκ·Έλμ΄ μ¬μ©λλ€.
μ½κ² MFC μ λΉμ νμ¬ μ€λͺ νλ μκ·Έλμ λ©μμ§μ λΉμ·νλ€κ³ 보면 λλ€. μ€μ λ‘ signal ν¨μλ₯Ό μ΄μ©ν΄μ μ°λ¦¬λ MFCμμ μ΄λ²€νΈμ μ΄λ²€νΈ νΈλ€λ¬λ₯Ό μ°κ²°νλ― νλ‘κ·Έλλ°μ νλ€λ μ¬μ€μ μ μ μλ€. (μ΄λ κ² λ³΄λ©΄ μ λ§λ‘ μ λμ€κ° Cλ‘ μ§μ¬μλ€λ κ²μ νμ€νκ² μ μ μλ€.)
μ½κ² MFC μ λΉμ νμ¬ μ€λͺ νλ μκ·Έλμ λ©μμ§μ λΉμ·νλ€κ³ 보면 λλ€. μ€μ λ‘ signal ν¨μλ₯Ό μ΄μ©ν΄μ μ°λ¦¬λ MFCμμ μ΄λ²€νΈμ μ΄λ²€νΈ νΈλ€λ¬λ₯Ό μ°κ²°νλ― νλ‘κ·Έλλ°μ νλ€λ μ¬μ€μ μ μ μλ€. (μ΄λ κ² λ³΄λ©΄ μ λ§λ‘ μ λμ€κ° Cλ‘ μ§μ¬μλ€λ κ²μ νμ€νκ² μ μ μλ€.)
2. λνμ signal ¶
SIGABRT - process aborted
SIGALRM - signal raised by alarm
SIGBUS - bus error "access to undefined portion of memory object"(SUS)
SIGCHLD - child process terminated, stopped (*or continued)
SIGCONT - continue if stopped
SIGFPE - floating point exception -- "erroneous arithmetic operation"(SUS)
SIGHUP - hangup
SIGILL - illegal instruction
SIGINT - interrupt
SIGKILL - kill
SIGPIPE - write to pipe with no one reading
SIGQUIT - quit
SIGSEGV - segmentation violation
SIGSTOP - stop executing
SIGTERM - termination
SIGTSTP - terminal stop signal
SIGTTIN - background process attempting to read ("in")
SIGTTOU - background process attempting to write ("out")
SIGUSR1 - user defined 1
SIGUSR2 - user defined 2
SIGURG - urgent data available on socket
SIGALRM - signal raised by alarm
SIGBUS - bus error "access to undefined portion of memory object"(SUS)
SIGCHLD - child process terminated, stopped (*or continued)
SIGCONT - continue if stopped
SIGFPE - floating point exception -- "erroneous arithmetic operation"(SUS)
SIGHUP - hangup
SIGILL - illegal instruction
SIGINT - interrupt
SIGKILL - kill
SIGPIPE - write to pipe with no one reading
SIGQUIT - quit
SIGSEGV - segmentation violation
SIGSTOP - stop executing
SIGTERM - termination
SIGTSTP - terminal stop signal
SIGTTIN - background process attempting to read ("in")
SIGTTOU - background process attempting to write ("out")
SIGUSR1 - user defined 1
SIGUSR2 - user defined 2
SIGURG - urgent data available on socket
SIGPOLL - pollable event
SIGPROF - profiling timer expired
SIGSYS - bad syscall
SIGTRAP - trace/breakpoint trap
SIGVTALRM - signal raised by timer counting virtual time -- "virtual timer expired"(SUS)
SIGXCPU - CPU time limit exceeded
SIGXFSZ - file size limit exceeded
SIGPROF - profiling timer expired
SIGSYS - bad syscall
SIGTRAP - trace/breakpoint trap
SIGVTALRM - signal raised by timer counting virtual time -- "virtual timer expired"(SUS)
SIGXCPU - CPU time limit exceeded
SIGXFSZ - file size limit exceeded
3. sample code ¶
int signal() μμ
int sigaction() μμ
μ΄λ―Έ OS μμ λͺ¨λ μ²λ¦¬λ₯Ό λ€ν΄μ£ΌκΈ° λλ¬Έμ νλ‘κ·Έλλ¨Έλ νΈλ€λ¬λ§ μ μνλ©΄ λ§μ¬ OKλ€
~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); }λΉμ°ν μ΄ν΄λ₯Ό μν΄μλ ν¨μ ν¬μΈν°λ₯Ό μμμΌνλ©°, κ·Έ μ΄μΈμλ κ΅μ₯ν λ¨μνλ€λ κ²μ μ μ μλ€.
μ΄λ―Έ OS μμ λͺ¨λ μ²λ¦¬λ₯Ό λ€ν΄μ£ΌκΈ° λλ¬Έμ νλ‘κ·Έλλ¨Έλ νΈλ€λ¬λ§ μ μνλ©΄ λ§μ¬ OKλ€
4. signal μ μ΄μ©ν νμ΄λ¨Έ ¶
~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); }