Advertisement
Advertisement
| 04.27.2008 at 07:29PM PDT, ID: 23357674 |
|
[x]
Attachment Details
|
||
|
[x]
The Solution Rating System
|
||
With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.
Your Input Matters If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support. Thank you! |
||
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: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: |
#include <time.h>
#include <signal.h>
#define SIGTIMER (SIGRTMAX)
#define ONESHOTTIMER (SIGRTMAX-1)
timer_t SetTimer(int signo, int sec, int mode);
void SignalHandler(int signo, siginfo_t * info, void *context);
timer_t timerid,oneshotTimer;
int main()
{
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);
timerid = SetTimer(SIGTIMER, 1000, 1);
oneshotTimer = SetTimer(ONESHOTTIMER, 10000, 0);
for(;;)
;
return 0;
}
timer_t SetTimer(int signo, int sec, int mode)
{
struct sigevent sigev;
timer_t timerid;
struct itimerspec itval;
struct itimerspec oitval;
// Create the POSIX timer to generate signo
sigev.sigev_notify = SIGEV_SIGNAL;
sigev.sigev_signo = signo;
sigev.sigev_value.sival_ptr = &timerid;
if (timer_create(CLOCK_REALTIME, &sigev, &timerid) == 0) {
itval.it_value.tv_sec = sec / 1000;
itval.it_value.tv_nsec = (long)(sec % 1000) * (1000000L);
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(timerid, 0, &itval, &oitval) != 0) {
perror("time_settime error!");
}
} else {
perror("timer_create error!");
return -1;
}
return timerid;
}
void SignalHandler(int signo, siginfo_t * info, void *context)
{
if (signo == SIGTIMER) {
puts("Periodic timer");
}
else if (signo == ONESHOTTIMER) {
puts("One-short timer");
}
else if (signo == SIGINT) {
timer_delete(oneshotTimer);
timer_delete(timerid);
perror("Ctrl + C cached!\n");
exit(1);
}
}
|