From owner-freebsd-hackers Fri Nov 19 10:19:33 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from bomber.avantgo.com (ws1.avantgo.com [207.214.200.194]) by hub.freebsd.org (Postfix) with ESMTP id 4A6FF14E24 for ; Fri, 19 Nov 1999 10:19:23 -0800 (PST) (envelope-from scott@avantgo.com) Received: from river ([10.0.128.30]) by bomber.avantgo.com (Netscape Messaging Server 3.5) with SMTP id 369; Fri, 19 Nov 1999 10:15:13 -0800 Message-ID: <03e501bf32ba$76d83550$1e80000a@avantgo.com> From: "Scott Hess" To: "Daniel Eischen" , References: <199911191814.NAA02164@pcnet1.pcnet.com> Subject: Re: EINTR problems with multithreaded programs. Date: Fri, 19 Nov 1999 10:18:22 -0800 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_03E2_01BF3277.68A11F30" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.00.2314.1300 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG This is a multi-part message in MIME format. ------=_NextPart_000_03E2_01BF3277.68A11F30 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Daniel Eischen wrote: >Scott Hess mailto:scott@avantgo.com wrote: > > When using -pthread on FreeBSD3.3 to build a multithreaded program, I find > > that signals are delivered to all threads (see attached program). > > Specifically, if multiple threads are in blocking read calls, and a signal > > is handled, they will all receive -1 from the read and EINTR in errno. > > If you don't want all threads to see the signal(s), then you > have to block the signal(s) in each thread. I've checked further, and found that FreeBSD correctly handles blocking signals on a per-thread basis. _But_, all threads still get EINTR when a signal happens while they're in a blocking read. I've attached the updated program that shows the correct delivery of the signals, with system calls still being interrupted. [Sorry about the attachment, but that seems the safest way to go about getting the file delivered in usable fashion.] Thanks, scott ------=_NextPart_000_03E2_01BF3277.68A11F30 Content-Type: application/octet-stream; name="thread_EINTR.c" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="thread_EINTR.c" #include =0A= #include =0A= #include =0A= #include =0A= =0A= static void signal_handler( int sig)=0A= {=0A= printf( "%p Got signal %d\n", pthread_self(), sig);=0A= }=0A= =0A= static void *reader( void *arg)=0A= {=0A= char buf[ 1024];=0A= int cc;=0A= int ii=3D(int)arg;=0A= int fd=3Ddup( 0);=0A= sigset_t set;=0A= =0A= sigemptyset( &set);=0A= if( (ii%3)=3D=3D0 || (ii%3)=3D=3D1) {=0A= sigaddset( &set, SIGUSR1);=0A= }=0A= if( (ii%3)=3D=3D0 || (ii%3)=3D=3D2) {=0A= sigaddset( &set, SIGUSR2);=0A= }=0A= pthread_sigmask( SIG_BLOCK, &set, NULL);=0A= =0A= {=0A= struct sigaction sa_usr1=3D{ signal_handler, 0, SA_RESTART};=0A= struct sigaction sa_usr2=3D{ signal_handler, 0, SA_RESTART};=0A= sigaction( SIGUSR1, &sa_usr1, NULL);=0A= sigaction( SIGUSR2, &sa_usr2, NULL);=0A= }=0A= =0A= printf( "%p/%d waiting for data\n", pthread_self(), ii);=0A= while(1) {=0A= cc=3Dread( fd, buf, sizeof( buf));=0A= if( cc>=3D0) {=0A= printf( "%p got %d bytes\n", pthread_self(), cc);=0A= } else {=0A= printf( "%p saw errno=3D=3D%d/%s\n", pthread_self(), errno, = strerror( errno));=0A= }=0A= }=0A= return NULL;=0A= }=0A= =0A= void main( void)=0A= {=0A= int ii, cc=3D9;=0A= pthread_t child;=0A= =0A= #if 0=0A= signal( SIGUSR1, signal_handler);=0A= #else=0A= {=0A= struct sigaction sa_usr1=3D{ signal_handler, 0, SA_RESTART};=0A= struct sigaction sa_usr2=3D{ signal_handler, 0, SA_RESTART};=0A= sigaction( SIGUSR1, &sa_usr1, NULL);=0A= sigaction( SIGUSR2, &sa_usr2, NULL);=0A= }=0A= #endif=0A= =0A= for( ii=3D0; ii