1

アプリケーションで使用するために Linux のタイマーを理解しようとしています。複数のソースからコードを収集し、次のプログラムを作成しました。

#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 秒ごと) でキューのタイムアウトをチェックします。メッセージが見つかった場合は削除します。

4

1 に答える 1