전역 변수로 임의 파일을 하나 열어 내용을 출력한다.
 그리고 종료 직전 atexit()를 사용해 등록한 핸들러를 이용해 파일을 닫으며
 메시지를 출력하도록 해보자.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
 
int filedes;
void end_msg(void);
 
int main(){
    ssize_t nread;
    char buffer[1024];
 
    atexit(end_msg);
 
    filedes=open("test.txt",O_RDONLY);
    while(nread=read(filedes,buffer,1024)>0)
    {   
        printf("%s",buffer);
    }   
 
}
 
void end_msg(void)
{
    printf("close filedes\n");
    close(filedes);
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

-----------------------------------------------------------------------------------------------------------------------------------

 

자식 프로세스를 생성후 SIGINT를 발생해 종료 후 부모 프로세스도 종료하게 하자

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <signal.h>
#include <sys/types.h>
#include <stdio.h>
main()
{
 
    pid_t pid;
    int count = 5;
 
    if((pid=fork())>0){
        printf("[parent] hello\n");
        pause();
        printf("[parent] bye\n");
    }   
    else if(pid==0){
        printf("[child] hello\n");
        sleep(1);
        kill(getppid(),SIGINT);
        printf("[child] bye\n");
    }   
    else
        printf("fail to fork\n");
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

sigaction 함수를 이용해서 사용자가 인터럽트 키(Ctrl+C})를 입력하면 임의 함수가 호출 되고 파일에 종료 메시지를 

"ex_sigint_out.txt"에 저장하도록 프로그램을 작성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
int flag=1;
void int_handle(int signum)
{
    int filedes;
    char *msg="Interrupted bu SIGINT..\n";
    if((filedes=open("ex_sigint_out.txt",O_RDWR|O_CREAT,0666))<0){
        perror("open error");
        return -1;
    }
    write(filedes,msg,strlen(msg));
    close(filedes);
 
    flag=0;
}
 
 
main()
{
    static struct sigaction act;
    void int_handle(int);
 
    act.sa_handler=int_handle;
    sigfillset(&(act.sa_mask));
    sigaction(SIGINT,&act,NULL);
 
    while(flag)
    {
        printf("....\n");
        sleep(1);
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

-----------------------------------------------------------------------------------------------------------------------------------

 메인 프로그램이 무한반복으로 실행되고, 1초가 대기후 "메인 프로그램이 실행중입니다..."를 출력한다.

 단, 모든 시그널은 블록하고 (sigfillset()), SIGINT 를 수신시 핸들러를 호출해서 수행한다. 

 이때 Ctrl+\ (quit),  Ctrl+z(멈춤) 키를 누르면 어떻게 되는지 확인해 보자.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <signal.h>
 
void sigint_handler(int);
int main(void)
{
    struct sigaction act;
 
    act.sa_handler = sigint_handler;
    sigfillset(&act.sa_mask);
 
    sigaction(SIGINT,&act,NULL);
 
    while(1)
    {   
        printf("메인 프로그램 실행 중....\n");
        sleep(1);
    }   
}
 
void sigint_handler(int signo){
    int ndx;
 
    printf("Ctrl-C키가 눌렸다.\n");
    printf("3초간 대기중, Ctrl-\(Quit)키 눌러짐\n");
 
    for(ndx=3;0<ndx;ndx--)
    {   
        printf("%d 초 남았습니다.\n",ndx);
        sleep(1);
    }   
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

+ Recent posts