Drkcore

06 10 2009 Linux Tweet

Unix/Linuxプログラミング理論と実践 7章 (インターバルタイマ)

interval timerを使うと定期的にシグナルを送れる。

ProductName Unix/Linuxプログラミング理論と実践
Bruce Molay
アスキー・メディアワークス / ¥ 6,090 ()
在庫あり。

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <signal.h>

int main()
{
  void countdown(int);

  signal(SIGALRM, countdown);
  if ( set_ticker(50) == -1)
    perror("set_ticker");
  else
    while( 1 )
      pause();
  return 0;
}

void countdown(int signum)
{
  static int num = 10;
  printf("%d ..", num--);
  fflush(stdout);
  if ( num < 0 ){
    printf("DONE!\n");
    exit(0);
  }
}

int set_ticker( int n_msecs )
{
  struct itimerval new_timeset;
  long n_sec, n_usecs;

  n_sec = n_msecs / 1000;
  n_usecs = ( n_msecs % 1000 ) * 1000L;

  new_timeset.it_interval.tv_sec = n_sec;
  new_timeset.it_interval.tv_usec = n_usecs;

  new_timeset.it_value.tv_sec = n_sec;
  new_timeset.it_value.tv_usec = n_usecs;

  return setitimer(ITIMER_REAL, &new_timeset, NULL);
}

sleepは秒単位だけどusleep使えばmicrosecondで一時停止できる

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021