zoukankan      html  css  js  c++  java
  • linux 中的setitimer()函数

    /* *
    #include <sys/time.h> 
    int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue);
     
    struct itimerval

         struct timerval it_interval; 
         struct timerval it_value; 
    }
     
    struct timeval
    {
         long tv_sec; 
         long tv_usec;
    }  
    */
     
    #include <stdio.h>    // for printf()  
    #include <unistd.h>   // for pause()  
    #include <signal.h>   // for signal()  
    #include <string.h>   // for memset()  
    #include <sys/time.h> // struct itimeral,setitimer()
    #include <time.h>     // for localtime(),asctime(),time()
     
     
    void printMsg(int);  
     
     
    int main()

     
      // Get system call result to determine successful or failed  
     
      int res = 0; 
     
      // Register printMsg to SIGALRM  
     
      signal(SIGALRM, printMsg);    
     
      struct itimerval tick; 
     
      // Initialize struct  
     
      memset(&tick, 0, sizeof(tick)); 
     
      // Timeout to run function first time  
     
      tick.it_value.tv_sec = 1;  // sec  
     
      tick.it_value.tv_usec = 0; // micro sec.  
     
      // Interval time to run function  
     
      tick.it_interval.tv_sec = 1; 
     
      tick.it_interval.tv_usec = 0; 
     
      // Set timer, ITIMER_REAL : real-time to decrease timer,    
      //                          send SIGALRM when timeout  
     
      res = setitimer(ITIMER_REAL, &tick, NULL); 
     
      if (res)
     { 
     
         printf("Set timer failed!!/n"); 
     
       }  
     
      // Always sleep to catch SIGALRM signal  
     
       while(1)
      { 
     
         pause(); 
     
       }  
       
      return 0;   
     

     
     
      
    void printMsg(int num)
    {   
       time_t now;
       time(&now); 
        printf("Hello World!!  %s ", asctime(localtime(&now)));   

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

     Hello World!!  Tue Jan 31 11:00:49 2012
     Hello World!!  Tue Jan 31 11:00:50 2012
     Hello World!!  Tue Jan 31 11:00:51 2012
     Hello World!!  Tue Jan 31 11:00:52 2012
     Hello World!!  Tue Jan 31 11:00:53 2012
     Hello World!!  Tue Jan 31 11:00:54 2012
     Hello World!!  Tue Jan 31 11:00:55 2012
     Hello World!!  Tue Jan 31 11:00:56 2012
     Hello World!!  Tue Jan 31 11:00:57 2012
     Hello World!!  Tue Jan 31 11:00:58 2012
     Hello World!!  Tue Jan 31 11:00:59 2012
     Hello World!!  Tue Jan 31 11:01:00 2012
     Hello World!!  Tue Jan 31 11:01:01 2012
     Hello World!!  Tue Jan 31 11:01:02 2012
     Hello World!!  Tue Jan 31 11:01:03 2012
     Hello World!!  Tue Jan 31 11:01:04 2012
     Hello World!!  Tue Jan 31 11:01:05 2012
     Hello World!!  Tue Jan 31 11:01:06 2012
     Hello World!!  Tue Jan 31 11:01:07 2012
     Hello World!!  Tue Jan 31 11:01:08 2012
     Hello World!!  Tue Jan 31 11:01:09 2012
     Hello World!!  Tue Jan 31 11:01:10 2012
     Hello World!!  Tue Jan 31 11:01:11 2012
     Hello World!!  Tue Jan 31 11:01:12 2012
     Hello World!!  Tue Jan 31 11:01:13 2012
     Hello World!!  Tue Jan 31 11:01:14 2012
     Hello World!!  Tue Jan 31 11:01:15 2012
     Hello World!!  Tue Jan 31 11:01:16 2012
     Hello World!!  Tue Jan 31 11:01:17 2012
     Hello World!!  Tue Jan 31 11:01:18 2012
     Hello World!!  Tue Jan 31 11:01:19 2012
     Hello World!!  Tue Jan 31 11:01:20 2012
     Hello World!!  Tue Jan 31 11:01:21 2012
     Hello World!!  Tue Jan 31 11:01:22 2012
     Hello World!!  Tue Jan 31 11:01:23 2012
     Hello World!!  Tue Jan 31 11:01:24 2012
     Hello World!!  Tue Jan 31 11:01:25 2012
     Hello World!!  Tue Jan 31 11:01:26 2012
     Hello World!!  Tue Jan 31 11:01:27 2012
     Hello World!!  Tue Jan 31 11:01:28 2012
     Hello World!!  Tue Jan 31 11:01:29 2012
     Hello World!!  Tue Jan 31 11:01:30 2012
     Hello World!!  Tue Jan 31 11:01:31 2012
     Hello World!!  Tue Jan 31 11:01:32 2012
     Hello World!!  Tue Jan 31 11:01:33 2012
     Hello World!!  Tue Jan 31 11:01:34 2012
     Hello World!!  Tue Jan 31 11:01:35 2012
     Hello World!!  Tue Jan 31 11:01:36 2012
     Hello World!!  Tue Jan 31 11:01:37 2012
     Hello World!!  Tue Jan 31 11:01:38 2012
     Hello World!!  Tue Jan 31 11:01:39 2012
     Hello World!!  Tue Jan 31 11:01:40 2012
     Hello World!!  Tue Jan 31 11:01:41 2012
     Hello World!!  Tue Jan 31 11:01:42 2012
     Hello World!!  Tue Jan 31 11:01:43 2012
     Hello World!!  Tue Jan 31 11:01:44 2012
     Hello World!!  Tue Jan 31 11:01:45 2012
     Hello World!!  Tue Jan 31 11:01:46 2012
     Hello World!!  Tue Jan 31 11:01:47 2012
     Hello World!!  Tue Jan 31 11:01:48 2012
     Hello World!!  Tue Jan 31 11:01:49 2012
     Hello World!!  Tue Jan 31 11:01:50 2012
     Hello World!!  Tue Jan 31 11:01:51 2012
     Hello World!!  Tue Jan 31 11:01:52 2012
     Hello World!!  Tue Jan 31 11:01:53 2012
     Hello World!!  Tue Jan 31 11:01:54 2012
     Hello World!!  Tue Jan 31 11:01:55 2012
     Hello World!!  Tue Jan 31 11:01:56 2012
     Hello World!!  Tue Jan 31 11:01:57 2012
     Hello World!!  Tue Jan 31 11:01:58 2012
     Hello World!!  Tue Jan 31 11:01:59 2012

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

  • 相关阅读:
    Git命令大全
    window系统查看端口被哪个进程占用了
    字体大小自适应纯css解决方案
    CSS3的rem设置字体大小
    javascript同名变量
    西部数码云服务器手记
    十年,站酷已成设计论坛霸主,博客园却成无兵之将
    PHP的性能大坑--strtotime函数
    csv表格处理(上)-- JS 与 PHP 协作导入导出
    致互联网--那些我浅尝则止的昙花
  • 原文地址:https://www.cnblogs.com/Neddy/p/2332713.html
Copyright © 2011-2022 走看看