2013-04-11 2 views
1

프로세스 타이머 리눅스, 나는 여러 소스 코드를 수집하고 내가 proctect하기 위해 노력하고있어내가 내 응용 프로그램에서 사용하는 리눅스에서 타이머를 이해하려고 노력 중이 야

#include <stdlib.h> 
#include <unistd.h> 
#include <stdio.h> 
#include <signal.h> 
#include <time.h> 
#include <pthread.h> 

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; 
#define CLOCKID CLOCK_REALTIME 
#define SIG SIGUSR1 
timer_t timerid; 


void *print_message_function(void *ptr); 
static void handler(int sig, siginfo_t *si, void *uc) 
{ 

    pthread_mutex_lock(&mutex1); 
    printf("Caught signal %d from timer\n", sig); 
    //it will be working like this in real application 
    // 1. check the front of the queue, if timeout then detete it from queue 

    pthread_mutex_unlock(&mutex1); 


} 

int main(int argc, char *argv[]) 
{ 
    struct sigevent sev; 
    struct itimerspec its; 
    long long freq_nanosecs; 
    sigset_t mask; 
    struct sigaction sa; 

    printf("Establishing handler for signal %d\n", SIG); 
    sa.sa_flags = SA_SIGINFO; 
    sa.sa_sigaction = handler; 
    sigemptyset(&sa.sa_mask); 
    sigaction(SIG, &sa, NULL); 

    sev.sigev_notify = SIGEV_SIGNAL; 
    sev.sigev_signo = SIG; 
    sev.sigev_value.sival_ptr = &timerid; 
    timer_create(CLOCKID, &sev, &timerid); 
    /* Start the timer */ 

    its.it_value.tv_sec = 1; 
    its.it_value.tv_nsec = 0; 
    its.it_interval.tv_sec = its.it_value.tv_sec; 
    its.it_interval.tv_nsec = its.it_value.tv_nsec; 

    timer_settime(timerid, 0, &its, NULL); 


    pthread_t thread1, thread2; 
    char *message1 = "Thread 1"; 
    int iret1 ; 

    /* Create independent threads each of which will execute function */ 

    iret1 = pthread_create(&thread1, NULL, print_message_function, (void*) message1); 

    pthread_join(thread1, NULL); 

    printf("Thread 1 returns: %d\n",iret1); 
    exit(0); 
} 


void *print_message_function(void *ptr) 
{ 
    char *message; 
    message = (char *) ptr; 
    int i; 
    for(i=0;; i++){ 
    pthread_mutex_lock(&mutex1); 
    printf("%s \n", message); 
     //it will be working like this in real application 
     // 1. check if a response received from the network 
     // 2. if received then delete from queue 

    pthread_mutex_unlock(&mutex1); 
    usleep(50022); 
    } 
    pause(); 
} 

다음 프로그램을 만들었어요 이 경우에 중요한 부분은 printf입니다. printf는 단지 예제입니다. 제 응용 프로그램에서는 실제로 대기열입니다. 그래서 스레드는 항상 printf에서 작업 할 것이고 타이머가 준비되면 준비가됩니다. 뮤텍스를 누른 다음 인쇄, 위의 옳은 방법은 그것을 할 수 있습니까? 실제 응용 프로그램에서는 스레드가 응답을 기다리는 중입니다. 다른 응용 프로그램에서 응답을 받으면 대기열에 액세스합니다. 타이머는 항상 일정 간격 (예 : 2 초마다)으로 대기열을 검사합니다. timedout 메시지를 발견하고 발견되면 삭제하십시오.

+0

http://codereview.stackexchange.com/에 더 적합하지 않습니까? – Kninnug

답변

1

아니요, 올바른 방법은 아닙니다. singla 핸들러에서만 비동기 신호 안전 함수를 호출 할 수 있습니다. 그렇지 않으면 동작은 정의되어

가«신호 처리기 기능이 다른 프로그램의 실행 일부 임의의 지점에서 중단 될 수 를 처리하기 때문에, 매우 신중해야한다. POSIX는 "안전 기능"이라는 개념을 가지고 있습니다. 신호 가 안전하지 않은 함수의 실행을 중단하고, 핸들러는 안전하지 않은 함수를 호출하는 경우, 프로그램의 동작은 보증되지 않습니다. 좀 더 자세한 내용은»

, man 7 signal를 참조하십시오.

Linux 용으로 작성하는 경우 signalfd을 사용해보십시오. 다른 운영 체제에는 유사한 대안이 있습니다. 긴 이야기를 보려면«How Not To Write a Signal Handler»을 참조하십시오.

희망이 있습니다. 행운을 빕니다!

관련 문제