next up previous
Next: 5.3 FIFOによるユーザ空間-カーネル空間の通信 Up: 5 RT-Linux APIによる実時間タスクのプログラミング Previous: 5.1 モジュールのプログラミング

5.2 リアルタイムスレッドの生成と周期実行

付録につけた sound_module1.c を例にリアルタイムスレッドの生成と周期実行 のプログラミングを説明する. リアルタイムスレッドの生成

/* スレッドオブジェクト */
pthread_t task;

int init_module(void){
  /* スレッド属性オブジェクト */
  pthread_attr_t attr;
  /* スケジューリングパラメータ */
  struct sched_param sched_param;

  /* スレッド属性オブジェクトattrの初期化 */
  pthread_attr_init (&attr);

  /* スケジューリングパラメータにプライオリティ値を設定 */
  sched_param.sched_priority = 4; 

  /* プライオリティ値をスレッド属性オブジェクトに設定する */
  pthread_attr_setschedparam (&attr, &sched_param);
            
  /* thread_code という関数をスレッドとして生成
     最後の(void *)1 はスレッドに渡す引数.*/
  ret = pthread_create (&task, &attr, 
                        thread_code, (void *)1);
}
リアルタイムスレッドの生成

void *thread_code(void t){
  /* 実行周期を第三引数でナノ秒単位で指定 */
  pthread_mask_periodic(pthread_self(), gethrtime, 
                        500000)

  while (done){
    /* 次の実行周期まで待つ.
       実行周期はpthread_make_periodic_np で指定する.*/
    pthread_wait_np()

    /* 周期実行を一次停止する.*/
    pthread_suspend_np(pthread_self())
  }
}

int my_handler(unsigned int fifo){
  /* pthread_wait_np() しているスレッドを起こす.*/
   pthread_wakeup_np()
}


generated through LaTeX2HTML. M.Inaba 平成18年5月6日