sigprocmask oldset
2019. 7. 24. 16:02ㆍ임베디드/리눅스시스템프로그래밍
다른 일에 방해를 받지 않고, 매우 중요한 일을 반드시 처리해야 할 경우 sigprocmask가 사용된다. 매우 중요한 일이라 시그널이 발생하더라도 먼저 처리하고 나중에 발생한 시그널을 처리하고 싶을 때말이다. 이렇게 시그널이 발생하더라도 대기 상태로 만드는 함수가 sigprocmask다.
그 중 sigprocmask oldset 파라미터에 NULL을 입력하게 되면 이전에 지속적으로 block을 등록한 sigset에 대해서저장하지 못한다. 하지만 oldset을 설정한다면 단발적으로 어떠한 시그널을 block을 한 후, SIG_SETMASK 마스킹을 통해 oldset으로 sigset을 바꿀 수 있다.
깃허브주소 : https://github.com/surinoel/Linux-SP/blob/master/sigprocmask_oldset.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <signal.h> | |
int main(int argc, char **argv) { | |
sigset_t set, oldset; | |
sigemptyset(&oldset); | |
sigemptyset(&set); | |
sigaddset(&set, SIGINT); | |
printf("SIGINT BLOCKING........\n"); | |
// SIG_BLOCK : 기존에 블록화된 시그널 집합에 두 번째 인수 set 시그널 집합을 추가 | |
sigprocmask(SIG_BLOCK, &set, &oldset); | |
sleep(3); | |
printf("SIGSETMASK......\n"); | |
// SIG_SETMASK : 이전 블록된 시그널 집합을 모두 지우고 두 번째 인수인 set 시그널 집합으로 설정 | |
sigprocmask(SIG_SETMASK, &oldset, NULL); | |
while(1) { | |
sleep(1); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if 0 | |
tmpset을 통해서 oldset에 SIGINT를 등록한 후 마지막에 교체하는 예제 | |
결국에는 oldset이라는 것은 이전 블록된 시그널 내용들을 넣어주게 | |
#endif | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <signal.h> | |
/* | |
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset); | |
*/ | |
int main(int argc, char **argv) | |
{ | |
sigset_t set, oldset, tmpset; | |
sigemptyset(&set); | |
sigemptyset(&oldset); | |
sigemptyset(&tmpset); | |
sigaddset(&tmpset, SIGINT); | |
if(sigprocmask(SIG_BLOCK, &tmpset, NULL) < 0) { | |
perror("sigprocmask"); | |
return -1; | |
} | |
printf("change Empty set....\n"); | |
if(sigprocmask(SIG_SETMASK, &set, &oldset) < 0) { | |
perror("sigprocmask"); | |
return -1; | |
} | |
sleep(3); | |
printf("change Old set....\n"); | |
if(sigprocmask(SIG_SETMASK, &oldset, NULL) < 0) { | |
perror("sigprocmask"); | |
return -1; | |
} | |
while(1) { | |
sleep(1); | |
} | |
return 0; | |
} |
'임베디드 > 리눅스시스템프로그래밍' 카테고리의 다른 글
detach를 통한 스레드 반환 (0) | 2019.07.26 |
---|---|
세마포어 Semaphore (0) | 2019.07.25 |
alarm과 sleep (0) | 2019.07.24 |
wait와 waitpid (0) | 2019.07.23 |
프로세스 생성 fork (0) | 2019.07.23 |