乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      如何在Linux下實(shí)現(xiàn)定時(shí)器

       jijo 2008-08-13

      Linux實(shí)現(xiàn)一個(gè)定時(shí)器,不像Win32下那樣直觀。在Win32調(diào)用SetTimer就行了,在Linux下則沒有相應(yīng)函數(shù)可以直接調(diào)用。定時(shí)器作為一個(gè)常用的功能,在Linux當(dāng)然也有相應(yīng)實(shí)現(xiàn)。下面我們看看幾種常用的方法。

      要實(shí)現(xiàn)定時(shí)器功能,最土的辦法實(shí)現(xiàn)莫過于用sleep/usleep來實(shí)現(xiàn)了。當(dāng)然,它會(huì)阻塞當(dāng)前線程,除了處理定時(shí)功能外,什么活也干不了。當(dāng)然要解決這個(gè)問題不難,創(chuàng)建一個(gè)單獨(dú)的線程來負(fù)責(zé)定時(shí)器,其它線程負(fù)責(zé)正常的任務(wù)就行了。

      要實(shí)現(xiàn)定時(shí)器功能,最簡(jiǎn)單的辦法就是ALARM信號(hào)。這種方法簡(jiǎn)單,也相應(yīng)的缺陷:用信號(hào)實(shí)現(xiàn)效率較低; 最小精度為1秒,無法實(shí)現(xiàn)高精度的定義器。簡(jiǎn)單示例:

      #include <stdio.h>

      #include <signal.h>

      static void timer(int sig)

      {

          if(sig == SIGALRM)

          {

              printf("timer\n");

          }

          return;

      }

      int main(int argc, char* argv[])

      {

          signal(SIGALRM, timer);

          alarm(1);

          getchar();

          return 0;

      }

      setitimeralarm有類似的功能,也是通過信號(hào)來實(shí)現(xiàn))

      最優(yōu)雅的方法是使用RTC機(jī)制。利用select函數(shù),你可以用單線程實(shí)現(xiàn)定時(shí)器,同時(shí)還可以處理其它任務(wù)。簡(jiǎn)單示例:

      #include <stdio.h>

      #include <linux/rtc.h>

      #include <sys/ioctl.h>

      #include <sys/time.h>

      #include <sys/types.h>

      #include <fcntl.h>

      #include <unistd.h>

      #include <errno.h>

      int main(int argc, char* argv[])

      {

          unsigned long i = 0;

          unsigned long data = 0;

          int retval = 0;

          int fd = open ("/dev/rtc", O_RDONLY);

          if(fd < 0)

          {

              perror("open");

              exit(errno);

          }

          /*Set the freq as 4Hz*/

          if(ioctl(fd, RTC_IRQP_SET, 4) < 0)

          {

              perror("ioctl(RTC_IRQP_SET)");

              close(fd);

              exit(errno);

          }

          /*Set the freq as 4Hz*/

          if(ioctl(fd, RTC_IRQP_SET, 4) < 0)

          {

              perror("ioctl(RTC_IRQP_SET)");

              close(fd);

              exit(errno);

          }

          /* Enable periodic interrupts */

          if(ioctl(fd, RTC_PIE_ON, 0) < 0)

          {

              perror("ioctl(RTC_PIE_ON)");

              close(fd);

              exit(errno);

         }

          for(i = 0; i < 100; i++)

          {

              if(read(fd, &data, sizeof(unsigned long)) < 0)

              {

                  perror("read");

                  close(fd);

                  exit(errno);

              }

              printf("timer\n");

          }

          /* Disable periodic interrupts */

          ioctl(fd, RTC_PIE_OFF, 0);

          close(fd);

          return 0;

      }

      **********************************************************************************

      調(diào)用setitimer安裝定時(shí)器:
         它有三個(gè)參數(shù)第一個(gè)設(shè)ITIMER_REAL,第二和三個(gè)參數(shù)是新的時(shí)鐘間隔和之前設(shè)置的時(shí)鐘間隔。
         struct itimerval {
             struct timeval it_interval; //設(shè)為時(shí)鐘間隔
             struct timeval it_value;   //設(shè)為第一次觸發(fā)的時(shí)鐘間隔,其實(shí)只被執(zhí)行一次,以后按照it_interval的值
      }
      下面是一個(gè)具體的例子
      #include <stdio.h>
      #include <time.h>
      #include <sys/time.h>
      #include <stdlib.h>
      #include <signal.h>
      int count = 0;
      void set_timer()
      {
      struct itimerval itv, oldtv;
      itv.it_interval.tv_sec = 1;
      itv.it_interval.tv_usec = 0;
      itv.it_value.tv_sec = 5;
      itv.it_value.tv_usec = 0;

      setitimer(ITIMER_REAL, &itv, &oldtv);
      }

      void sigalrm_handler(int sig)
      {
      count++;
      printf("timer signal.. %d\n", count);
      }

      int main()
      {
      signal(SIGALRM, sigalrm_handler);
      set_timer();
      while (count < 30)
      {}
      exit(0);
      }

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多