source: trunk/src/lib/test-eintr-bug-2.c

Last change on this file was 2474, checked in by bird, 14 years ago

adj.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.6 KB
RevLine 
[2471]1
2
3/*******************************************************************************
4* Header Files *
5*******************************************************************************/
6#define _BSD_SOURCE
[2472]7#define _GNU_SOURCE
[2471]8#include <sys/time.h>
9#include <sys/stat.h>
[2474]10#include <fcntl.h>
[2472]11#include <time.h>
[2471]12#include <stdio.h>
13#include <string.h>
14#include <errno.h>
[2472]15#include <pthread.h>
16#include <signal.h>
[2474]17#include <unistd.h>
[2471]18
19
[2472]20/*******************************************************************************
21* Global Variables *
22*******************************************************************************/
23/** The number of signals. */
24static volatile long g_cSigs = 0;
[2474]25/** Number of signals received on threads other than the main one. */
26static volatile long g_cSigsOther = 0;
[2472]27/** Whether to shutdown or not. */
28static volatile int g_fShutdown = 0;
29/** The handle of the main thread. */
30static pthread_t g_hMainThread;
31
32
33static void SigHandler(int iSig)
[2471]34{
[2472]35 g_cSigs++;
[2474]36 if (pthread_self() != g_hMainThread)
37 g_cSigsOther++;
[2472]38
[2471]39 (void)iSig;
40}
41
42
[2472]43static void NanoSleep(unsigned long cNanoSecs)
44{
45 struct timespec Ts;
46 Ts.tv_sec = 0;
47 Ts.tv_nsec = cNanoSecs;
48 nanosleep(&Ts, NULL);
49}
50
51
52static void *ThreadProc(void *pvIgnored)
53{
[2474]54 int volatile i = 0;
[2472]55 while (!g_fShutdown)
56 {
[2474]57// NanoSleep(850);
[2472]58 if (g_fShutdown)
59 break;
60
61 pthread_kill(g_hMainThread, SIGALRM);
[2474]62 for (i = 6666; i > 0; i--)
63 /* nothing */;
[2472]64 }
65 return NULL;
66}
67
68
[2471]69int main(int argc, char **argv)
70{
71 void (*rcSig)(int);
[2472]72 pthread_t hThread;
73 char szName[1024];
[2471]74 int i;
75 int rc;
76
77 /*
[2472]78 * Set up the signal handlers.
[2471]79 */
[2472]80 rcSig = bsd_signal(SIGALRM, SigHandler);
81 if (rcSig != SIG_ERR)
82 rcSig = bsd_signal(SIGCHLD, SigHandler);
[2471]83 if (rcSig == SIG_ERR)
84 {
85 fprintf(stderr, "bsd_signal failed: %s\n", strerror(errno));
86 return 1;
87 }
88 if (argc == 2) /* testing... */
[2472]89 {
[2471]90 siginterrupt(SIGALRM, 1);
[2472]91 siginterrupt(SIGCHLD, 1);
92 }
[2471]93
[2472]94 /*
95 * Kick off a thread that will signal us like there was no tomorrow.
96 */
97 g_hMainThread = pthread_self();
98 rc = pthread_create(&hThread, NULL, ThreadProc, NULL);
[2471]99 if (rc != 0)
100 {
[2472]101 fprintf(stderr, "pthread_create failed: %s\n", strerror(rc));
[2471]102 return 1;
103 }
104
105 /*
106 * Do path related stuff.
107 */
[2472]108 snprintf(szName, sizeof(szName), "%s-test2", argv[0]);
[2471]109 for (i = 0; i < 100*1000*1000; i++)
110 {
111 struct stat St;
[2474]112 int fd;
[2472]113
[2471]114 rc = stat(argv[0], &St);
[2472]115 if (rc == 0 || errno != EINTR)
116 rc = stat(szName, &St);
117 if (errno == EINTR && rc != 0)
[2471]118 {
119 printf("iteration %d: stat: %u\n", i, errno);
120 break;
121 }
[2474]122
123 fd = open(szName, O_CREAT | O_RDWR, 0666);
124 if (errno == EINTR && fd < 0)
125 {
126 printf("iteration %d: open: %u\n", i, errno);
127 break;
128 }
129 close(fd);
130 rc = unlink(szName);
131 if (errno == EINTR && rc != 0)
132 {
133 printf("iteration %d: unlink: %u\n", i, errno);
134 break;
135 }
136
137 /* Show progress info */
[2472]138 if ((i % 100000) == 0)
139 {
140 printf(".");
141 if ((i % 1000000) == 0)
[2474]142 printf("[%d/%ld/%ld]\n", i, g_cSigs, g_cSigsOther);
[2472]143 fflush(stdout);
144 }
[2471]145 }
146
[2472]147 g_fShutdown = 1;
[2471]148 if (rc)
149 printf("No EINTR in %d iterations - system is working nicely!\n", i);
[2472]150 NanoSleep(10000000);
[2471]151
152 return rc ? 1 : 0;
153}
154
Note: See TracBrowser for help on using the repository browser.