Hello_Again()
- set이라는 이름의 시그널 세트를 생성하시오.
- set을 이용하여 프로그램이 ctrl + c로 종료되지 않도록 만드시오.
- (힌트: sigprocmask를 사용할 것)
#define _GNU_SOURCE
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void) {
int cnt;
sigset_t chkset;
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGINT);
sigprocmask(SIG_SETMASK, &set, NULL);
// do not modify
cnt = 0;
for (;;) {
printf("hello\n");
fflush(stdout);
sleep(1);
cnt++;
if (!(cnt % 3)) {
raise(SIGINT);
sigprocmask(0, NULL, &chkset);
// check for other signals are blocked
// NSIG is defined in signal.h
// don't worry about the red underline
for (int i = 1; i < NSIG; i++) {
if (i == SIGINT)
continue;
if (sigismember(&set, i)) {
printf("Block just SIGINT not SIG%s(%d)\n", strsignal(i), i);
exit(EXIT_FAILURE);
}
}
// take pended masks
if (sigpending(&chkset)) {
perror("pending\n");
exit(EXIT_FAILURE);
}
// check SIGINT is successfully blocked
if (sigismember(&chkset, SIGINT)) {
printf("Success\n");
exit(EXIT_SUCCESS);
}
// fail to blocking SIGINT
else {
printf("Failed\n");
exit(EXIT_FAILURE);
}
}
}
return 0;
}
'C > Linux' 카테고리의 다른 글
[C] 2개의 파이프를 사용한 양방향 통신 프로그램 구현하기 (0) | 2023.01.03 |
---|---|
[C] alarm 시그널을 받으면 1초 간격으로 메시지 출력하는 프로그램 구현하기 (0) | 2023.01.03 |
[C] execve()를 사용하여 execlp() 구현하기 (0) | 2023.01.03 |
[C] chmod a+rX [path] 구현하기 (0) | 2023.01.03 |
[C] copy_file() 구현하기 (0) | 2023.01.03 |