Linux - 시그널 Signal (Ctrl+c, Ctrl+z)

Posted by ironmask84
2018. 5. 13. 00:39 나는 프로그래머다!/Linux



리눅스에서 Ctrl + C 키 조합을 통해 프로세스 종료를 많이 해보셨을 겁니다.

저 또한 그냥 개발시에 테스트 진행하다가 빨리 강제종료 시키기 위해 사용을 해왔었습니다.

그러다가 Ctrl + C가 먹히지 않는 프로세스가 발생하고는...

Ctrl + Z 를 어디선가 써본적이 있어! 라는 생각이 스쳐지나가면서 써보니 

뭔가 다시 콘솔창에 프롬프트가 상태가 되어 명령어 입력이 가능해집니다.

뭔가 근데 찜찜하기 시작합니다. 

알고 보니 이건 foreground process를 background로 전환하면서 잠깐 중지 시키는 것이더군요 ㅋㅋ

Ctrl + C와 Ctrl + Z 는 리눅스나 윈도우 같은 OS가 기본적으로 지원해주는 시스템호출을 통한 인터럽트 입니다!!

리눅스에서는 이러한 개념을 Signal 이라고 하는 것 같아요.

https://en.wikipedia.org/wiki/Unix_signal


Ctrl-C (in older Unixes, DEL) sends an INT signal ("interrupt", SIGINT); by default, this causes the process to terminate.

Ctrl-Z sends a TSTP signal ("terminal stop", SIGTSTP); by default, this causes the process to suspend execution.

Ctrl-\ sends a QUIT signal (SIGQUIT); by default, this causes the process to terminate and dump core.

Ctrl-T (not supported on all UNIXes) sends an INFO signal (SIGINFO); by default, and if supported by the command, this causes the operating system to show information about the running command.

These default key combinations with modern operating systems can be changed with the stty command.


좀 더 자세하고 다양한 내용은 아래를 참조하면 좋을 것 같습니다.

시그널의 명령어 리스트 : kill -l

시그널의 의미 (/usr/include/asm/signal.h)


자, 그럼 왜 Ctrl + C가 먹히지 않았을까..

검색을 해보자!!

리눅스 C 프로그래밍에는 signal() 이란 함수가 있습니다!!

프로세스에서 아래와 같이 호출하면, Ctrl + C가 무시되도록 설정됩니다!!

signal(SIGINT, SIG_IGN); // SIGINT 명령을 무시하도록 설정 


아래와 같이 handler를 추가해서 커스터마이즈한 활용도 가능합니다!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int  main(void)
{
     signal(SIGINT, INThandler);
     while (1)
          pause();
     return 0;
}
 
void  INThandler(int sig)
{
     char  c;
 
     signal(sig, SIG_IGN);
     printf("singnal test");
}

cs