Changeset 2472 in kBuild for trunk/src/lib
- Timestamp:
- Jul 12, 2011 2:12:57 PM (14 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/test-eintr-bug-2.c
r2471 r2472 4 4 * Header Files * 5 5 *******************************************************************************/ 6 #define _XOPEN_SOURCE7 6 #define _BSD_SOURCE 7 #define _GNU_SOURCE 8 8 #include <sys/time.h> 9 9 #include <sys/stat.h> 10 #include <time.h> 10 11 #include <stdio.h> 11 #include <signal.h>12 12 #include <string.h> 13 13 #include <errno.h> 14 #include <pthread.h> 15 #include <signal.h> 14 16 15 17 16 static void SigAlaramHandler(int iSig) 18 /******************************************************************************* 19 * Global Variables * 20 *******************************************************************************/ 21 /** The number of signals. */ 22 static volatile long g_cSigs = 0; 23 /** Whether to shutdown or not. */ 24 static volatile int g_fShutdown = 0; 25 /** The handle of the main thread. */ 26 static pthread_t g_hMainThread; 27 28 29 static void SigHandler(int iSig) 17 30 { 18 /* ignore */ 31 g_cSigs++; 32 19 33 (void)iSig; 34 } 35 36 37 static void NanoSleep(unsigned long cNanoSecs) 38 { 39 struct timespec Ts; 40 Ts.tv_sec = 0; 41 Ts.tv_nsec = cNanoSecs; 42 nanosleep(&Ts, NULL); 43 } 44 45 46 static void *ThreadProc(void *pvIgnored) 47 { 48 while (!g_fShutdown) 49 { 50 NanoSleep(850); 51 if (g_fShutdown) 52 break; 53 54 pthread_kill(g_hMainThread, SIGALRM); 55 } 56 return NULL; 20 57 } 21 58 … … 23 60 int main(int argc, char **argv) 24 61 { 25 struct itimerval TmrVal;26 62 void (*rcSig)(int); 63 pthread_t hThread; 64 char szName[1024]; 27 65 int i; 28 66 int rc; 29 67 30 68 /* 31 * Set up the timer signal.69 * Set up the signal handlers. 32 70 */ 33 rcSig = bsd_signal(SIGALRM, SigAlaramHandler); 71 rcSig = bsd_signal(SIGALRM, SigHandler); 72 if (rcSig != SIG_ERR) 73 rcSig = bsd_signal(SIGCHLD, SigHandler); 34 74 if (rcSig == SIG_ERR) 35 75 { … … 38 78 } 39 79 if (argc == 2) /* testing... */ 80 { 40 81 siginterrupt(SIGALRM, 1); 82 siginterrupt(SIGCHLD, 1); 83 } 41 84 42 memset(&TmrVal, '\0', sizeof(TmrVal)); 43 TmrVal.it_interval.tv_sec = TmrVal.it_value.tv_sec = 0; 44 TmrVal.it_interval.tv_usec = TmrVal.it_value.tv_usec = 1000; 45 rc = setitimer(ITIMER_REAL, &TmrVal, NULL); 85 /* 86 * Kick off a thread that will signal us like there was no tomorrow. 87 */ 88 g_hMainThread = pthread_self(); 89 rc = pthread_create(&hThread, NULL, ThreadProc, NULL); 46 90 if (rc != 0) 47 91 { 48 fprintf(stderr, " setitimer failed: %s\n", strerror(errno));92 fprintf(stderr, "pthread_create failed: %s\n", strerror(rc)); 49 93 return 1; 50 94 } … … 53 97 * Do path related stuff. 54 98 */ 99 snprintf(szName, sizeof(szName), "%s-test2", argv[0]); 55 100 for (i = 0; i < 100*1000*1000; i++) 56 101 { 57 102 struct stat St; 103 58 104 rc = stat(argv[0], &St); 59 if (rc != 0) 105 if (rc == 0 || errno != EINTR) 106 rc = stat(szName, &St); 107 if (errno == EINTR && rc != 0) 60 108 { 61 109 printf("iteration %d: stat: %u\n", i, errno); 62 110 break; 63 111 } 112 if ((i % 100000) == 0) 113 { 114 printf("."); 115 if ((i % 1000000) == 0) 116 printf("[%d/%ld]", i, g_cSigs); 117 fflush(stdout); 118 } 64 119 } 65 120 121 g_fShutdown = 1; 66 122 if (rc) 67 123 printf("No EINTR in %d iterations - system is working nicely!\n", i); 68 69 TmrVal.it_interval.tv_sec = TmrVal.it_value.tv_sec = 0; 70 TmrVal.it_interval.tv_usec = TmrVal.it_value.tv_usec = 0; 71 setitimer(ITIMER_REAL, &TmrVal, NULL); 124 NanoSleep(10000000); 72 125 73 126 return rc ? 1 : 0;
Note:
See TracChangeset
for help on using the changeset viewer.