setitimer함수를 사용할떄 while안에 다른내용을 추가가 불가능 한가요??

xoduddk123의 이미지

안녕하세요.

리눅스 일반 어플리케이션에서 사용가능한 timer를 찾던중 setitimer를 알게되었습니다.

인터넷에서 예제를 보고 따라해보니 250usec마다 메세지가 출력되는거였는데요

값을 수정하여서 1초마다 출력되게 수정을 하였습니다.

원래는 while(1);하고 끝나있었는데

제가 임의로 while문안에 내용을 넣어서 while이 따로 반복실행되면서 handler함수에서 if조건이 만족되면 특정 동작을 하게 하려고하는데요

while문안에 소스를 넣으니 아예 handler함수가 호출이 되지가 않습니다.

어떻게 해야하나요 . . ?

입력형식으로 php를 써서 소스코드르 넣어보려고하는데

잘되는지 모르겠네요....

#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
 
void timer_handler (int signum)
{
        static int count = 0;
        printf("timer expired %d timers\n", ++count);
	if(count==10){
		printf("timer count 10 sec ! ! !\n");
	}
}
 
int main ()
{
        struct sigaction sa;
        struct itimerval timer;
 
        /* Install timer_handler as the signal handler for SIGVTALRM. */
        memset (&sa, 0, sizeof (sa));
        sa.sa_handler = &timer_handler;
        sigaction (SIGVTALRM, &sa, NULL);
 
        /* Configure the timer to expire after 250 msec... */
        timer.it_value.tv_sec = 1;
        timer.it_value.tv_usec = 0;
 
        /* ... and every 250 msec after that. */
        timer.it_interval.tv_sec = 1;
        timer.it_interval.tv_usec = 0;
 
        /* Start a virtual timer. It counts down whenever this process is executing. */
        setitimer (ITIMER_VIRTUAL, &timer, NULL);
 
	printf("123123123123 \n");
        /* Do busy work.  */
        while (1){
		printf("setitimer test wait....\n");
		sleep(2);
	}
}

xoduddk123의 이미지

#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
 
void timer_handler (int signum)
{
        static int count = 0;
        printf("timer expired %d timers\n", ++count);
	if(count==10){
		printf("timer count 10 sec ! ! !\n");
	}
}
 
int main ()
{
        struct sigaction sa;
        struct itimerval timer;
 
        /* Install timer_handler as the signal handler for SIGVTALRM. */
        memset (&sa, 0, sizeof (sa));
        sa.sa_handler = &timer_handler;
        sigaction (SIGVTALRM, &sa, NULL);
 
        /* Configure the timer to expire after 250 msec... */
        timer.it_value.tv_sec = 1;
        timer.it_value.tv_usec = 0;
 
        /* ... and every 250 msec after that. */
        timer.it_interval.tv_sec = 1;
        timer.it_interval.tv_usec = 0;
 
        /* Start a virtual timer. It counts down whenever this process is executing. */
        setitimer (ITIMER_VIRTUAL, &timer, NULL);
 
	printf("123123123123 \n");
        /* Do busy work.  */
        while (1){
		printf("setitimer test wait....\n");
		sleep(2);
	}
}
goforit의 이미지



''The ITIMER_VIRTUAL timer decrements in process virtual time.
It runs only when the process is executing''
-----------------------------------------------

excerpts from
"$ man setitimer"

xoduddk123의 이미지

while(1)안에 printf같은 함수를 집어넣으면 해당 프로그램은 무한반복인 상태인것 아닌가요 ? ?

peecky의 이미지

while (1){
아주 짧은 시간동안 printf() 실행;
2초간 프로그램 실행 안 함;
}