Timer on Windows and Linux

Source: Internet
Author: User
Tags exit in signal handler

1. timer on Windows 

 

# Define win32_lean_and_mean
# Include "windows. h"

Uint tid;

Void callback timerhandler (hwnd, uint umsg, uint idevent, DWORD dwtime ){
Printf ("timer called @ [% ld]/n", dwtime );
}

Static bool winapi signalhandler (DWORD event ){
Switch (event ){
Case ctrl_c_event:
Case ctrl_break_event:
Killtimer (hwnd) null, tid );
// Handle Y not to call other handlers.
Return false;
}

Return true;
}

Int main (INT argc, char ** argv ){
MSG;

If (! Setconsolectrlhandler (signalhandler, true/* Add */)){
Printf ("setconsolectrlhandler error-% d", getlasterror ());
}
If (! (TID = settimer (hwnd) null, 0, 1000, (timerproc) timerhandler ))){
Printf ("settimer error-% d", getlasterror ());
}

While (getmessage (& MSG, (hwnd) null, 0, 0 )){
Translatemessage (& MSG); // translates virtual-key codes
Dispatchmessage (& MSG); // dispatches message to window
}
// Remove our handler from the list of handlers
Setconsolectrlhandler (signalhandler, false );
}

 

2. timer on Linux

# Include "stdio. h"
# Include "unistd. h"
# Include "errno. h"
# Include "time. h"
# Include "sys/time. h"
# Include "signal. h"

# Define sigtimer (sigrtmax)

Static timer_t tid;

Timer_t settimer (INT signo, int sec, int mode ){
Static struct sigevent sigev;
Static timer_t tid;
Static struct itimerspec itval;
Static struct itimerspec oitval;

// Create the POSIX timer to generate signo
Sigev. sigev_notify = sigev_signal;
Sigev. sigev_signo = signo;
Sigev. sigev_value.sival_ptr = & tid;

If (timer_create (clock_realtime, & sigev, & tid) = 0 ){
Itval. it_value. TV _sec = SEC/1000;
Itval. it_value. TV _nsec = (long) (SEC % 1000) * (000000l );

If (mode = 1 ){
Itval. it_interval. TV _sec = itval. it_value. TV _sec;
Itval. it_interval. TV _nsec = itval. it_value. TV _nsec;
} Else {
Itval. it_interval. TV _sec = 0;
Itval. it_interval. TV _nsec = 0;
}

If (timer_settime (TID, 0, & itval, & oitval )! = 0 ){
Perror ("time_settime Error !");
}
} Else {
Perror ("timer_create Error !");
Return-1;
}
Return tid;
}

Void signalhandler (INT signo, siginfo_t * info, void * context ){
If (signo = sigtimer ){
Struct timeval TV;
Time_t T;
Struct TM * TM;

Gettimeofday (& TV, null );
T = TV. TV _sec;
TM = localtime (& T );
Printf ("timer called @ [% 02d: % 02d: % 02d. % 03d]/n ", TM-> tm_hour, TM-> tm_min, TM-> tm_sec, TV. TV _u SEC/1000 );
} Else if (signo = SIGINT ){
Timer_delete (TID );
Perror ("crtl + C cached !");
Exit (1); // exit if crtl/C is issued
}
}

Int main (INT argc, char ** argv ){
Struct sigaction sigact;

Sigemptyset (& sigact. sa_mask );
Sigact. sa_flags = sa_siginfo;
Sigact. sa_sigaction = signalhandler;
// Set up sigaction to catch Signal
If (sigaction (sigtimer, & sigact, null) =-1 ){
Perror ("sigaction failed ");
Return-1;
}

// Establish a handler to catch Ctrl + C and use it for exiting.
Sigaction (SIGINT, & sigact, null );
 
Tid = settimer (sigtimer, 1000, 1 );
 
// Loop forever. The application will exit in the signal handler
// When a SIGINT is issued (crtl + C will do this ).
For (;) pause ();

Return 0;
}

 

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.