Date: Fri, 19 Nov 1999 10:18:22 -0800 From: "Scott Hess" <scott@avantgo.com> To: "Daniel Eischen" <eischen@vigrid.com>, <freebsd-hackers@FreeBSD.ORG> Subject: Re: EINTR problems with multithreaded programs. Message-ID: <03e501bf32ba$76d83550$1e80000a@avantgo.com> References: <199911191814.NAA02164@pcnet1.pcnet.com>
next in thread | previous in thread | raw e-mail | index | archive | help
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 <eischen@vigrid.com> 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 <pthread.h>=0A= #include <errno.h>=0A= #include <sys/types.h>=0A= #include <signal.h>=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<cc; ii++) {=0A= if( pthread_create( &child, NULL, reader, (void *)ii)) {=0A= perror( "spawning thread");=0A= exit( 1);=0A= }=0A= }=0A= while( 1) {=0A= sleep( 2);=0A= kill( getpid(), SIGUSR1);=0A= sleep( 2);=0A= kill( getpid(), SIGUSR2);=0A= }=0A= exit( 0);=0A= }=0A= ------=_NextPart_000_03E2_01BF3277.68A11F30-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?03e501bf32ba$76d83550$1e80000a>