©2004 by Marc J. Rochkind. All rights reserved. Portions marked "Open Source" may be copied under license.

 

Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

c9/tmr.c

Go to the documentation of this file.
00001 /*
00002     Timer and clock examples
00003     AUP2, Sec. 9.07.4, 9.07.5
00004 
00005     Copyright 2003 by Marc J. Rochkind. All rights reserved.
00006     May be copied only for purposes and under conditions described
00007     on the Web page www.basepath.com/aup/copyright.htm.
00008 
00009     The Example Files are provided "as is," without any warranty;
00010     without even the implied warranty of merchantability or fitness
00011     for a particular purpose. The author and his publisher are not
00012     responsible for any damages, direct or incidental, resulting
00013     from the use or non-use of these Example Files.
00014 
00015     The Example Files may contain defects, and some contain deliberate
00016     coding mistakes that were included for educational reasons.
00017     You are responsible for determining if and how the Example Files
00018     are to be used.
00019 
00020 */
00021 #include "defs.h"
00022 
00023 /*
00024     Linux defines _POSIX_TIMERS as > 0, but doesn't seem to support
00025     the feature. The headers seem to be OK, but there is no man page.
00026     If you figure this out, please send mail to aup@basepath.com.
00027 */
00028 
00029 #if _POSIX_TIMERS > 0 && !defined(LINUX)
00030 
00031 #include <sys/time.h>
00032 
00033 /*[handler]*/
00034 void handler(int signum)
00035 {
00036     write(STDOUT_FILENO, "\nX\n", 3);
00037 }
00038 /*[timer_try1]*/
00039 void timer_try1(void)
00040 {
00041     struct sigaction act;
00042     struct itimerval itv;
00043     char buf[100];
00044     ssize_t nread;
00045 
00046     memset(&act, 0, sizeof(act));
00047     act.sa_handler = handler;
00048     ec_neg1( sigaction(SIGALRM, &act, NULL) )
00049     memset(&itv, 0, sizeof(itv));
00050     itv.it_interval.tv_sec = 2;
00051     itv.it_value.tv_sec = 2;
00052     ec_neg1( setitimer(ITIMER_REAL, &itv, NULL) )
00053     while (true) {
00054         switch( nread = read(STDIN_FILENO, buf, sizeof(buf) - 1) ) {
00055         case -1:
00056             EC_FAIL
00057         case 0:
00058             printf("EOF\n");
00059             break;
00060         default:
00061             if (nread > 0)
00062                 buf[nread] = '\0';
00063             ec_neg1( write(STDOUT_FILENO, buf, strlen(buf)) )
00064             continue;
00065         }
00066         break;
00067     }
00068     return;
00069 
00070 EC_CLEANUP_BGN
00071     EC_FLUSH("timer_try1")
00072 EC_CLEANUP_END
00073 }
00074 
00075 void timer_try2(void)
00076 {
00077     struct sigaction act;
00078     struct itimerval itv;
00079     char buf[100];
00080     ssize_t nread;
00081 
00082 /*[timer_try2]*/
00083     memset(&act, 0, sizeof(act));
00084     act.sa_handler = handler;
00085     act.sa_flags = SA_RESTART;
00086     ec_neg1( sigaction(SIGALRM, &act, NULL) )
00087 /*[]*/
00088     memset(&itv, 0, sizeof(itv));
00089     itv.it_interval.tv_sec = 2;
00090     itv.it_value.tv_sec = 2;
00091     ec_neg1( setitimer(ITIMER_REAL, &itv, NULL) )
00092     while (true) {
00093         switch( nread = read(STDIN_FILENO, buf, sizeof(buf) - 1) ) {
00094         case -1:
00095             EC_FAIL
00096         case 0:
00097             printf("EOF\n");
00098             break;
00099         default:
00100             if (nread > 0)
00101                 buf[nread] = '\0';
00102             ec_neg1( write(STDOUT_FILENO, buf, strlen(buf)) )
00103             continue;
00104         }
00105         break;
00106     }
00107     return;
00108 
00109 EC_CLEANUP_BGN
00110     EC_FLUSH("timer_try2")
00111 EC_CLEANUP_END
00112 }
00113 /*[clocks]*/
00114 void clocks(void)
00115 {
00116     struct timespec ts;
00117     time_t tm;
00118 
00119     ec_neg1( time(&tm) )
00120     printf("time() Time: %ld secs.\n", (long)tm);
00121     printf("CLOCK_REALTIME:\n");
00122     ec_neg1( clock_gettime(CLOCK_REALTIME, &ts) )
00123     printf("Time: %ld.%09ld secs.\n", (long)ts.tv_sec, (long)ts.tv_nsec);
00124     ec_neg1( clock_getres(CLOCK_REALTIME, &ts) )
00125     printf("Res.: %ld.%09ld secs.\n", (long)ts.tv_sec, (long)ts.tv_nsec);
00126     return;
00127 
00128 EC_CLEANUP_BGN
00129     EC_FLUSH("clocks")
00130 EC_CLEANUP_END
00131 }
00132 /*[]*/
00133 int main(void)
00134 {
00135     clocks();
00136     exit(EXIT_SUCCESS);
00137 }
00138 
00139 #else /* _POSIX_TIMERS */
00140 
00141 int main(void)
00142 {
00143     printf("Not supported.\n");
00144     exit(1);
00145 }
00146 
00147 #endif /* _POSIX_TIMERS */

Generated on Fri Apr 23 10:57:03 2004 for AUP2 Example Source by doxygen 1.3.1