프로세스 타이머 리눅스, 나는 여러 소스 코드를 수집하고 내가 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 메시지를 발견하고 발견되면 삭제하십시오.
http://codereview.stackexchange.com/에 더 적합하지 않습니까? – Kninnug