Linux setitimer parameter settings

Source: Internet
Author: User

Linux setitimer is a commonly used software for computer players. Then I will study and study Linux setitimer in depth. Here I will discuss with you how to use Linux setimer, hoping to help you. Linux setitimer () is a Linux API, not the Standard Library of C language. Linux setitimer () has two functions. One is to execute a function after a specified period of time, the second is to execute a function in each cell for a period of time. The following program demo uses Linux setitimer ().

 
 
  1. view plaincopy to clipboardprint?/*    
  2. Filename    : timer.cpp    
  3. Compiler    : gcc 4.1.0 on Fedora Core 5    
  4. Description : Linux setitimer() set the interval to run function    
  5. Synopsis    : #include <sys/time.h>    
  6. int Linux setitimer(int which, const struct itimerval *value, struct itimerval *ovalue);    
  7. struct itimerval {    
  8. struct timerval it_interval;    
  9. struct timerval it_value;    
  10. };    
  11. struct timeval {    
  12. long tv_sec;    
  13. long tv_usec;    
  14. }  
  15. Release     : 11/25/2006    
  16. */     
  17. #include <stdio.h>    // for printf()     
  18. #include <unistd.h>   // for pause()     
  19. #include <signal.h>   // for signal()     
  20. #include <string.h>   // for memset()     
  21. #include <sys/time.h> // struct itimeral. Linux setitimer()     
  22. void printMsg(int);     
  23. int main() {     
  24. // Get system call result to determine successful or failed     
  25. int res = 0;     
  26. // Register printMsg to SIGALRM     
  27. signal(SIGALRM, printMsg);     
  28. struct itimerval tick;     
  29. // Initialize struct     
  30. memset(&tick, 0, sizeof(tick));     
  31. // Timeout to run function first time     
  32. tick.it_value.tv_sec = 1;  // sec     
  33. tick.it_value.tv_usec = 0; // micro sec.     
  34. // Interval time to run function     
  35. tick.it_interval.tv_sec = 1;     
  36. tick.it_interval.tv_usec = 0;     
  37. // Set timer, ITIMER_REAL : real-time to decrease timer,     
  38. //            send SIGALRM when timeout     
  39. res = Linux setitimer(ITIMER_REAL, &tick, NULL);     
  40. if (res) {     
  41. printf("Set timer failed!!\n");     
  42. }     
  43. // Always sleep to catch SIGALRM signal     
  44. while(1) {     
  45. pause();     
  46. }     
  47. return 0;       
  48. }     
  49. void printMsg(int num) {     
  50. printf("%s","Hello World!!\n");     
  51. }     
  52.  

When the time for Linux setitimer () to execute the timer is reached, it will call SIGALRM signal. Therefore, in line 30th, the function to be executed by signal () is specified to SIGALRM. In line 3, call Linux setitimer () to set timer, but Linux setitimer ()

The second parameter is sturct, which is used to set the timeout time. Therefore, this struct is set for rows 36th to 40th. Itimerval. it_value sets the number of seconds after the first function execution. itimerval. it_interval sets the number of seconds after the function execution. If you only want to delay the function execution for a period of time, you only need to set itimerval. it_value.

If you want to set the interval to execute the function for a period of time, it_value and it_interval must be set. Otherwise, the function cannot be executed for the first time. The TV _sec values for rows 36th and 39th are sec, 37th and 40 Act micro sec (0.001 sec ). ITIMER_REAL, the first parameter of row 43rd, indicates that timer is reduced in real-time mode, and SIGALRM signal is sent out in timeout.

The third parameter stores the old timeout value. If this parameter is not required, specify NULL. The command system enters the sleep state for pause () of Line 1. Wait for any signal and use the while (1) infinite loop to execute pause (), in this way, you can always receive SIGALRM signal to execute the function at intervals. If while (1) is removed, the function will only be executed once.

  1. Common LinuxYUM commands
  2. A Brief Introduction to the use of Linux Operators
  3. One of the methods to modify the default Linux Startup System
  4. Install cross compiler in Linux SkyEye
  5. Linux rpm installation source code Packaging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.