Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use timer in Linux C language

Shulou Source: shulou.com Published: 2022-06-03 06:43:16 09月23日 Update

Write a program (library) to realize the function of timer (timer), which can provide users with timers used many times in the same process. Here, it is required to be realized by signal. The sample code is as follows: # include # include static int time_count = 0; static struct itimerval oldtv; void set_timer () {struct itimerval itv; itv.it_interval.tv_sec = 1; itv.it_interval.tv_usec = 0; itv.it_value.tv_sec = 1; itv.it_value.tv_usec = 0 Setitimer (ITIMER_REAL, & itv, & oldtv);} void signal_handler (int m) {time_count + +; printf ("% d\ n", time_count);} int main () {signal (SIGALRM, signal_handler); set_timer (); while (1) {if (time_count==5) {printf ("time_count= 5\ n") Time_count=0;}} return 1;}

Tags: Timer code signal function user example program timer process usage method language Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Redmi Apple NVidia MariaDB