复制view plaincopy to clipboardprint?参数/* Filename : timer.cpp Compiler : gcc 4.1.0 on Fedora Core 5 Description : Linux setitimer() set the interval to run function Synopsis : #include <sys/time.h>int Linux 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; } Release : 11/25/2006 */ #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. Linux setitimer() 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 = Linux 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) { printf("%s","Hello World!!n"); } 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.