How to implement a timer event handler callba...
来源:百度文库 编辑:神马文学网 时间:2024/04/28 02:14:18
How to implement a timer event handler callback function in user-space?
use select():
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 // timer_cb.c #include #include #include #include #include #include typedef signed char int8_t; typedef short int16_t; typedef int int32_t; typedef unsigned int uint32_t; typedef unsigned short uint16_t; typedef unsigned char uint8_t; struct timeval timeout; static bool led_status=true; static void SetLed(bool sw) { if(sw) led_status = false; else led_status = true; } static bool GetLed() { return led_status; } int get_gpio_time_out() { int ret = 0; timeout.tv_sec = 0; timeout.tv_usec = 500000; // Setup fd_set structure fd_set fds; FD_ZERO(&fds); //FD_SET(socket, &fds); // Return value: // -1: error occured // 0: timed out // >0: data ready to be read ret =select(0, &fds, 0, 0, &timeout); //ret =select(0, NULL, NULL, NULL, &timeout); //traceVerbose(bspIfTraceFilter, "real time: %ld sec %ld\n", timeout.tv_sec, timeout.tv_usec); return ret; } int main(int argc, char *argv[]) { #if 1 if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(EXIT_FAILURE); } uint8_t duration = atoi(argv[1]); int ret = 0; int count = 0; int led_status; duration *= 2; while (count++ < duration) { ret = get_gpio_time_out(); SetLed(GetLed()); //printf("real time: %ld sec %ld\n",timeout.tv_sec, timeout.tv_usec ); printf("LED is %s\n", (GetLed())?"On":"Off"); if(!ret) { if (led_status) led_status = false; else led_status= true; } } #else while(1){ fd_set rfds; struct timeval tv; int retval; /* Watch stdin (fd 0) to see when it has input. */ FD_ZERO(&rfds); /* Initialize the set of file descriptors */ //FD_SET(0, &rfds); /* Add stdin to the file descriptors set*/ /* Wait up to five seconds. */ tv.tv_sec = 5; tv.tv_usec = 0; retval = select(0, &rfds, NULL, NULL, &tv); /* Don't rely on the value of tv now! */ if (retval == -1) perror("select()"); else if (retval) printf("Data is available now.\n"); /* FD_ISSET(0, &rfds) will be true. */ else printf("No data within five seconds.\n"); } #endif return 0; }
Execute the timer_cb :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # time ./timer_cb 5 LED is Off LED is On LED is Off LED is On LED is Off LED is On LED is Off LED is On LED is Off LED is On real 0m5.004s user 0m0.000s sys 0m0.002s
usesignal():
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 //sighandler.c #include #include #include #include #include #ifndef bool typedef int bool; #define true 1 #define false 0 #endif typedef signed char int8_t;
typedef short int16_t; typedef int int32_t; typedef unsigned int uint32_t; typedef unsigned short uint16_t; typedef unsigned char uint8_t; bool sleeping=true; int count,last; static bool switches=true; static void SetLed(bool sw) { if(sw) switches = false; else switches = true; } static bool GetLed() { return switches; } static void sighandler(int sigty) { if (sigty==SIGUSR1){ struct itimerval v; getitimer(ITIMER_REAL,&v); printf("LED is %s\n", (GetLed())?"On":"Off"); printf("real time: %ld sec %ld\n",v.it_value.tv_sec ,v.it_value.tv_usec ); }else if (sigty==SIGALRM){ printf("timer stop\n"); sleeping=false; } } int LEDAlert(uint8_t rsrc, uint8_t duration) { struct itimerval v; if (signal(SIGUSR1, sighandler)==SIG_ERR){ printf("error on reg"); exit(1); } if (signal(SIGALRM, sighandler)==SIG_ERR){ printf("error on reg"); exit(2); } v.it_value.tv_sec = duration; /* Setting the timer */ v.it_value.tv_usec = 0; v.it_interval.tv_sec = 0; // Just do once. v.it_interval.tv_usec = 0; setitimer(ITIMER_REAL,&v,NULL); printf("timer has be seted\n"); count=0; last=v.it_value.tv_sec; sleeping=true; while (sleeping){ SetLed(GetLed()); getitimer(ITIMER_REAL,&v); int now = v.it_value.tv_sec; //printf("now: %d\n",now); if (now==last-1){ raise(SIGUSR1); count++; } last=now; } return 0; } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s\n", argv[0]); exit(EXIT_FAILURE); } return LEDAlert(0, atoi(argv[1]));
}
Execute the sighandler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 # time ./sighandler1 5 timer has be seted LED is Off real time: 4 sec 999945 LED is On real time: 3 sec 999987 LED is On real time: 2 sec 999987 LED is Off real time: 1 sec 999986 LED is Off real time: 0 sec 999987 timer stop real 0m5.003s user 0m2.349s sys 0m2.559s
use select():
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 // timer_cb.c #include
Execute the timer_cb :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # time ./timer_cb 5 LED is Off LED is On LED is Off LED is On LED is Off LED is On LED is Off LED is On LED is Off LED is On real 0m5.004s user 0m0.000s sys 0m0.002s
usesignal():
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 //sighandler.c #include
typedef short int16_t; typedef int int32_t; typedef unsigned int uint32_t; typedef unsigned short uint16_t; typedef unsigned char uint8_t; bool sleeping=true; int count,last; static bool switches=true; static void SetLed(bool sw) { if(sw) switches = false; else switches = true; } static bool GetLed() { return switches; } static void sighandler(int sigty) { if (sigty==SIGUSR1){ struct itimerval v; getitimer(ITIMER_REAL,&v); printf("LED is %s\n", (GetLed())?"On":"Off"); printf("real time: %ld sec %ld\n",v.it_value.tv_sec ,v.it_value.tv_usec ); }else if (sigty==SIGALRM){ printf("timer stop\n"); sleeping=false; } } int LEDAlert(uint8_t rsrc, uint8_t duration) { struct itimerval v; if (signal(SIGUSR1, sighandler)==SIG_ERR){ printf("error on reg"); exit(1); } if (signal(SIGALRM, sighandler)==SIG_ERR){ printf("error on reg"); exit(2); } v.it_value.tv_sec = duration; /* Setting the timer */ v.it_value.tv_usec = 0; v.it_interval.tv_sec = 0; // Just do once. v.it_interval.tv_usec = 0; setitimer(ITIMER_REAL,&v,NULL); printf("timer has be seted\n"); count=0; last=v.it_value.tv_sec; sleeping=true; while (sleeping){ SetLed(GetLed()); getitimer(ITIMER_REAL,&v); int now = v.it_value.tv_sec; //printf("now: %d\n",now); if (now==last-1){ raise(SIGUSR1); count++; } last=now; } return 0; } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s
}
Execute the sighandler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 # time ./sighandler1 5 timer has be seted LED is Off real time: 4 sec 999945 LED is On real time: 3 sec 999987 LED is On real time: 2 sec 999987 LED is Off real time: 1 sec 999986 LED is Off real time: 0 sec 999987 timer stop real 0m5.003s user 0m2.349s sys 0m2.559s
How to implement a timer event handler callba...
How to implement COMET with PHP
How to implement COMET with PHP
How to find a file?
How To Compile A Kernel
How to Start a Startup
how to read a paper
How to read a book!!!
how to make a sop
How to Propose a Toast
Scheduler,Handler和Event类的关系
How to write a service script ?
How to Build a Speech Recognition Circuit
How to Write a Spelling Corrector
How to Write a Book Report
How to Think Strategically in a Recession
how to be a good receptionist
How to Present like a Pro!
How to Skin CListCtrl, Including Scrollbars a...
How to Write a Book Report
服务员how to be a good receptionist
how to be a good teacher
How to Write a Good Recommendation
How to Be a Good Mentor