Alarm

지정한 시간이 경과한 후에 자신에게 SIGALRM 시그널을 보낸다.

#include <unistd.h>

unsigned int alarm(unsigned int seconds);

seconds 초 단위의 시간이다.

반환값: 지정한 시간 중 남은 시간을 반환한다. 0 이상의 값이다.

 

alarm 설정은 한번에 하나만 등록할 수 있다.

여러 개를 누적해서 등록할 수 없다.

마지막에 등록한 하나의 alarm만 유효하다.

이전에 등록된 alarm은 취소된다.

지금까지 사용했던 sleep 함수는 alarm을 사용하여 구현되었다.

sleep과 alarm은 함께 사용하지 않는 것이 좋다.

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
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
static void sig_handler(int);
int counter=0;
int main(void)
{
    if(signal(SIGALRM,sig_handler)==SIG_ERR) {
        perror("can't catch SIGALRM");
        return -1;
    }
    alarm(1);
while(1)
    {    
    }
    return 0;
 
}
 
void sig_handler(int signo) // argument is signal number
{
    printf("Alarm count:%d !\n",counter++);
    alarm(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
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 <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
 
void timeover(int signum)
{
    printf("\n\ntime over !! \n\n");
    exit(0);
}
 
main()
{
    char buf[1024];
    char *alpha="abcdefg";
 
    int timelimit;
    struct sigaction act;
 
    act.sa_handler=timeover;
    sigaction(SIGALRM,&act,NULL);
 
    printf("input timelimit (sec)..\n");
    scanf("%d",&timelimit);
 
    alarm(timelimit);
 
    printf("START!!\n>");
    scanf("%s",buf);
 
    if(!strcmp(buf,alpha))
        printf("well done... you succeed!\n");
    else
        printf("sorry.. you fail!\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

 

 

sleep 함수

#inc;ude <unistd.h>

unsigned int sleep(unsigned int secnods);

호출한 프로세스를 중지시킨다.

1. secnods 인자로 지정한 시간이 지날 때까지 혹은

2. 시그널이 전달되고 해당 시그널 처리 함수가 리턴할 때 까지

스케줄링 등의 이유로 정확한 시간은 보장되지 않는다.

리턴 값은 0 (1의 경우) 혹은 남은 시간 (2의 경우) 이다.

 

sleep 함수의 구현

alarm 함수와 pause 함수를 이용한 구현의 문제점

- 이미 설정해 놓은 알람이 있는 경우에 alarm 호출에 의해 해제, alarm 함수의 리턴 값을 확인하여 알람을 재설정

- SIGALRM 시그널의 처리 방법을 새로 설정, signal 함수의 리턴 값을 확인하여 리턴하기 전에 복구

- alarm 함수와 pause 함수의 호출 사이에 경쟁이 발생할 수 있다.

 --pause 함수가 호출되기 전에 SIGALRM 시그널이 발생하여 처리될 수 있다.

 --setjmp 함수를 사용하여 해결한다. 다른 시그널의 처리 함수가 같이 사용될 때 문제가 발생할 수 있다.

 

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 <stdio.h>
#include <signal.h>
#include <unistd.h>
 
static void sig_alrm(int signo)
{
    return//nothing to do,just return to wake up the pause
}
 
unsigned int sleep1(unsigned int neces)
{
    if(signal(SIGALRM,sig_alrm) == SIG_ERR) return(neces);
    alarm(neces); //start the timer
    pause(); //next caught signal wakes us up
    return(alarm(0)); //turn off timer, return unslept time
}
 
int main()
{
    sleep1(10);
    return 0;
}
 
 
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