/* * */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #define ARRAY_SIZE(a) ((sizeof (a)) / sizeof (*(a))) #define BUFLEN 4096 // cyrus-sasl int check(void) { const char *addr = "0.0.0.0;7636"; char hbuf[NI_MAXHOST]; struct addrinfo hints, *ai = NULL; int i, j; /* Parse the address */ for (i = 0; addr[i] != '\0' && addr[i] != ';'; i++) { if (i >= NI_MAXHOST) return 3; hbuf[i] = addr[i]; } hbuf[i] = '\0'; if (addr[i] == ';') i++; /* XXX: Do we need this check? */ for (j = i; addr[j] != '\0'; j++) if (!isdigit((int)(addr[j]))) return 2; memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; if (getaddrinfo(hbuf, &addr[i], &hints, &ai) != 0) return 1; freeaddrinfo(ai); return 0; } static pthread_t thread[10]; static void *run(void *data) { unsigned int round, j; unsigned int self = (unsigned int)(unsigned long)data; pthread_t tid = pthread_self(); printf("[%d] POSIX thread ID is %u\n", self, (unsigned int)tid); for (round = 0; round < 1000; round++) { for (j = 0; j < 1000; j++) { int i; i = check(); if (i) break; } printf("%d\n", round); unsigned int usecs = (round * (unsigned int)tid) % 1000000; usleep(usecs); if (round % 10 == 0) putchar('0' + (self % 10)); } printf("[%d] end\n", self); pthread_exit(NULL); } int main(int argc, char *argv[], char *envp[]) { pid_t pid = getpid(); printf("pid=%d\n", pid); unsigned int t; for (t = 0; t < ARRAY_SIZE(thread); t++) pthread_create(&thread[t], NULL, run, (void *)(long)t); int ret = 0; void *value_ptr; for (t = 0; t < ARRAY_SIZE(thread); t++) ret |= pthread_join(thread[t], &value_ptr); return ret; } /* :!gcc -O0 -g -Wall -lpthread % */