From owner-freebsd-hackers Tue Sep 29 10:08:01 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id KAA25724 for freebsd-hackers-outgoing; Tue, 29 Sep 1998 10:08:01 -0700 (PDT) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from citadel.cdsec.com (citadel.cdsec.com [192.96.22.18]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id KAA25689 for ; Tue, 29 Sep 1998 10:07:43 -0700 (PDT) (envelope-from gram@cdsec.com) Received: (from nobody@localhost) by citadel.cdsec.com (8.8.8/8.6.9) id TAA26340; Tue, 29 Sep 1998 19:15:02 +0200 (SAST) Received: by citadel via recvmail id 26289; Tue Sep 29 19:14:24 1998 From: Graham Wheeler Message-Id: <199809291713.TAA01742@cdsec.com> Subject: Re: Thread safe resolver library? To: bright@hotjobs.com (Alfred Perlstein) Date: Tue, 29 Sep 1998 19:13:05 +0200 (SAT) Cc: hackers@FreeBSD.ORG In-Reply-To: from "Alfred Perlstein" at Sep 29, 98 12:46:02 pm X-Mailer: ELM [version 2.4 PL25-h4.1] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG In case anyone wants to try this, here is the program. It is very basic; it's my first threads-based program. There are no mutexes or anything like that, but I don't think this should affect things. The main thread shares access to an array of unsigned longs with the child threads (each of which access one element of the array). The main thread sets an element to a non-zero value; the appropriate child thread should do a name lookup for that value and then set the value back to zero to tell the main thread it is ready for a new lookup. Run it, type in some numbers hitting enter in between, and eventually the whole thing should block. /*******************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #define MAX_THREADS 8 void Sleep(int dlay) { struct timeval tv; tv.tv_sec = dlay; tv.tv_usec = 0; (void)select(0, 0, 0, 0, &tv); } /* Lookup the name for an address */ int Lookup(unsigned long addr, char *name, int maxlen) { int x; char qbuf[40]; unsigned char ans[1024]; unsigned char *uaddr = (unsigned char *)&addr; sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff), (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff)); x = res_search(qbuf, C_IN, T_PTR, ans, maxlen); if (x>=0) ans[x] = 0; return (x < 0) ? -1 : 0; } /* Each thread runs a handler */ void *handler(void *arg) { unsigned long *addr = (unsigned long *)arg; /* ptr to address to look up */ for (;;) { if (*addr == 0) /* nothing to do */ Sleep(1); else { int x; char ans[PACKETSZ]; printf("Thread starting lookup\n", idx); x = Lookup(*addr, ans, sizeof(ans)); printf("Thread done lookup\n", idx); if (x==0) { fprintf(stdout, "%lX %s\n", *addr, ans); fflush(stdout); } else { fprintf(stdout, "%lX no name\n", *addr); fflush(stdout); } *addr = 0; } } } int main(int argc, char **argv) { int i; pthread_t threads[MAX_THREADS]; unsigned long addresses[MAX_THREADS]; /* spin off the threads */ for (i = 0; i < MAX_THREADS; i++) { addresses[i] = 0; if (pthread_create(&threads[i],NULL,handler,(void*)&addresses[i]) != 0) exit(-1); } /* read addresses in hex from the command line, and give them to threads to resolve */ while (!feof(stdin)) { unsigned long a; if (fscanf(stdin, "%lX", &a) == 1) { retry: for (i = 0; i < MAX_THREADS; i++) /* find an available thread */ { if (addresses[i] == 0) { addresses[i] = a; break; } } if (i == MAX_THREADS) /* no threads available */ { Sleep(1); goto retry; } } } return 0; } -- Dr Graham Wheeler E-mail: gram@cdsec.com Citadel Data Security Phone: +27(21)23-6065/6/7 Internet/Intranet Network Specialists Mobile: +27(83)253-9864 Firewalls/Virtual Private Networks Fax: +27(21)24-3656 Data Security Products WWW: http://www.cdsec.com/ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message